stk-table-vue 0.6.5 → 0.6.6

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.
package/README.md CHANGED
@@ -12,7 +12,7 @@ Vue2.7支持引入源码(**ts**)使用。
12
12
 
13
13
  ## License
14
14
  MIT
15
- repo:
15
+ repo(求 star🌟):
16
16
  - [Github](https://github.com/ja-plus/stk-table-vue)
17
17
  - [Gitee](https://gitee.com/japlus/stk-table-vue) 🇨🇳
18
18
 
@@ -26,6 +26,7 @@ declare function setSelectedCell(row?: DT, col?: StkTableColumn<DT>, option?: {
26
26
  * @param option.sort 是否触发排序-默认true
27
27
  * @param option.silent 是否禁止触发回调-默认true
28
28
  * @param option.force 是否触发排序-默认true
29
+ * @return 表格数据
29
30
  */
30
31
  declare function setSorter(colKey: string, order: Order, option?: {
31
32
  sortOption?: SortOption<DT>;
@@ -42,7 +43,10 @@ declare function resetSorter(): void;
42
43
  declare function scrollTo(top?: number | null, left?: number | null): void;
43
44
  /** get current table data */
44
45
  declare function getTableData(): any[];
45
- /** get current sort info */
46
+ /**
47
+ * get current sort info
48
+ * @return {{key:string,order:Order}[]}
49
+ */
46
50
  declare function getSortColumns(): {
47
51
  key: string | number | symbol | undefined;
48
52
  order: "desc" | "asc";
@@ -492,6 +496,7 @@ declare const _default: __VLS_WithTemplateSlots<import('vue').DefineComponent<{
492
496
  col: any;
493
497
  }): any;
494
498
  empty?(_: {}): any;
499
+ customBottom?(_: {}): any;
495
500
  }>;
496
501
  export default _default;
497
502
  type __VLS_WithTemplateSlots<T, S> = T & {
@@ -1,3 +1,3 @@
1
1
  export { default as StkTable } from './StkTable.vue';
2
- export { tableSort, insertToOrderedArray, strCompare } from './utils';
2
+ export { tableSort, insertToOrderedArray, strCompare, binarySearch } from './utils';
3
3
  export type { StkTableColumn } from './types/index';
@@ -14,6 +14,12 @@ export declare function isEmptyValue(val: any, isNumber?: boolean): boolean;
14
14
  * @return targetArray 的浅拷贝
15
15
  */
16
16
  export declare function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: T, targetArray: T[], sortConfig?: SortConfig<T>): T[];
17
+ /**
18
+ * 二分查找
19
+ * @param searchArray 查找数组
20
+ * @param compareCallback 比较函数,返回 -1|0|1
21
+ */
22
+ export declare function binarySearch(searchArray: any[], compareCallback: (midIndex: number) => number): number;
17
23
  /**
18
24
  * 字符串比较
19
25
  * @param a
@@ -21,7 +27,7 @@ export declare function insertToOrderedArray<T extends object>(sortState: SortSt
21
27
  * @param type 类型
22
28
  * @param isNumber 是否是数字类型
23
29
  * @param localeCompare 是否 使用Array.prototyshpe.localeCompare
24
- * @return {-1|0|1}
30
+ * @return {number} <0: a < b, 0: a = b, >0: a > b
25
31
  */
26
32
  export declare function strCompare(a: string, b: string, isNumber: boolean, localeCompare?: boolean): number;
27
33
  /**
@@ -59,7 +59,6 @@ function insertToOrderedArray(sortState, newItem, targetArray, sortConfig = {})
59
59
  sortConfig = { emptyToBottom: false, ...sortConfig };
60
60
  let { sortType } = sortState;
61
61
  if (!sortType) sortType = typeof newItem[dataIndex];
62
- const isNumber = sortType === "number";
63
62
  const data = [...targetArray];
64
63
  if (!order || !data.length) {
65
64
  data.unshift(newItem);
@@ -68,26 +67,32 @@ function insertToOrderedArray(sortState, newItem, targetArray, sortConfig = {})
68
67
  if (sortConfig.emptyToBottom && isEmptyValue(newItem)) {
69
68
  data.push(newItem);
70
69
  }
71
- let sIndex = 0;
72
- let eIndex = data.length - 1;
70
+ const isNumber = sortType === "number";
73
71
  const targetVal = newItem[dataIndex];
74
- while (sIndex <= eIndex) {
75
- const midIndex = Math.floor((sIndex + eIndex) / 2);
72
+ const sIndex = binarySearch(data, (midIndex) => {
76
73
  const midVal = data[midIndex][dataIndex];
77
74
  const compareRes = strCompare(midVal, targetVal, isNumber, sortConfig.stringLocaleCompare);
75
+ return order === "asc" ? compareRes : -compareRes;
76
+ });
77
+ data.splice(sIndex, 0, newItem);
78
+ return data;
79
+ }
80
+ function binarySearch(searchArray, compareCallback) {
81
+ let sIndex = 0;
82
+ let eIndex = searchArray.length - 1;
83
+ while (sIndex <= eIndex) {
84
+ const midIndex = Math.floor((sIndex + eIndex) / 2);
85
+ const compareRes = compareCallback(midIndex);
78
86
  if (compareRes === 0) {
79
87
  sIndex = midIndex;
80
88
  break;
81
- } else if (compareRes === -1) {
82
- if (order === "asc") sIndex = midIndex + 1;
83
- else eIndex = midIndex - 1;
89
+ } else if (compareRes < 0) {
90
+ sIndex = midIndex + 1;
84
91
  } else {
85
- if (order === "asc") eIndex = midIndex - 1;
86
- else sIndex = midIndex + 1;
92
+ eIndex = midIndex - 1;
87
93
  }
88
94
  }
89
- data.splice(sIndex, 0, newItem);
90
- return data;
95
+ return sIndex;
91
96
  }
92
97
  function strCompare(a, b, isNumber, localeCompare = false) {
93
98
  let _a = a;
@@ -2343,13 +2348,15 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
2343
2348
  renderSlot(_ctx.$slots, "empty", {}, () => [
2344
2349
  _cache[7] || (_cache[7] = createTextVNode("暂无数据"))
2345
2350
  ])
2346
- ], 2)) : createCommentVNode("", true)
2351
+ ], 2)) : createCommentVNode("", true),
2352
+ renderSlot(_ctx.$slots, "customBottom")
2347
2353
  ], 38);
2348
2354
  };
2349
2355
  }
2350
2356
  });
2351
2357
  export {
2352
2358
  _sfc_main as StkTable,
2359
+ binarySearch,
2353
2360
  insertToOrderedArray,
2354
2361
  strCompare,
2355
2362
  tableSort
package/lib/style.css CHANGED
@@ -260,7 +260,7 @@
260
260
  .stk-table .expand-cell{
261
261
  cursor:pointer;
262
262
  }
263
- .stk-table .expand-cell .table-cell-wrapper.expanded-cell-wrapper::before{
263
+ .stk-table .expand-cell .expanded-cell-wrapper::before{
264
264
  content:'';
265
265
  display:inline-block;
266
266
  margin:0 2px;
@@ -271,7 +271,7 @@
271
271
  border-bottom:4px solid transparent;
272
272
  transition:transform 0.2s ease;
273
273
  }
274
- .stk-table .expand-cell .table-cell-wrapper.expanded-cell-wrapper > span{
274
+ .stk-table .expand-cell .expanded-cell-wrapper > span{
275
275
  margin-left:var(--cell-padding-x);
276
276
  }
277
277
  .stk-table .expand-cell.expanded .table-cell-wrapper::before{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stk-table-vue",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
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",
@@ -227,6 +227,7 @@
227
227
  <div v-if="(!dataSourceCopy || !dataSourceCopy.length) && showNoData" class="stk-table-no-data" :class="{ 'no-data-full': noDataFull }">
228
228
  <slot name="empty">暂无数据</slot>
229
229
  </div>
230
+ <slot name="customBottom"></slot>
230
231
  </div>
231
232
  </template>
232
233
 
@@ -1288,6 +1289,7 @@ function setSelectedCell(row?: DT, col?: StkTableColumn<DT>, option = { silent:
1288
1289
  * @param option.sort 是否触发排序-默认true
1289
1290
  * @param option.silent 是否禁止触发回调-默认true
1290
1291
  * @param option.force 是否触发排序-默认true
1292
+ * @return 表格数据
1291
1293
  */
1292
1294
  function setSorter(colKey: string, order: Order, option: { sortOption?: SortOption<DT>; force?: boolean; silent?: boolean; sort?: boolean } = {}) {
1293
1295
  const newOption = { silent: true, sortOption: null, sort: true, ...option };
@@ -1325,7 +1327,10 @@ function getTableData() {
1325
1327
  return toRaw(dataSourceCopy.value);
1326
1328
  }
1327
1329
 
1328
- /** get current sort info */
1330
+ /**
1331
+ * get current sort info
1332
+ * @return {{key:string,order:Order}[]}
1333
+ */
1329
1334
  function getSortColumns() {
1330
1335
  const sortOrder = sortSwitchOrder[sortOrderIndex.value];
1331
1336
  if (!sortOrder) return [];
@@ -1,4 +1,4 @@
1
1
  export { default as StkTable } from './StkTable.vue';
2
- export { tableSort, insertToOrderedArray, strCompare } from './utils';
2
+ export { tableSort, insertToOrderedArray, strCompare, binarySearch } from './utils';
3
3
  export type { StkTableColumn } from './types/index';
4
4
  import './style.less';
@@ -386,8 +386,7 @@
386
386
  .expand-cell {
387
387
  cursor: pointer;
388
388
 
389
- .table-cell-wrapper {
390
- &.expanded-cell-wrapper {
389
+ .expanded-cell-wrapper {
391
390
  &::before {
392
391
  content: '';
393
392
  display: inline-block;
@@ -404,7 +403,6 @@
404
403
  margin-left: var(--cell-padding-x)
405
404
  }
406
405
  }
407
- }
408
406
 
409
407
  &.expanded {
410
408
  .table-cell-wrapper::before {
@@ -26,7 +26,6 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
26
26
  sortConfig = { emptyToBottom: false, ...sortConfig };
27
27
  let { sortType } = sortState;
28
28
  if (!sortType) sortType = typeof newItem[dataIndex] as 'number' | 'string';
29
- const isNumber = sortType === 'number';
30
29
  const data = [...targetArray];
31
30
 
32
31
  if (!order || !data.length) {
@@ -40,31 +39,43 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
40
39
  data.push(newItem);
41
40
  }
42
41
 
42
+ const isNumber = sortType === 'number';
43
+
43
44
  // 二分插入
44
- let sIndex = 0;
45
- let eIndex = data.length - 1;
46
45
  const targetVal: any = newItem[dataIndex];
47
- while (sIndex <= eIndex) {
48
- // console.log(sIndex, eIndex);
49
- const midIndex = Math.floor((sIndex + eIndex) / 2);
46
+ const sIndex = binarySearch(data, midIndex => {
50
47
  const midVal: any = data[midIndex][dataIndex];
51
48
  const compareRes = strCompare(midVal, targetVal, isNumber, sortConfig.stringLocaleCompare);
49
+ return order === 'asc' ? compareRes : -compareRes;
50
+ });
51
+ data.splice(sIndex, 0, newItem);
52
+ return data;
53
+ }
54
+
55
+ /**
56
+ * 二分查找
57
+ * @param searchArray 查找数组
58
+ * @param compareCallback 比较函数,返回 -1|0|1
59
+ */
60
+ export function binarySearch(searchArray: any[], compareCallback: (midIndex: number) => number) {
61
+ let sIndex = 0;
62
+ let eIndex = searchArray.length - 1;
63
+ while (sIndex <= eIndex) {
64
+ const midIndex = Math.floor((sIndex + eIndex) / 2);
65
+ const compareRes = compareCallback(midIndex);
52
66
  if (compareRes === 0) {
53
67
  //midVal == targetVal
54
68
  sIndex = midIndex;
55
69
  break;
56
- } else if (compareRes === -1) {
70
+ } else if (compareRes < 0) {
57
71
  // midVal < targetVal
58
- if (order === 'asc') sIndex = midIndex + 1;
59
- else eIndex = midIndex - 1;
72
+ sIndex = midIndex + 1;
60
73
  } else {
61
74
  //midVal > targetVal
62
- if (order === 'asc') eIndex = midIndex - 1;
63
- else sIndex = midIndex + 1;
75
+ eIndex = midIndex - 1;
64
76
  }
65
77
  }
66
- data.splice(sIndex, 0, newItem);
67
- return data;
78
+ return sIndex;
68
79
  }
69
80
  /**
70
81
  * 字符串比较
@@ -73,7 +84,7 @@ export function insertToOrderedArray<T extends object>(sortState: SortState<T>,
73
84
  * @param type 类型
74
85
  * @param isNumber 是否是数字类型
75
86
  * @param localeCompare 是否 使用Array.prototyshpe.localeCompare
76
- * @return {-1|0|1}
87
+ * @return {number} <0: a < b, 0: a = b, >0: a > b
77
88
  */
78
89
  export function strCompare(a: string, b: string, isNumber: boolean, localeCompare = false): number {
79
90
  let _a: number | string = a;