|
| 1 | +import type { IColumn } from '@react-editable-tables/formily'; |
| 2 | +import { createForm, FormilyEditableTable, FormProvider } from '@react-editable-tables/formily'; |
| 3 | +import { Button, Input, InputNumber, Select } from 'antd'; |
| 4 | + |
| 5 | +const departments = ['技术部', '产品部', '设计部', '市场部', '运营部']; |
| 6 | +const deptOptions = departments.map((d) => ({ label: d, value: d })); |
| 7 | + |
| 8 | +function generateItems(count: number) { |
| 9 | + const names = ['张', '李', '王', '赵', '刘', '陈', '杨', '黄', '周', '吴']; |
| 10 | + return Array.from({ length: count }, (_, i) => ({ |
| 11 | + name: `${names[i % names.length]}${String.fromCharCode(65 + (i % 26))}`, |
| 12 | + age: 22 + (i % 30), |
| 13 | + department: departments[i % departments.length], |
| 14 | + salary: 8000 + (i % 10) * 1000, |
| 15 | + remark: '', |
| 16 | + })); |
| 17 | +} |
| 18 | + |
| 19 | +const form = createForm({ |
| 20 | + initialValues: { items: generateItems(500) }, |
| 21 | +}); |
| 22 | + |
| 23 | +const columns: IColumn[] = [ |
| 24 | + { |
| 25 | + title: '序号', |
| 26 | + key: '__index', |
| 27 | + dataIndex: '__index', |
| 28 | + width: 60, |
| 29 | + fixed: 'left', |
| 30 | + render: ({ index }) => <span style={{ color: '#999' }}>{index + 1}</span>, |
| 31 | + }, |
| 32 | + { |
| 33 | + title: '姓名', |
| 34 | + width: 120, |
| 35 | + render: () => ( |
| 36 | + <FormilyEditableTable.Field name="name" required parse={(e: any) => e?.target?.value ?? e}> |
| 37 | + <Input /> |
| 38 | + </FormilyEditableTable.Field> |
| 39 | + ), |
| 40 | + }, |
| 41 | + { |
| 42 | + title: '年龄', |
| 43 | + width: 100, |
| 44 | + render: () => ( |
| 45 | + <FormilyEditableTable.Field name="age"> |
| 46 | + <InputNumber style={{ width: '100%' }} min={1} max={100} /> |
| 47 | + </FormilyEditableTable.Field> |
| 48 | + ), |
| 49 | + }, |
| 50 | + { |
| 51 | + title: '部门', |
| 52 | + width: 130, |
| 53 | + render: () => ( |
| 54 | + <FormilyEditableTable.Field name="department"> |
| 55 | + <Select options={deptOptions} style={{ width: '100%' }} /> |
| 56 | + </FormilyEditableTable.Field> |
| 57 | + ), |
| 58 | + }, |
| 59 | + { |
| 60 | + title: '薪资', |
| 61 | + width: 130, |
| 62 | + render: () => ( |
| 63 | + <FormilyEditableTable.Field name="salary"> |
| 64 | + <InputNumber style={{ width: '100%' }} min={0} step={1000} /> |
| 65 | + </FormilyEditableTable.Field> |
| 66 | + ), |
| 67 | + }, |
| 68 | + { |
| 69 | + title: '备注', |
| 70 | + width: 120, |
| 71 | + render: () => ( |
| 72 | + <FormilyEditableTable.Field name="remark" parse={(e: any) => e?.target?.value ?? e}> |
| 73 | + <Input /> |
| 74 | + </FormilyEditableTable.Field> |
| 75 | + ), |
| 76 | + }, |
| 77 | + { |
| 78 | + title: '操作', |
| 79 | + key: '__ops', |
| 80 | + dataIndex: '__ops', |
| 81 | + width: 80, |
| 82 | + fixed: 'right', |
| 83 | + render: ({ index, field }) => ( |
| 84 | + <Button type="link" danger onClick={() => field.remove(index)}> |
| 85 | + 删除 |
| 86 | + </Button> |
| 87 | + ), |
| 88 | + }, |
| 89 | +]; |
| 90 | + |
| 91 | +export default function VirtualScrollDemo() { |
| 92 | + const handleSubmit = async () => { |
| 93 | + try { |
| 94 | + await form.validate(); |
| 95 | + console.log('提交数据:', form.values); |
| 96 | + } catch { } |
| 97 | + }; |
| 98 | + |
| 99 | + return ( |
| 100 | + <FormProvider form={form}> |
| 101 | + <FormilyEditableTable |
| 102 | + name="items" |
| 103 | + columns={columns} |
| 104 | + addText="添加行" |
| 105 | + itemDefaultValue={{ name: '', age: 25, department: undefined, salary: 8000, remark: '' }} |
| 106 | + min={1} |
| 107 | + pagination={{ pageSize: 50, showSizeChanger: true, pageSizeOptions: [20, 50, 100] }} |
| 108 | + tableProps={{ |
| 109 | + bordered: true, |
| 110 | + scroll: { x: 'max-content' }, |
| 111 | + }} |
| 112 | + /> |
| 113 | + <Button type="primary" onClick={handleSubmit} style={{ marginTop: 16 }}> |
| 114 | + 提交 |
| 115 | + </Button> |
| 116 | + </FormProvider> |
| 117 | + ); |
| 118 | +} |
0 commit comments