-
Notifications
You must be signed in to change notification settings - Fork 807
/
columns.tsx
182 lines (178 loc) · 5.22 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
176
177
178
179
180
181
182
import { debounce } from 'lodash-es';
import { PlusOutlined } from '@ant-design/icons-vue';
import { Tag, Image } from 'ant-design-vue';
import type { TableColumn } from '@/components/core/dynamic-table';
import {
fetchStatusMapData,
getClothesByGender,
tableData,
} from '@/views/demos/tables/search-table/columns';
export { tableData };
// 数据项类型
export type ListItemType = (typeof tableData)[number];
// 使用TableColumn<ListItemType> 将会限制dataIndex的类型,但换来的是dataIndex有类型提示
export const columns: TableColumn<ListItemType>[] = [
{
title: '姓名',
dataIndex: 'name',
sorter: true,
/** 默认开启编辑, 仅`editableType`为`cell`时有效 */
defaultEditable: true,
editable: ({ index }) => {
// 第一行不允许被编辑
return index > 0;
},
formItemProps: {
defaultValue: '李白',
rules: [{ required: true, message: '请输入姓名' }],
},
editFormItemProps: {
/** 不继承于 `formItemProps`的属性 */
extendSearchFormProps: false,
rules: [{ required: true, message: '请输入姓名' }],
},
},
{
title: '性别',
dataIndex: 'gender',
/** 搜索表单配置 */
formItemProps: {
component: 'Select',
componentProps: ({ formInstance, formModel }) => ({
options: [
{
label: '男',
value: 1,
},
{
label: '女',
value: 0,
},
],
onChange() {
console.log('formModel', formModel);
// 根据当前选择的性别,更新衣服可选项
formInstance?.updateSchema({
field: `clothes`,
componentProps: {
options: getClothesByGender(formModel.gender),
},
});
formModel['clothes'] = '';
},
}),
},
/** 可编辑行表单配置 */
editFormItemProps: {
/** 继承 formItemProps 的属性配置, 默认就是是true,这里为了演示有这个字段开关,所以特别写上 */
extendSearchFormProps: true,
rules: [{ required: true, type: 'number', message: '请选择性别' }],
componentProps: ({ formInstance, formModel, tableRowKey }) => ({
onChange() {
// 根据当前选择的性别,更新衣服可选项
formInstance?.updateSchema({
field: `${tableRowKey}.clothes`,
componentProps: {
options: getClothesByGender(formModel.gender),
},
});
formModel['clothes'] = '';
},
}),
},
customRender: ({ record }) => ['女', '男'][record.gender],
},
{
title: '衣服',
dataIndex: 'clothes',
formItemProps: {
component: 'Select',
componentProps: ({ formModel }) => ({
options: getClothesByGender(formModel.gender),
}),
},
},
{
title: '图片',
dataIndex: 'img',
hideInSearch: true,
formItemProps: {
component: 'Upload',
defaultValue: [
{
uid: '-1',
name: 'image.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
],
componentProps: {
maxCount: 1,
listType: 'picture-card',
action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
},
componentSlots: ({ formModel }) => ({
default: () =>
formModel['img']?.length ? (
''
) : (
<div>
<PlusOutlined />
<div class="mt-8px">Upload</div>
</div>
),
}),
},
customRender: ({ record }) => <Image width={100} src={record.img}></Image>,
},
{
title: '价格',
dataIndex: 'price',
editFormItemProps: {
component: 'InputNumber',
rules: [{ required: true, message: '请输入价格!' }],
},
customRender: ({ record }) => `${record.price}元`,
},
{
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: schema.value.field,
componentProps: {
options: [] as LabelValueOptions,
},
};
formInstance?.updateSchema([newSchema]);
const result = await fetchStatusMapData(keyword).finally(
() => (schema.value.loading = false),
);
console.log('onSearch keyword', keyword, formInstance, newSchema);
newSchema.componentProps.options = result;
formInstance?.updateSchema([newSchema]);
}, 500),
onChange(value: string) {
console.log('onChange', value);
},
}),
},
editFormItemProps: {
rules: [{ required: true, type: 'number', message: '请选择状态' }],
},
customRender: ({ record }) => (
<Tag color={record.status == 1 ? 'red' : 'default'}>
{['已售罄', '热卖中'][record.status]}
</Tag>
),
},
];