Skip to content

Commit 843234d

Browse files
authored
refactor(Table): 解决初始排序错误问题&优化排序代码 (#1242)
1 parent e7a90f5 commit 843234d

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

packages/devui-vue/devui/table/src/components/header-th/use-header-th.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref, Ref, computed, getCurrentInstance, inject, onMounted } from 'vue';
1+
import { ref, Ref, computed, getCurrentInstance, inject, onMounted, nextTick } from 'vue';
22
import { Column, FilterConfig, SortDirection } from '../column/column-types';
33
import { TABLE_TOKEN, ITableInstanceAndDefaultRow } from '../../table-types';
44
import { UseSort, UseFilter, UseBaseRender, UseDragColumnWidth } from './header-th-types';
@@ -15,13 +15,14 @@ export function useBaseRender(column: Ref<Column>): UseBaseRender {
1515
export function useSort(column: Ref<Column>): UseSort {
1616
const table = inject(TABLE_TOKEN) as ITableInstanceAndDefaultRow;
1717
const store = table.store;
18-
const direction = ref<SortDirection | undefined>(column.value.sortDirection);
18+
const direction = ref<SortDirection>(column.value.sortDirection || '');
1919
const sortClass = computed(() => ({
2020
'sort-active': Boolean(direction.value),
2121
}));
2222
const thInstance = getCurrentInstance();
23-
thInstance && store.states.thList.push(thInstance);
24-
onMounted(() => {
23+
thInstance && store.collectTh(thInstance);
24+
onMounted(async () => {
25+
await nextTick();
2526
column.value.sortable && column.value.sortDirection && store.sortData?.(direction.value, column.value.sortMethod);
2627
});
2728
const execClearSortOrder = () => {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { ComponentInternalInstance, Ref } from 'vue';
2+
import { SortDirection, SortMethod } from '../components/column/column-types';
3+
import { UseSort } from './use-table-types';
4+
5+
export function useSort<T extends Record<string, unknown>>(dataSource: Ref<T[]>, _data: Ref<T[]>): UseSort<T> {
6+
const thList: ComponentInternalInstance[] = [];
7+
8+
const sortData = (direction: SortDirection, sortMethod: SortMethod<T> | undefined) => {
9+
if (direction === 'ASC') {
10+
_data.value = _data.value.sort((a, b) => (sortMethod ? (sortMethod(a, b) ? 1 : -1) : 0));
11+
} else if (direction === 'DESC') {
12+
_data.value = _data.value.sort((a, b) => (sortMethod ? (sortMethod(a, b) ? -1 : 1) : 0));
13+
} else {
14+
_data.value = [...dataSource.value];
15+
}
16+
};
17+
18+
const collectTh = (th: ComponentInternalInstance) => {
19+
thList.push(th);
20+
};
21+
22+
return { thList, collectTh, sortData };
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import type { ComponentInternalInstance } from 'vue';
2+
import type { SortDirection, SortMethod } from '../components/column/column-types';
3+
14
export interface UseHorizontalScroll {
25
onTableScroll: (e: Event) => void;
36
}
7+
8+
export interface UseSort<T> {
9+
thList: ComponentInternalInstance[];
10+
collectTh: (th: ComponentInternalInstance) => void;
11+
sortData: (direction: SortDirection, sortMethod: SortMethod<T> | undefined) => void;
12+
}

packages/devui-vue/devui/table/src/store/index.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { isBoolean } from '../../../shared/utils';
2-
import { watch, Ref, ref, computed, unref, ComponentInternalInstance } from 'vue';
3-
import type { Column, SortMethod, SortDirection, LevelColumn } from '../components/column/column-types';
2+
import { watch, Ref, ref, computed, unref } from 'vue';
3+
import type { Column, LevelColumn } from '../components/column/column-types';
44
import type { DefaultRow, ITable, RowKeyType } from '../table-types';
55
import type { TableStore } from './store-types';
66
import { useExpand } from './use-expand';
77
import { useEditTableCell } from './use-edit-table-cell';
88
import { getRowIdentity } from '../utils';
9+
import { useSort } from '../composables/use-sort';
910

1011
function replaceColumn(array: LevelColumn[], column: LevelColumn) {
1112
return array.map((item) => {
@@ -96,15 +97,7 @@ function doFlattenRows<T extends Record<string, unknown>>(
9697
if ((data as Record<string, unknown>).children) {
9798
rowLevelMap.value[getRowIdentity(data as Record<string, unknown>, rowKey)] = level;
9899
// eslint-disable-next-line prefer-spread
99-
result.push.apply(
100-
result,
101-
doFlattenRows<T>(
102-
data.children as T[],
103-
level + 1,
104-
rowKey,
105-
rowLevelMap,
106-
hiddenRowKeys
107-
));
100+
result.push.apply(result, doFlattenRows<T>(data.children as T[], level + 1, rowKey, rowLevelMap, hiddenRowKeys));
108101
}
109102
});
110103
return result;
@@ -232,21 +225,6 @@ function createSelection<T extends Record<string, unknown>>(dataSource: Ref<T[]>
232225
};
233226
}
234227

235-
function createSorter<T extends Record<string, unknown>>(dataSource: Ref<T[]>, _data: Ref<T[]>) {
236-
const sortData = (direction: SortDirection, sortMethod: SortMethod<T>) => {
237-
if (direction === 'ASC') {
238-
_data.value = _data.value.sort((a, b) => (sortMethod ? (sortMethod(a, b) ? 1 : -1) : 0));
239-
} else if (direction === 'DESC') {
240-
_data.value = _data.value.sort((a, b) => (sortMethod ? (sortMethod(a, b) ? -1 : 1) : 0));
241-
} else {
242-
_data.value = [...dataSource.value];
243-
}
244-
};
245-
246-
const thList: ComponentInternalInstance[] = [];
247-
return { sortData, thList };
248-
}
249-
250228
function createFixedLogic(columns: Ref<Column[]>) {
251229
const isFixedLeft = computed(() => {
252230
return columns.value.reduce((prev, current) => prev || !!current.fixedLeft, false);
@@ -276,7 +254,7 @@ export function createStore<T extends Record<string, unknown>>(dataSource: Ref<T
276254
flatRows
277255
);
278256

279-
const { sortData, thList } = createSorter<T>(dataSource, flatRows);
257+
const { thList, collectTh, sortData } = useSort(dataSource, flatRows);
280258

281259
const { isFixedLeft } = createFixedLogic(_columns);
282260
const { isRowExpanded, updateExpandRows, setExpandRows, toggleRowExpansion } = useExpand(_data, table);
@@ -315,6 +293,7 @@ export function createStore<T extends Record<string, unknown>>(dataSource: Ref<T
315293
updateColumns,
316294
updateRows,
317295
getCheckedRows,
296+
collectTh,
318297
sortData,
319298
isRowChecked,
320299
checkRow,

packages/devui-vue/devui/table/src/store/store-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export interface TableStore<T = Record<string, unknown>> {
4848
updateRows(): void;
4949
// 获取勾选行
5050
getCheckedRows(): T[];
51+
collectTh(th: ComponentInternalInstance): void;
5152
// 排序数据
52-
sortData(direction: SortDirection, sortMethod: SortMethod<T>): void;
53+
sortData(direction: SortDirection, sortMethod: SortMethod<T> | undefined): void;
5354
// 特定行数据是否勾选
5455
isRowChecked(row: T, index: number): boolean;
5556
// 保存勾选行的信息

0 commit comments

Comments
 (0)