Skip to content

Commit fdad1ad

Browse files
committed
修改 formily 包中的大数据 dome
1 parent cf98330 commit fdad1ad

5 files changed

Lines changed: 140 additions & 103 deletions

File tree

packages/docs/.vitepress/theme/react-demo.css

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
transition: all 0.15s;
8282
}
8383

84-
.react-demo-copy:hover {
85-
background: rgba(128, 128, 128, 0.2);
86-
color: var(--vp-c-text-1);
84+
.react-demo-preview table {
85+
overflow: visible;
8786
}

packages/docs/demos/formily/LargeDataDemo.tsx

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
# 大数据量性能
22

33
<script setup>
4-
import LargeDataDemo from '../demos/formily/LargeDataDemo.tsx'
5-
import LargeDataDemoSource from '../demos/formily/LargeDataDemo.tsx?raw'
4+
import VirtualScrollDemo from '../demos/formily/VirtualScrollDemo.tsx'
5+
import VirtualScrollDemoSource from '../demos/formily/VirtualScrollDemo.tsx?raw'
66
</script>
77

88
FormilyEditableTable 基于行级响应式更新,配合 antd Table 分页,使得 500+ 行数据也能保持良好性能。
99

10-
## 交互式示例
11-
1210
<ClientOnly>
13-
<ReactDemo :component="LargeDataDemo" :source="LargeDataDemoSource" title="500 行数据" description="500 行数据,antd Table 分页,每页 20 行" />
11+
<ReactDemo :component="VirtualScrollDemo" :source="VirtualScrollDemoSource" title="500 行数据 + 固定列" description="500 行数据,分页,固定序号列和操作列,横向滚动" />
1412
</ClientOnly>
1513

14+
```tsx
15+
columns={[
16+
{ title: '序号', width: 60, fixed: 'left', render: ({ index }) => index + 1 },
17+
// ... 中间列
18+
{ title: '操作', width: 80, fixed: 'right', render: ({ index, field }) => ... },
19+
]}
20+
tableProps={{ scroll: { x: 'max-content' } }}
21+
```
22+
23+
> **注意**:固定列必须配合 `scroll.x: 'max-content'` 使用,所有列都应设置明确的 `width`
24+
1625
## 性能优化要点
1726

1827
1. **行级响应式更新**:仅在行数增减时重渲染表格,单元格编辑不会触发整表重渲染
1928
2. **分页**:大数据量场景建议开启分页,默认每页 10 行
20-
21-
```tsx
22-
<FormilyEditableTable
23-
name="items"
24-
pagination={{ pageSize: 20 }}
25-
...
26-
/>
27-
```
29+
3. **固定列**:所有列设置明确 `width`

packages/fast-editable-table/src/FormilyEditableTable.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ const FormilyEditableTableInner: React.FC<Omit<IFormilyEditableTableProps, 'name
111111
const fieldIndex = (current - 1) * pageSize + index;
112112
if (fieldIndex >= valueLenRef.current) return null;
113113

114-
const content = col.render ? (
115-
col.render({ index: fieldIndex, field } as IColumnRenderOpt)
116-
) : (
117-
<DefaultOperator index={fieldIndex} field={field} disabled={disableRemove} />
118-
);
114+
if (!col.render) {
115+
return <DefaultOperator index={fieldIndex} field={field} disabled={disableRemove} />;
116+
}
117+
118+
const content = col.render?.({ index: fieldIndex, field } as IColumnRenderOpt);
119119

120-
// 行级 Field 上下文:使 <FormilyEditableTableField name="type"> 解析为 items.{fieldIndex}.type
121120
return <Field name={String(fieldIndex)}>{content}</Field>;
122121
},
123122
}));

0 commit comments

Comments
 (0)