forked from Tencent/tdesign-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced-table.tsx
More file actions
121 lines (108 loc) · 3.75 KB
/
enhanced-table.tsx
File metadata and controls
121 lines (108 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
defineComponent, SetupContext, computed, ref,
} from '@vue/composition-api';
import baseTableProps from './base-table-props';
import primaryTableProps from './primary-table-props';
import enhancedTableProps from './enhanced-table-props';
import PrimaryTable, { BASE_TABLE_ALL_EVENTS } from './primary-table';
import {
TdEnhancedTableProps, PrimaryTableCol, TableRowData, DragSortContext,
} from './type';
import useTreeData from './hooks/useTreeData';
import useTreeSelect from './hooks/useTreeSelect';
import { TableListeners } from './base-table';
const PRIMARY_B_EVENTS = [
'change',
'page-change',
'expand-change',
'filter-change',
'sort-change',
'data-change',
'async-loading-click',
];
const PRIMARY_ALL_EVENTS = BASE_TABLE_ALL_EVENTS.concat(PRIMARY_B_EVENTS);
export default defineComponent({
name: 'TEnhancedTable',
props: {
...baseTableProps,
...primaryTableProps,
...enhancedTableProps,
},
setup(props: TdEnhancedTableProps, context: SetupContext) {
const {
store, dataSource, formatTreeColumn, swapData, ...treeInstanceFunctions
} = useTreeData(props, context);
const treeDataMap = ref(store.value.treeDataMap);
const { onInnerSelectChange } = useTreeSelect(props, treeDataMap);
// 影响列和单元格内容的因素有:树形节点需要添加操作符 [+] [-]
const getColumns = (columns: PrimaryTableCol<TableRowData>[]) => {
const arr: PrimaryTableCol<TableRowData>[] = [];
for (let i = 0, len = columns.length; i < len; i++) {
let item = { ...columns[i] };
item = formatTreeColumn(item);
if (item.children?.length) {
item.children = getColumns(item.children);
}
// 多级表头和自定义列配置特殊逻辑:要么子节点不存在,要么子节点长度大于 1,方便做自定义列配置
if (!item.children || item.children?.length) {
arr.push(item);
}
}
return arr;
};
const tColumns = computed(() => {
// 暂时只有树形结构需要处理 column.cell
const isTreeData = !props.tree || !Object.keys(props.tree).length;
return isTreeData ? props.columns : getColumns(props.columns);
});
const onDragSortChange = (params: DragSortContext<TableRowData>) => {
if (props.beforeDragSort && !props.beforeDragSort(params)) return;
swapData({
current: params.current,
target: params.target,
currentIndex: params.currentIndex,
targetIndex: params.targetIndex,
});
props.onDragSort?.(params);
// Vue3 do not need next line
context.emit('drag-sort', params);
};
return {
store,
dataSource,
tColumns,
onDragSortChange,
onInnerSelectChange,
...treeInstanceFunctions,
};
},
methods: {
// support @row-click @page-change @row-hover .etc. events, Vue3 do not need this function
getListener() {
const listeners: TableListeners = {};
PRIMARY_ALL_EVENTS.forEach((key) => {
listeners[key] = (...args: any) => {
this.$emit(key, ...args);
};
});
return listeners;
},
},
render() {
const props = {
...this.$props,
data: this.dataSource,
columns: this.tColumns,
// 树形结构不允许本地数据分页
disableDataPage: Boolean(this.tree && Object.keys(this.tree).length),
};
// 事件,Vue3 do not need this.getListener
const on: TableListeners = {
...this.getListener(),
'select-change': this.onInnerSelectChange,
'drag-sort': this.onDragSortChange,
};
// replace `scopedSlots={this.$scopedSlots}` of `v-slots={this.$slots}` in Vue3
return <PrimaryTable scopedSlots={this.$scopedSlots} props={props} on={on} {...this.$attrs} />;
},
});