stk-table-vue 0.6.13 → 0.6.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -646,12 +646,12 @@ declare const __VLS_component: import('vue').DefineComponent<import('vue').Extra
646
646
  maxWidth: string;
647
647
  sortConfig: SortConfig<DT>;
648
648
  rowHeight: number;
649
- headerRowHeight: number | null;
650
649
  headless: boolean;
651
650
  autoRowHeight: boolean | AutoRowHeightConfig<DT>;
652
651
  stripe: boolean;
653
652
  optimizeVue2Scroll: boolean;
654
653
  rowKey: UniqKeyProp;
654
+ headerRowHeight: number | null;
655
655
  colKey: UniqKeyProp;
656
656
  fixedMode: boolean;
657
657
  theme: "light" | "dark";
@@ -211,7 +211,7 @@ export type HeaderDragConfig<DT extends Record<string, any> = any> = boolean | {
211
211
  };
212
212
  export type AutoRowHeightConfig<DT> = {
213
213
  /** Estimated row height */
214
- expectedHeight?: number | ((row: DT, index: number) => number);
214
+ expectedHeight?: number | ((row: DT) => number);
215
215
  };
216
216
  export type ColResizableConfig<DT extends Record<string, any>> = {
217
217
  disabled: (col: StkTableColumn<DT>) => boolean;
@@ -1061,7 +1061,7 @@ function useVirtualScroll({
1061
1061
  const virtualScroll = ref({
1062
1062
  containerHeight: 0,
1063
1063
  rowHeight: props.rowHeight,
1064
- pageSize: 10,
1064
+ pageSize: 0,
1065
1065
  startIndex: 0,
1066
1066
  endIndex: 0,
1067
1067
  offsetTop: 0,
@@ -1080,7 +1080,7 @@ function useVirtualScroll({
1080
1080
  return tableHeaderLast.value.some((col) => col.type === "expand");
1081
1081
  });
1082
1082
  const virtual_on = computed(() => {
1083
- return props.virtual && dataSourceCopy.value.length > virtualScroll.value.pageSize * 2;
1083
+ return props.virtual && dataSourceCopy.value.length > virtualScroll.value.pageSize;
1084
1084
  });
1085
1085
  const virtual_dataSourcePart = computed(() => {
1086
1086
  if (!virtual_on.value) return dataSourceCopy.value;
@@ -1089,8 +1089,18 @@ function useVirtualScroll({
1089
1089
  });
1090
1090
  const virtual_offsetBottom = computed(() => {
1091
1091
  if (!virtual_on.value) return 0;
1092
- const { startIndex, rowHeight } = virtualScroll.value;
1093
- return (dataSourceCopy.value.length - startIndex - virtual_dataSourcePart.value.length) * rowHeight;
1092
+ const { startIndex, endIndex } = virtualScroll.value;
1093
+ const dataSourceCopyValue = dataSourceCopy.value;
1094
+ const rowHeight = getRowHeightFn.value();
1095
+ if (props.autoRowHeight) {
1096
+ let offsetBottom = 0;
1097
+ for (let i = endIndex; i < dataSourceCopyValue.length; i++) {
1098
+ const rowHeight2 = getRowHeightFn.value(dataSourceCopyValue[i]);
1099
+ offsetBottom += rowHeight2;
1100
+ }
1101
+ return offsetBottom;
1102
+ }
1103
+ return (dataSourceCopyValue.length - startIndex - virtual_dataSourcePart.value.length) * rowHeight;
1094
1104
  });
1095
1105
  const virtualX_on = computed(() => {
1096
1106
  return props.virtualX && tableHeaderLast.value.reduce((sum, col) => sum += getCalculatedColWidth(col), 0) > virtualScrollX.value.containerWidth + 100;
@@ -1126,9 +1136,22 @@ function useVirtualScroll({
1126
1136
  }
1127
1137
  return width;
1128
1138
  });
1139
+ const getRowHeightFn = computed(() => {
1140
+ var _a;
1141
+ let rowHeightFn = () => props.rowHeight || DEFAULT_ROW_HEIGHT;
1142
+ if (props.autoRowHeight) {
1143
+ const tempRowHeightFn = rowHeightFn;
1144
+ rowHeightFn = (row) => getAutoRowHeight(row) || tempRowHeightFn(row);
1145
+ }
1146
+ if (hasExpandCol.value) {
1147
+ const expandedRowHeight = (_a = props.expandConfig) == null ? void 0 : _a.height;
1148
+ const tempRowHeightFn = rowHeightFn;
1149
+ rowHeightFn = (row) => row && row.__EXPANDED_ROW__ && expandedRowHeight || tempRowHeightFn(row);
1150
+ }
1151
+ return rowHeightFn;
1152
+ });
1129
1153
  function getTableHeaderHeight() {
1130
- const { headerRowHeight } = props;
1131
- return headerRowHeight * tableHeaders.value.length;
1154
+ return props.headerRowHeight * tableHeaders.value.length;
1132
1155
  }
1133
1156
  function initVirtualScroll(height) {
1134
1157
  initVirtualScrollY(height);
@@ -1140,10 +1163,9 @@ function useVirtualScroll({
1140
1163
  console.warn("initVirtualScrollY: height must be a number");
1141
1164
  height = 0;
1142
1165
  }
1143
- if (!virtual_on.value) return;
1144
1166
  const { offsetHeight, scrollHeight } = tableContainerRef.value || {};
1145
1167
  let scrollTop = ((_a = tableContainerRef.value) == null ? void 0 : _a.scrollTop) || 0;
1146
- const { rowHeight } = virtualScroll.value;
1168
+ const rowHeight = getRowHeightFn.value(props.dataSource[0]);
1147
1169
  const containerHeight = height || offsetHeight || DEFAULT_TABLE_HEIGHT;
1148
1170
  const { headless } = props;
1149
1171
  let pageSize = Math.ceil(containerHeight / rowHeight);
@@ -1170,9 +1192,9 @@ function useVirtualScroll({
1170
1192
  const autoRowHeightMap = /* @__PURE__ */ new Map();
1171
1193
  function setAutoHeight(rowKey, height) {
1172
1194
  if (!height) {
1173
- autoRowHeightMap.delete(rowKey);
1195
+ autoRowHeightMap.delete(String(rowKey));
1174
1196
  } else {
1175
- autoRowHeightMap.set(rowKey, height);
1197
+ autoRowHeightMap.set(String(rowKey), height);
1176
1198
  }
1177
1199
  }
1178
1200
  function clearAllAutoHeight() {
@@ -1180,8 +1202,9 @@ function useVirtualScroll({
1180
1202
  }
1181
1203
  function getAutoRowHeight(row) {
1182
1204
  var _a;
1183
- const rowKey = String(rowKeyGen(row));
1184
- const storedHeight = autoRowHeightMap.get(rowKey);
1205
+ if (!row) return;
1206
+ const rowKey = rowKeyGen(row);
1207
+ const storedHeight = autoRowHeightMap.get(String(rowKey));
1185
1208
  if (storedHeight) {
1186
1209
  return storedHeight;
1187
1210
  }
@@ -1193,32 +1216,19 @@ function useVirtualScroll({
1193
1216
  return expectedHeight;
1194
1217
  }
1195
1218
  }
1196
- return props.rowHeight || DEFAULT_ROW_HEIGHT;
1197
- }
1198
- function createGetRowHeightFn() {
1199
- var _a;
1200
- if (props.autoRowHeight) {
1201
- return (row) => getAutoRowHeight(row);
1202
- }
1203
- if (hasExpandCol.value) {
1204
- const { rowHeight } = virtualScroll.value;
1205
- const expandedRowHeight = ((_a = props.expandConfig) == null ? void 0 : _a.height) || rowHeight;
1206
- return (row) => row.__EXPANDED_ROW__ ? expandedRowHeight : rowHeight;
1207
- }
1208
- return () => props.rowHeight || DEFAULT_ROW_HEIGHT;
1209
1219
  }
1210
1220
  function updateVirtualScrollY(sTop = 0) {
1211
1221
  var _a;
1212
- const { rowHeight, pageSize, scrollTop, startIndex: oldStartIndex, endIndex: oldEndIndex } = virtualScroll.value;
1222
+ const { pageSize, scrollTop, startIndex: oldStartIndex, endIndex: oldEndIndex } = virtualScroll.value;
1213
1223
  virtualScroll.value.scrollTop = sTop;
1214
1224
  if (!virtual_on.value) {
1215
1225
  return;
1216
1226
  }
1217
1227
  const dataSourceCopyTemp = dataSourceCopy.value;
1228
+ const rowHeight = getRowHeightFn.value(dataSourceCopyTemp[0]);
1218
1229
  const { autoRowHeight, stripe, optimizeVue2Scroll } = props;
1219
1230
  let startIndex = 0;
1220
1231
  let autoRowHeightTop = 0;
1221
- let getRowHeight = null;
1222
1232
  const dataLength = dataSourceCopyTemp.length;
1223
1233
  if (autoRowHeight || hasExpandCol.value) {
1224
1234
  if (autoRowHeight) {
@@ -1228,11 +1238,10 @@ function useVirtualScroll({
1228
1238
  autoRowHeightMap.set(rowKey, tr.offsetHeight);
1229
1239
  });
1230
1240
  }
1231
- getRowHeight = createGetRowHeightFn();
1232
1241
  for (let i = 0; i < dataLength; i++) {
1233
- const height = getRowHeight(dataSourceCopyTemp[i]);
1242
+ const height = getRowHeightFn.value(dataSourceCopyTemp[i]);
1234
1243
  autoRowHeightTop += height;
1235
- if (autoRowHeightTop >= sTop) {
1244
+ if (autoRowHeightTop > sTop) {
1236
1245
  startIndex = i;
1237
1246
  autoRowHeightTop -= height;
1238
1247
  break;
@@ -1245,7 +1254,7 @@ function useVirtualScroll({
1245
1254
  if (stripe && startIndex > 0 && startIndex % 2) {
1246
1255
  startIndex -= 1;
1247
1256
  if (autoRowHeight || hasExpandCol.value) {
1248
- const height = getRowHeight(dataSourceCopyTemp[startIndex]);
1257
+ const height = getRowHeightFn.value(dataSourceCopyTemp[startIndex]);
1249
1258
  autoRowHeightTop -= height;
1250
1259
  }
1251
1260
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stk-table-vue",
3
- "version": "0.6.13",
3
+ "version": "0.6.15",
4
4
  "description": "Simple realtime virtual table for vue3 and vue2.7",
5
5
  "main": "./lib/stk-table-vue.js",
6
6
  "types": "./lib/src/StkTable/index.d.ts",
@@ -65,6 +65,7 @@
65
65
  "vite-plugin-dts": "3.9.1",
66
66
  "vitepress": "^1.5.0",
67
67
  "vitepress-demo-plugin": "^1.3.1",
68
+ "vitepress-plugin-llms": "^1.1.3",
68
69
  "vitest": "^2.1.3",
69
70
  "vue": "^3.5.12",
70
71
  "vue-eslint-parser": "^9.4.2"
@@ -231,7 +231,7 @@ export type HeaderDragConfig<DT extends Record<string, any> = any> =
231
231
 
232
232
  export type AutoRowHeightConfig<DT> = {
233
233
  /** Estimated row height */
234
- expectedHeight?: number | ((row: DT, index: number) => number);
234
+ expectedHeight?: number | ((row: DT) => number);
235
235
  };
236
236
 
237
237
  export type ColResizableConfig<DT extends Record<string, any>> = {
@@ -1,6 +1,6 @@
1
1
  import { Ref, ShallowRef, computed, ref } from 'vue';
2
2
  import { DEFAULT_ROW_HEIGHT, DEFAULT_TABLE_HEIGHT, DEFAULT_TABLE_WIDTH } from './const';
3
- import { StkTableColumn, UniqKey } from './types';
3
+ import { AutoRowHeightConfig, StkTableColumn, UniqKey } from './types';
4
4
  import { getCalculatedColWidth } from './utils/constRefUtils';
5
5
 
6
6
  type Option<DT extends Record<string, any>> = {
@@ -71,7 +71,7 @@ export function useVirtualScroll<DT extends Record<string, any>>({
71
71
  const virtualScroll = ref<VirtualScrollStore>({
72
72
  containerHeight: 0,
73
73
  rowHeight: props.rowHeight,
74
- pageSize: 10,
74
+ pageSize: 0,
75
75
  startIndex: 0,
76
76
  endIndex: 0,
77
77
  offsetTop: 0,
@@ -96,7 +96,7 @@ export function useVirtualScroll<DT extends Record<string, any>>({
96
96
 
97
97
  /** 是否虚拟滚动标志 */
98
98
  const virtual_on = computed(() => {
99
- return props.virtual && dataSourceCopy.value.length > virtualScroll.value.pageSize * 2;
99
+ return props.virtual && dataSourceCopy.value.length > virtualScroll.value.pageSize;
100
100
  });
101
101
 
102
102
  const virtual_dataSourcePart = computed(() => {
@@ -107,8 +107,19 @@ export function useVirtualScroll<DT extends Record<string, any>>({
107
107
 
108
108
  const virtual_offsetBottom = computed(() => {
109
109
  if (!virtual_on.value) return 0;
110
- const { startIndex, rowHeight } = virtualScroll.value;
111
- return (dataSourceCopy.value.length - startIndex - virtual_dataSourcePart.value.length) * rowHeight;
110
+ const { startIndex, endIndex } = virtualScroll.value;
111
+ const dataSourceCopyValue = dataSourceCopy.value;
112
+ const rowHeight = getRowHeightFn.value();
113
+ if (props.autoRowHeight) {
114
+ let offsetBottom = 0;
115
+ for (let i = endIndex; i < dataSourceCopyValue.length; i++) {
116
+ const rowHeight = getRowHeightFn.value(dataSourceCopyValue[i]);
117
+ offsetBottom += rowHeight;
118
+ }
119
+ return offsetBottom;
120
+ }
121
+
122
+ return (dataSourceCopyValue.length - startIndex - virtual_dataSourcePart.value.length) * rowHeight;
112
123
  });
113
124
 
114
125
  const virtualX_on = computed(() => {
@@ -162,10 +173,22 @@ export function useVirtualScroll<DT extends Record<string, any>>({
162
173
  return width;
163
174
  });
164
175
 
165
- /** 表头高度 */
176
+ const getRowHeightFn = computed(() => {
177
+ let rowHeightFn: (row?: DT) => number = () => props.rowHeight || DEFAULT_ROW_HEIGHT;
178
+ if (props.autoRowHeight) {
179
+ const tempRowHeightFn = rowHeightFn;
180
+ rowHeightFn = (row?: DT) => getAutoRowHeight(row) || tempRowHeightFn(row);
181
+ }
182
+ if (hasExpandCol.value) {
183
+ const expandedRowHeight = props.expandConfig?.height;
184
+ const tempRowHeightFn = rowHeightFn;
185
+ rowHeightFn = (row?: DT) => (row && row.__EXPANDED_ROW__ && expandedRowHeight) || tempRowHeightFn(row);
186
+ }
187
+ return rowHeightFn;
188
+ });
189
+
166
190
  function getTableHeaderHeight() {
167
- const { headerRowHeight } = props;
168
- return headerRowHeight * tableHeaders.value.length;
191
+ return props.headerRowHeight * tableHeaders.value.length;
169
192
  }
170
193
 
171
194
  /**
@@ -186,11 +209,10 @@ export function useVirtualScroll<DT extends Record<string, any>>({
186
209
  console.warn('initVirtualScrollY: height must be a number');
187
210
  height = 0;
188
211
  }
189
- if (!virtual_on.value) return;
190
212
  const { offsetHeight, scrollHeight } = tableContainerRef.value || {};
191
213
  let scrollTop = tableContainerRef.value?.scrollTop || 0;
192
214
 
193
- const { rowHeight } = virtualScroll.value;
215
+ const rowHeight = getRowHeightFn.value(props.dataSource[0]);
194
216
  const containerHeight = height || offsetHeight || DEFAULT_TABLE_HEIGHT;
195
217
  const { headless } = props;
196
218
  let pageSize = Math.ceil(containerHeight / rowHeight);
@@ -220,13 +242,13 @@ export function useVirtualScroll<DT extends Record<string, any>>({
220
242
  let vue2ScrollYTimeout: null | number = null;
221
243
 
222
244
  /** every row actual height */
223
- const autoRowHeightMap = new Map<UniqKey, number>();
245
+ const autoRowHeightMap = new Map<string, number>();
224
246
  /** 如果行高度有变化,则要调用此方法清除保存的行高 */
225
247
  function setAutoHeight(rowKey: UniqKey, height?: number | null) {
226
248
  if (!height) {
227
- autoRowHeightMap.delete(rowKey);
249
+ autoRowHeightMap.delete(String(rowKey));
228
250
  } else {
229
- autoRowHeightMap.set(rowKey, height);
251
+ autoRowHeightMap.set(String(rowKey), height);
230
252
  }
231
253
  }
232
254
 
@@ -234,13 +256,14 @@ export function useVirtualScroll<DT extends Record<string, any>>({
234
256
  autoRowHeightMap.clear();
235
257
  }
236
258
 
237
- function getAutoRowHeight(row: DT) {
238
- const rowKey = String(rowKeyGen(row));
239
- const storedHeight = autoRowHeightMap.get(rowKey);
259
+ function getAutoRowHeight(row?: DT) {
260
+ if (!row) return;
261
+ const rowKey = rowKeyGen(row);
262
+ const storedHeight = autoRowHeightMap.get(String(rowKey));
240
263
  if (storedHeight) {
241
264
  return storedHeight;
242
265
  }
243
- const expectedHeight = props.autoRowHeight?.expectedHeight;
266
+ const expectedHeight: AutoRowHeightConfig<DT>['expectedHeight'] = props.autoRowHeight?.expectedHeight;
244
267
  if (expectedHeight) {
245
268
  if (typeof expectedHeight === 'function') {
246
269
  return expectedHeight(row);
@@ -248,24 +271,11 @@ export function useVirtualScroll<DT extends Record<string, any>>({
248
271
  return expectedHeight;
249
272
  }
250
273
  }
251
- return props.rowHeight || DEFAULT_ROW_HEIGHT;
252
- }
253
-
254
- function createGetRowHeightFn(): (row: DT) => number {
255
- if (props.autoRowHeight) {
256
- return (row: DT) => getAutoRowHeight(row);
257
- }
258
- if (hasExpandCol.value) {
259
- const { rowHeight } = virtualScroll.value;
260
- const expandedRowHeight: number = props.expandConfig?.height || rowHeight;
261
- return (row: DT) => (row.__EXPANDED_ROW__ ? expandedRowHeight : rowHeight);
262
- }
263
- return () => props.rowHeight || (DEFAULT_ROW_HEIGHT as number);
264
274
  }
265
275
 
266
276
  /** 通过滚动条位置,计算虚拟滚动的参数 */
267
277
  function updateVirtualScrollY(sTop = 0) {
268
- const { rowHeight, pageSize, scrollTop, startIndex: oldStartIndex, endIndex: oldEndIndex } = virtualScroll.value;
278
+ const { pageSize, scrollTop, startIndex: oldStartIndex, endIndex: oldEndIndex } = virtualScroll.value;
269
279
  // 先更新滚动条位置记录,其他地方有依赖。(stripe 时ArrowUp/Down滚动依赖)
270
280
  virtualScroll.value.scrollTop = sTop;
271
281
 
@@ -275,11 +285,11 @@ export function useVirtualScroll<DT extends Record<string, any>>({
275
285
  }
276
286
 
277
287
  const dataSourceCopyTemp = dataSourceCopy.value;
288
+ const rowHeight = getRowHeightFn.value(dataSourceCopyTemp[0]);
278
289
  const { autoRowHeight, stripe, optimizeVue2Scroll } = props;
279
290
 
280
291
  let startIndex = 0;
281
292
  let autoRowHeightTop = 0;
282
- let getRowHeight: ReturnType<typeof createGetRowHeightFn> | null = null;
283
293
  const dataLength = dataSourceCopyTemp.length;
284
294
 
285
295
  if (autoRowHeight || hasExpandCol.value) {
@@ -291,12 +301,10 @@ export function useVirtualScroll<DT extends Record<string, any>>({
291
301
  });
292
302
  }
293
303
 
294
- getRowHeight = createGetRowHeightFn();
295
-
296
304
  for (let i = 0; i < dataLength; i++) {
297
- const height = getRowHeight(dataSourceCopyTemp[i]);
305
+ const height = getRowHeightFn.value(dataSourceCopyTemp[i]);
298
306
  autoRowHeightTop += height;
299
- if (autoRowHeightTop >= sTop) {
307
+ if (autoRowHeightTop > sTop) {
300
308
  startIndex = i;
301
309
  autoRowHeightTop -= height;
302
310
  break;
@@ -312,7 +320,7 @@ export function useVirtualScroll<DT extends Record<string, any>>({
312
320
  // 斑马纹情况下,每滚动偶数行才加载。防止斑马纹错位。
313
321
  startIndex -= 1; // 奇数-1变成偶数
314
322
  if (autoRowHeight || hasExpandCol.value) {
315
- const height = getRowHeight!(dataSourceCopyTemp[startIndex]);
323
+ const height = getRowHeightFn.value(dataSourceCopyTemp[startIndex]);
316
324
  autoRowHeightTop -= height;
317
325
  }
318
326
  }