-
Notifications
You must be signed in to change notification settings - Fork 803
/
columns.tsx
175 lines (170 loc) · 4.55 KB
/
columns.tsx
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { debounce } from 'lodash-es';
import { Tag, Spin } from 'ant-design-vue';
import type { TableColumn } from '@/components/core/dynamic-table';
import { waitTime } from '@/utils/common';
import { useDictStore } from '@/store/modules/dict';
const names = ['王路飞', '王大蛇', '李白', '刺客伍六七'];
const { fetchDict, dictPending, showDictLabel } = useDictStore();
export const fetchStatusMapData = async (keyword = '') => {
const [data] = await fetchDict('sell_status');
return waitTime<LabelValueOptions>(
100,
data.filter((n) => n.label.includes(keyword)),
);
};
export const getClothesByGender = (gender: number) => {
if (gender === 1) {
// 男
return [
{
label: '西装',
value: 1,
},
{
label: '领带',
value: 0,
},
];
} else if (gender === 0) {
//女
return [
{
label: '裙子',
value: 1,
},
{
label: '包包',
value: 0,
},
];
}
return [];
};
export const tableData = Array.from({ length: 30 }).map((_, i) => {
const gender = ~~(Math.random() * 2);
return {
id: i + 1,
date: new Date().toLocaleString(),
name: names[~~(Math.random() * 4)],
clothes: getClothesByGender(gender)[~~(Math.random() * 2)].label,
price: ~~(Math.random() * 1000),
gender,
img: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
status: ~~(Math.random() * 2),
nested: {
prop: `嵌套属性 ${i + 1}`,
},
};
});
// 数据项类型
export type ListItemType = (typeof tableData)[number];
// 使用TableColumn<ListItemType> 将会限制dataIndex的类型,但换来的是dataIndex有类型提示
export const columns: TableColumn<ListItemType>[] = [
{
title: '姓名',
dataIndex: 'name',
sorter: true,
width: 300,
resizable: true,
formItemProps: {
defaultValue: '李白',
required: true,
},
},
{
title: '性别',
dataIndex: 'gender',
width: 120,
resizable: true,
formItemProps: {
component: 'Select',
componentProps: ({ formInstance, formModel }) => ({
request: async () => {
const [data] = await fetchDict('gender');
return data;
},
onChange() {
// 根据当前选择的性别,更新衣服可选项
formInstance?.updateSchema({
field: 'clothes',
componentProps: {
options: getClothesByGender(formModel.gender),
},
});
formModel['clothes'] = '';
// console.log('formInstance', formInstance);
},
}),
},
// customRender: ({ record }) => ['女', '男'][record.gender],
customRender: ({ record }) => (
<Spin spinning={dictPending['sell_status']} size="small">
{showDictLabel('gender', record.gender)}
</Spin>
),
},
{
title: '衣服',
dataIndex: 'clothes',
formItemProps: {
component: 'Select',
componentProps: ({ tableInstance }) => ({
onChange() {
tableInstance?.reload();
},
}),
},
},
{
title: '价格',
dataIndex: 'price',
formItemProps: {
component: 'InputNumber',
},
customRender: ({ record }) => `${record.price}元`,
},
{
title: '嵌套属性',
dataIndex: ['nested', 'prop'],
},
{
title: '状态',
dataIndex: 'status',
formItemProps: {
component: 'Select',
componentProps: ({ formInstance, schema }) => ({
showSearch: true,
filterOption: false,
request: () => {
return fetchStatusMapData();
},
onSearch: debounce(async (keyword) => {
schema.value.loading = true;
const newSchema = {
field: 'status',
componentProps: {
options: [] as LabelValueOptions,
},
};
formInstance?.updateSchema([newSchema]);
console.log('onSearch keyword', keyword);
const result = await fetchStatusMapData(keyword).finally(
() => (schema.value.loading = false),
);
newSchema.componentProps.options = result;
formInstance?.updateSchema([newSchema]);
}, 500),
onChange(value: string) {
console.log('onChange', value);
},
}),
},
customRender: ({ record }) => (
<Spin spinning={dictPending['sell_status']} size="small">
<Tag color={record.status == 1 ? 'red' : 'default'}>
{showDictLabel('sell_status', record.status)}
</Tag>
</Spin>
),
},
];