stk-table-vue 0.7.0 → 0.7.1

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.
@@ -38,7 +38,7 @@ export type CustomCell<T extends CustomCellProps<U> | CustomHeaderCellProps<U>,
38
38
  /** 表格列配置 */
39
39
  export type StkTableColumn<T extends Record<string, any>> = {
40
40
  /**
41
- * 用于区分相同dataIndex 的列。
41
+ * 列唯一键,(可选),不传则默认取dataIndex 字段作为列唯一键。
42
42
  */
43
43
  key?: any;
44
44
  /**
@@ -14,7 +14,9 @@ export declare function isEmptyValue(val: any, isNumber?: boolean): boolean;
14
14
  * @param targetArray 表格数据
15
15
  * @return targetArray 的浅拷贝
16
16
  */
17
- export declare function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: T, targetArray: T[], sortConfig?: SortConfig<T>): T[];
17
+ export declare function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: T, targetArray: T[], sortConfig?: SortConfig<T> & {
18
+ customCompare?: (a: T, b: T) => number;
19
+ }): T[];
18
20
  /**
19
21
  * 二分查找
20
22
  * @param searchArray 查找数组
@@ -61,7 +61,6 @@ function isEmptyValue(val, isNumber) {
61
61
  }
62
62
  function insertToOrderedArray(sortState, newItem, targetArray, sortConfig = {}) {
63
63
  const { dataIndex, sortField, order } = sortState;
64
- sortConfig = { emptyToBottom: false, ...sortConfig };
65
64
  let { sortType } = sortState;
66
65
  const field = sortField || dataIndex;
67
66
  if (!sortType) sortType = typeof newItem[field];
@@ -70,15 +69,19 @@ function insertToOrderedArray(sortState, newItem, targetArray, sortConfig = {})
70
69
  data.unshift(newItem);
71
70
  return data;
72
71
  }
72
+ const { emptyToBottom, customCompare, stringLocaleCompare } = { emptyToBottom: false, ...sortConfig };
73
73
  const targetVal = newItem[field];
74
- if (sortConfig.emptyToBottom && isEmptyValue(targetVal)) {
74
+ if (emptyToBottom && isEmptyValue(targetVal)) {
75
75
  data.push(newItem);
76
76
  } else {
77
+ const customCompareFn = customCompare || ((a, b) => {
78
+ const midVal = a[field];
79
+ const compareRes = strCompare(midVal, targetVal, isNumber, stringLocaleCompare);
80
+ return order === "asc" ? compareRes : -compareRes;
81
+ });
77
82
  const isNumber = sortType === "number";
78
83
  const sIndex = binarySearch(data, (midIndex) => {
79
- const midVal = data[midIndex][field];
80
- const compareRes = strCompare(midVal, targetVal, isNumber, sortConfig.stringLocaleCompare);
81
- return order === "asc" ? compareRes : -compareRes;
84
+ return customCompareFn(data[midIndex], newItem);
82
85
  });
83
86
  data.splice(sIndex, 0, newItem);
84
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stk-table-vue",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
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",
@@ -40,7 +40,6 @@
40
40
  },
41
41
  "license": "MIT",
42
42
  "devDependencies": {
43
- "@types/d3-interpolate": "^3.0.4",
44
43
  "@types/mockjs": "^1.0.10",
45
44
  "@types/node": "^20.12.10",
46
45
  "@typescript-eslint/eslint-plugin": "^7.7.0",
@@ -43,7 +43,7 @@ export type CustomCell<T extends CustomCellProps<U> | CustomHeaderCellProps<U>,
43
43
  /** 表格列配置 */
44
44
  export type StkTableColumn<T extends Record<string, any>> = {
45
45
  /**
46
- * 用于区分相同dataIndex 的列。
46
+ * 列唯一键,(可选),不传则默认取dataIndex 字段作为列唯一键。
47
47
  */
48
48
  key?: any;
49
49
  /**
@@ -21,9 +21,13 @@ export function isEmptyValue(val: any, isNumber?: boolean) {
21
21
  * @param targetArray 表格数据
22
22
  * @return targetArray 的浅拷贝
23
23
  */
24
- export function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: T, targetArray: T[], sortConfig: SortConfig<T> = {}) {
24
+ export function insertToOrderedArray<T extends object>(
25
+ sortState: SortState<T>,
26
+ newItem: T,
27
+ targetArray: T[],
28
+ sortConfig: SortConfig<T> & { customCompare?: (a: T, b: T) => number } = {},
29
+ ) {
25
30
  const { dataIndex, sortField, order } = sortState;
26
- sortConfig = { emptyToBottom: false, ...sortConfig };
27
31
  let { sortType } = sortState;
28
32
  const field = sortField || dataIndex;
29
33
  if (!sortType) sortType = typeof newItem[field] as 'number' | 'string';
@@ -35,17 +39,24 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
35
39
  return data;
36
40
  }
37
41
 
42
+ const { emptyToBottom, customCompare, stringLocaleCompare } = { emptyToBottom: false, ...sortConfig };
43
+
38
44
  const targetVal: any = newItem[field];
39
- if (sortConfig.emptyToBottom && isEmptyValue(targetVal)) {
45
+ if (emptyToBottom && isEmptyValue(targetVal)) {
40
46
  // 空值排在最下方
41
47
  data.push(newItem);
42
48
  } else {
49
+ const customCompareFn =
50
+ customCompare ||
51
+ ((a, b) => {
52
+ const midVal: any = a[field];
53
+ const compareRes = strCompare(midVal, targetVal, isNumber, stringLocaleCompare);
54
+ return order === 'asc' ? compareRes : -compareRes;
55
+ });
43
56
  const isNumber = sortType === 'number';
44
57
  // 二分插入
45
58
  const sIndex = binarySearch(data, midIndex => {
46
- const midVal: any = data[midIndex][field];
47
- const compareRes = strCompare(midVal, targetVal, isNumber, sortConfig.stringLocaleCompare);
48
- return order === 'asc' ? compareRes : -compareRes;
59
+ return customCompareFn(data[midIndex], newItem);
49
60
  });
50
61
  data.splice(sIndex, 0, newItem);
51
62
  }