@kne/table-page 0.1.14 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +524 -7
- package/dist/index.css +108 -8
- package/dist/index.css.map +1 -1
- package/dist/index.js +627 -26
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +558 -38
- package/dist/index.modern.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -33,6 +33,7 @@ npm i --save @kne/table-page
|
|
|
33
33
|
- **Tab(tab)**:顶部分类切换,默认「全部」,选中值写入 filter value 参与请求但不在已选标签中重复展示;桌面端在表格边框外侧,移动端显示在 SearchInput 下方;可通过 `tabProps` 透传 antd Tabs 属性
|
|
34
34
|
- **批量操作(batchActions)**:配合 `rowSelection` 和 `useSelectedRow`,提供下拉菜单形式的批量操作(如批量导出、批量通知),未选中时自动禁用
|
|
35
35
|
- **PC 卡片视图(renderCard)**:取值同 `renderMobile`(true / function / preset 字符串),生效后 PC 端可切换表格/卡片(状态按 `name` 存 localStorage);`forceCard` 强制卡片并不显示切换按钮;卡片模式下外框透明、默认触底下拉加载;移动端忽略
|
|
36
|
+
- **树形数据**:透传 `dataType`(`tree` / `treeList`)、`expandedKeys`、`onLoadChildren`、`rowSelection.checkRelation` 等给内部 Table / TableView
|
|
36
37
|
- **已选筛选值展示**:工具栏下方展示当前生效的筛选条件标签,支持快速清除
|
|
37
38
|
|
|
38
39
|
#### Table
|
|
@@ -44,6 +45,7 @@ npm i --save @kne/table-page
|
|
|
44
45
|
- **配置持久化**:设置 `name` 后,列宽和显示状态自动保存到 localStorage
|
|
45
46
|
- **分组表头**:通过 `groupHeader` 配置实现多级表头结构
|
|
46
47
|
- **浮动横向滚动条**:当表格宽度超出容器时,底部自动显示横向滚动条(通过 `horizontalScroller` 控制)
|
|
48
|
+
- **树形数据**:支持与 `TableView` 相同的 `dataType`(`tree` / `treeList`),映射为 antd Table 树形展开,含懒加载与 `checkRelation` 勾选关联
|
|
47
49
|
|
|
48
50
|
#### TableView
|
|
49
51
|
|
|
@@ -54,6 +56,7 @@ npm i --save @kne/table-page
|
|
|
54
56
|
- 行选择(checkbox 多选 / radio 单选)
|
|
55
57
|
- 行点击事件
|
|
56
58
|
- 通过 `render` 属性自定义渲染,可拆分表头和表体
|
|
59
|
+
- 树形数据(`dataType: 'tree' | 'treeList'`):展开收起、懒加载、移动端面包屑卡片
|
|
57
60
|
|
|
58
61
|
### 核心 Hooks
|
|
59
62
|
|
|
@@ -107,7 +110,8 @@ npm i --save @kne/table-page
|
|
|
107
110
|
|
|
108
111
|
- `useTableConfig` 管理列宽拖动、显示/隐藏、localStorage 持久化
|
|
109
112
|
- `useGroupHeader` 生成分组表头
|
|
110
|
-
- `rowSelection` 映射为 antd 行选择(含 `allowSelectedAll`
|
|
113
|
+
- `rowSelection` 映射为 antd 行选择(含 `allowSelectedAll` 全选;树形下支持 `checkRelation`)
|
|
114
|
+
- `dataType` 为 `tree` / `treeList` 时归一化为嵌套数据并接入 antd `expandable`
|
|
111
115
|
- `render={({ header, renderBody }) => ...}` 可自定义表格外层,`renderBody()` 返回完整 antd Table
|
|
112
116
|
|
|
113
117
|
#### 移动端:`renderMobile`
|
|
@@ -186,15 +190,15 @@ npm i --save @kne/table-page
|
|
|
186
190
|
#### 示例代码
|
|
187
191
|
|
|
188
192
|
- TablePage
|
|
189
|
-
- 表格页面组件,基于 @kne/react-fetch 实现数据加载与分页,支持 sticky 固定表头、useSort 服务端排序、renderMobile 移动端卡片、renderCard PC 卡片视图切换、tab
|
|
193
|
+
- 表格页面组件,基于 @kne/react-fetch 实现数据加载与分页,支持 sticky 固定表头、useSort 服务端排序、renderMobile 移动端卡片、renderCard PC 卡片视图切换、tab 分类切换、列配置、总结栏、树形 dataType(含筛选/批量/操作列/卡片切换/懒加载);空数据(total 为 0)时不显示分页器。文末含仅 SearchInput + renderMobile 自定义卡片示例(验证工具栏与卡片间距)
|
|
190
194
|
- _TablePage(@kne/current-lib_table-page)[import * as _TablePage from "@kne/table-page"],(@kne/current-lib_table-page/dist/index.css),antd(antd),_ReactFilter(@kne/react-filter)[import * as _ReactFilter from "@kne/react-filter"],(@kne/react-filter/dist/index.css)
|
|
191
195
|
|
|
192
196
|
```jsx
|
|
193
|
-
const { default: TablePage, Table } = _TablePage;
|
|
197
|
+
const { default: TablePage, Table, mergeTreeChildren } = _TablePage;
|
|
194
198
|
const { fields } = _ReactFilter;
|
|
195
199
|
const { SuperSelectFilterItem } = fields;
|
|
196
|
-
const { Table: AntTable, Col, Flex, Row, Tag, Button, Space, Switch, message } = antd;
|
|
197
|
-
const { useMemo, useState } = React;
|
|
200
|
+
const { Table: AntTable, Col, Flex, Row, Tag, Button, Space, Switch, Radio, message } = antd;
|
|
201
|
+
const { useMemo, useState, useRef } = React;
|
|
198
202
|
|
|
199
203
|
const TOTAL = 156;
|
|
200
204
|
|
|
@@ -770,12 +774,353 @@ const SearchMobileExample = () => (
|
|
|
770
774
|
</Flex>
|
|
771
775
|
);
|
|
772
776
|
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
const orgStatusMap = {
|
|
780
|
+
active: { type: 'success', text: '启用' },
|
|
781
|
+
paused: { type: 'warning', text: '停用' }
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
const orgRegionOptions = [
|
|
785
|
+
{ value: 'east', label: '华东' },
|
|
786
|
+
{ value: 'north', label: '华北' },
|
|
787
|
+
{ value: 'south', label: '华南' }
|
|
788
|
+
];
|
|
789
|
+
|
|
790
|
+
const orgStatusOptions = Object.entries(orgStatusMap).map(([value, { text }]) => ({ value, label: text }));
|
|
791
|
+
|
|
792
|
+
const treeColumns = [
|
|
793
|
+
{ name: 'name', title: '名称', width: 200, renderType: 'main' },
|
|
794
|
+
{ name: 'code', title: '编码', width: 120 },
|
|
795
|
+
{ name: 'owner', title: '负责人', width: 100 },
|
|
796
|
+
{
|
|
797
|
+
name: 'status',
|
|
798
|
+
title: '状态',
|
|
799
|
+
width: 100,
|
|
800
|
+
renderType: 'status',
|
|
801
|
+
getValueOf: item => orgStatusMap[item.status] || { type: 'default', text: item.status }
|
|
802
|
+
},
|
|
803
|
+
{
|
|
804
|
+
name: 'options',
|
|
805
|
+
title: '操作',
|
|
806
|
+
width: 160,
|
|
807
|
+
renderType: 'options',
|
|
808
|
+
fixed: 'right',
|
|
809
|
+
getValueOf: item => [
|
|
810
|
+
{ children: '查看', onClick: () => message.info(`查看 ${item.name}`) },
|
|
811
|
+
{ children: '编辑', onClick: () => message.info(`编辑 ${item.name}`) },
|
|
812
|
+
{
|
|
813
|
+
children: '删除',
|
|
814
|
+
isDelete: true,
|
|
815
|
+
message: `确定删除 ${item.name} 吗?`,
|
|
816
|
+
onClick: () => message.warning(`已删除 ${item.name}`)
|
|
817
|
+
}
|
|
818
|
+
]
|
|
819
|
+
}
|
|
820
|
+
];
|
|
821
|
+
|
|
822
|
+
const orgTreeData = [
|
|
823
|
+
{
|
|
824
|
+
id: '1',
|
|
825
|
+
name: '华东区',
|
|
826
|
+
code: 'EAST',
|
|
827
|
+
owner: '张三',
|
|
828
|
+
region: 'east',
|
|
829
|
+
status: 'active',
|
|
830
|
+
children: [
|
|
831
|
+
{
|
|
832
|
+
id: '1-1',
|
|
833
|
+
name: '上海',
|
|
834
|
+
code: 'SH',
|
|
835
|
+
owner: '李四',
|
|
836
|
+
region: 'east',
|
|
837
|
+
status: 'active',
|
|
838
|
+
children: [
|
|
839
|
+
{ id: '1-1-1', name: '浦东分部', code: 'SH-PD', owner: '王五', region: 'east', status: 'active' },
|
|
840
|
+
{ id: '1-1-2', name: '徐汇分部', code: 'SH-XH', owner: '赵六', region: 'east', status: 'paused' }
|
|
841
|
+
]
|
|
842
|
+
},
|
|
843
|
+
{ id: '1-2', name: '杭州', code: 'HZ', owner: '钱七', region: 'east', status: 'active' }
|
|
844
|
+
]
|
|
845
|
+
},
|
|
846
|
+
{
|
|
847
|
+
id: '2',
|
|
848
|
+
name: '华北区',
|
|
849
|
+
code: 'NORTH',
|
|
850
|
+
owner: '孙八',
|
|
851
|
+
region: 'north',
|
|
852
|
+
status: 'active',
|
|
853
|
+
children: [{ id: '2-1', name: '北京', code: 'BJ', owner: '周九', region: 'north', status: 'paused' }]
|
|
854
|
+
}
|
|
855
|
+
];
|
|
856
|
+
|
|
857
|
+
const orgTreeListData = [
|
|
858
|
+
{ id: '1', name: '华东区', code: 'EAST', owner: '张三', parentId: null, region: 'east', status: 'active' },
|
|
859
|
+
{ id: '1-1', name: '上海', code: 'SH', owner: '李四', parentId: '1', region: 'east', status: 'active' },
|
|
860
|
+
{ id: '1-1-1', name: '浦东分部', code: 'SH-PD', owner: '王五', parentId: '1-1', region: 'east', status: 'active' },
|
|
861
|
+
{ id: '1-1-2', name: '徐汇分部', code: 'SH-XH', owner: '赵六', parentId: '1-1', region: 'east', status: 'paused' },
|
|
862
|
+
{ id: '1-2', name: '杭州', code: 'HZ', owner: '钱七', parentId: '1', region: 'east', status: 'active' },
|
|
863
|
+
{ id: '2', name: '华北区', code: 'NORTH', owner: '孙八', parentId: '', region: 'north', status: 'active' },
|
|
864
|
+
{ id: '2-1', name: '北京', code: 'BJ', owner: '周九', parentId: '2', region: 'north', status: 'paused' }
|
|
865
|
+
];
|
|
866
|
+
|
|
867
|
+
const lazyOrgRoot = [
|
|
868
|
+
{ id: 'org-1', name: '集团总部', code: 'HQ', owner: '张三', parentId: null, region: 'east', status: 'active', hasChildren: true },
|
|
869
|
+
{ id: 'org-2', name: '分公司', code: 'BR', owner: '李四', parentId: null, region: 'south', status: 'active', hasChildren: true }
|
|
870
|
+
];
|
|
871
|
+
|
|
872
|
+
const lazyOrgChildrenMap = {
|
|
873
|
+
'org-1': [
|
|
874
|
+
{ id: 'org-1-1', name: '研发中心', code: 'RD', owner: '王五', region: 'east', status: 'active', hasChildren: true },
|
|
875
|
+
{ id: 'org-1-2', name: '市场部', code: 'MKT', owner: '赵六', region: 'east', status: 'paused', hasChildren: false }
|
|
876
|
+
],
|
|
877
|
+
'org-1-1': [
|
|
878
|
+
{ id: 'org-1-1-1', name: '前端组', code: 'FE', owner: '钱七', region: 'east', status: 'active', hasChildren: false },
|
|
879
|
+
{ id: 'org-1-1-2', name: '后端组', code: 'BE', owner: '孙八', region: 'east', status: 'active', hasChildren: false }
|
|
880
|
+
],
|
|
881
|
+
'org-2': [{ id: 'org-2-1', name: '华南办', code: 'SC', owner: '周九', region: 'south', status: 'active', hasChildren: false }]
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
const OrgTreeCard = ({ item }) => (
|
|
885
|
+
<div
|
|
886
|
+
style={{
|
|
887
|
+
boxSizing: 'border-box',
|
|
888
|
+
border: '1px solid #f0f0f0',
|
|
889
|
+
borderRadius: 8,
|
|
890
|
+
padding: 16,
|
|
891
|
+
background: '#fff'
|
|
892
|
+
}}
|
|
893
|
+
>
|
|
894
|
+
<Flex justify="space-between" align="center" style={{ marginBottom: 8 }}>
|
|
895
|
+
<strong>{item.name}</strong>
|
|
896
|
+
<Tag color={orgStatusMap[item.status]?.type}>{orgStatusMap[item.status]?.text || item.status}</Tag>
|
|
897
|
+
</Flex>
|
|
898
|
+
<Flex align="center" gap={8} wrap style={{ marginBottom: 4, fontSize: 13, color: 'rgba(0,0,0,0.65)' }}>
|
|
899
|
+
<span>编码 {item.code}</span>
|
|
900
|
+
<span style={{ color: 'rgba(0,0,0,0.25)' }}>·</span>
|
|
901
|
+
<span>负责人 {item.owner}</span>
|
|
902
|
+
</Flex>
|
|
903
|
+
<Flex justify="flex-end" gap={4} style={{ marginTop: 12, paddingTop: 8, borderTop: '1px solid #f0f0f0' }}>
|
|
904
|
+
<Button type="link" size="small" onClick={() => message.info(`查看 ${item.name}`)}>
|
|
905
|
+
查看
|
|
906
|
+
</Button>
|
|
907
|
+
<Button type="link" size="small" onClick={() => message.info(`编辑 ${item.name}`)}>
|
|
908
|
+
编辑
|
|
909
|
+
</Button>
|
|
910
|
+
</Flex>
|
|
911
|
+
</div>
|
|
912
|
+
);
|
|
913
|
+
|
|
914
|
+
const renderOrgTreeCard = ({ displayDataSource, dataSource = [] }) => {
|
|
915
|
+
const list = displayDataSource || dataSource;
|
|
916
|
+
return (
|
|
917
|
+
<Row gutter={[12, 12]}>
|
|
918
|
+
{list.map(item => (
|
|
919
|
+
<Col span={12} key={item.id}>
|
|
920
|
+
<OrgTreeCard item={item} />
|
|
921
|
+
</Col>
|
|
922
|
+
))}
|
|
923
|
+
</Row>
|
|
924
|
+
);
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
const applyOrgTreeFilters = (list, data, requestParams) => {
|
|
928
|
+
const params = Object.assign({}, requestParams?.data, data);
|
|
929
|
+
const keyword = String(params.keyword || '').trim().toLowerCase();
|
|
930
|
+
const region = normalizeFilterValue(params.region);
|
|
931
|
+
const status = normalizeFilterValue(params.status);
|
|
932
|
+
return (list || []).filter(item => {
|
|
933
|
+
if (keyword) {
|
|
934
|
+
const hit = [item.name, item.code, item.owner].some(v => String(v || '').toLowerCase().includes(keyword));
|
|
935
|
+
if (!hit) return false;
|
|
936
|
+
}
|
|
937
|
+
if (region && item.region !== region) return false;
|
|
938
|
+
if (status && item.status !== status) return false;
|
|
939
|
+
return true;
|
|
940
|
+
});
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
/** TablePage 树形:筛选 / 搜索 / 批量 / 操作列 / 卡片切换 / 懒加载 */
|
|
944
|
+
const TreePageExample = () => {
|
|
945
|
+
const treePageRef = useRef(null);
|
|
946
|
+
const { selectedRowKeys, selectedRows, getRowSelection, clearSelectedRows } = Table.useSelectedRow({ rowKey: 'id' });
|
|
947
|
+
const [checkRelation, setCheckRelation] = useState('parent');
|
|
948
|
+
const lazyDataRef = useRef(lazyOrgRoot);
|
|
949
|
+
const lazyPageRef = useRef(null);
|
|
950
|
+
|
|
951
|
+
const handleLoadChildren = (item, { key }) =>
|
|
952
|
+
new Promise(resolve => {
|
|
953
|
+
setTimeout(() => {
|
|
954
|
+
lazyDataRef.current = mergeTreeChildren(lazyDataRef.current, lazyOrgChildrenMap[key] || [], {
|
|
955
|
+
parentKeyValue: key,
|
|
956
|
+
dataType: 'treeList',
|
|
957
|
+
rowKey: 'id',
|
|
958
|
+
parentKey: 'parentId',
|
|
959
|
+
hasChildrenKey: 'hasChildren'
|
|
960
|
+
});
|
|
961
|
+
lazyPageRef.current?.reload?.();
|
|
962
|
+
resolve();
|
|
963
|
+
}, 800);
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
return (
|
|
967
|
+
<Flex vertical gap={24}>
|
|
968
|
+
<div style={{ color: '#666', fontSize: 13, lineHeight: 1.7 }}>
|
|
969
|
+
<Tag color="blue" style={{ marginRight: 8 }}>
|
|
970
|
+
tree
|
|
971
|
+
</Tag>
|
|
972
|
+
树形 TablePage:支持 <code>dataType</code>、<code>checkRelation</code>,并演示筛选 / 搜索 / 批量操作 / 行操作 / <code>renderCard</code> 卡片切换 / 懒加载。
|
|
973
|
+
</div>
|
|
974
|
+
|
|
975
|
+
<div>
|
|
976
|
+
<div style={{ marginBottom: 8, color: '#666' }}>treeList + 工具栏(筛选 / 批量 / 操作 / 卡片切换)</div>
|
|
977
|
+
<Space style={{ marginBottom: 8 }} wrap>
|
|
978
|
+
<span style={{ color: '#666' }}>checkRelation:</span>
|
|
979
|
+
<Radio.Group
|
|
980
|
+
value={checkRelation}
|
|
981
|
+
optionType="button"
|
|
982
|
+
options={[
|
|
983
|
+
{ label: 'parent', value: 'parent' },
|
|
984
|
+
{ label: 'all', value: 'all' },
|
|
985
|
+
{ label: 'independent', value: 'independent' }
|
|
986
|
+
]}
|
|
987
|
+
onChange={e => {
|
|
988
|
+
setCheckRelation(e.target.value);
|
|
989
|
+
clearSelectedRows();
|
|
990
|
+
}}
|
|
991
|
+
/>
|
|
992
|
+
</Space>
|
|
993
|
+
<TablePage
|
|
994
|
+
ref={treePageRef}
|
|
995
|
+
name="demo-table-page-tree-list"
|
|
996
|
+
pagination={{ open: false }}
|
|
997
|
+
columns={treeColumns}
|
|
998
|
+
dataType="treeList"
|
|
999
|
+
defaultExpandedKeys
|
|
1000
|
+
controllerOpen={false}
|
|
1001
|
+
renderMobile
|
|
1002
|
+
renderCard={renderOrgTreeCard}
|
|
1003
|
+
rowSelection={getRowSelection(orgTreeListData, { type: 'checkbox', allowSelectedAll: true, checkRelation })}
|
|
1004
|
+
selectedRows={selectedRows}
|
|
1005
|
+
search={{ name: 'keyword', label: '关键词', placeholder: '搜索名称/编码/负责人', style: { width: 240 } }}
|
|
1006
|
+
buttonGroup={{
|
|
1007
|
+
list: [
|
|
1008
|
+
{
|
|
1009
|
+
type: 'primary',
|
|
1010
|
+
children: '新建组织',
|
|
1011
|
+
onClick: () => message.success('打开新建组织')
|
|
1012
|
+
},
|
|
1013
|
+
{
|
|
1014
|
+
children: '导出组织树',
|
|
1015
|
+
onClick: () => message.info('正在导出组织树')
|
|
1016
|
+
}
|
|
1017
|
+
]
|
|
1018
|
+
}}
|
|
1019
|
+
tab={{
|
|
1020
|
+
name: 'region',
|
|
1021
|
+
label: '大区',
|
|
1022
|
+
list: orgRegionOptions
|
|
1023
|
+
}}
|
|
1024
|
+
filter={{
|
|
1025
|
+
list: [
|
|
1026
|
+
[
|
|
1027
|
+
{
|
|
1028
|
+
type: SuperSelectFilterItem,
|
|
1029
|
+
props: { name: 'status', label: '状态', single: true, options: orgStatusOptions }
|
|
1030
|
+
}
|
|
1031
|
+
]
|
|
1032
|
+
],
|
|
1033
|
+
displayLine: 1
|
|
1034
|
+
}}
|
|
1035
|
+
batchActions={[
|
|
1036
|
+
{
|
|
1037
|
+
key: 'export',
|
|
1038
|
+
label: '批量导出',
|
|
1039
|
+
onClick: ({ selectedRowKeys: keys }) => {
|
|
1040
|
+
message.info(`正在导出 ${keys.length} 个节点`);
|
|
1041
|
+
}
|
|
1042
|
+
},
|
|
1043
|
+
{
|
|
1044
|
+
key: 'enable',
|
|
1045
|
+
label: '批量启用',
|
|
1046
|
+
onClick: ({ selectedRowKeys: keys }) => {
|
|
1047
|
+
message.success(`已启用 ${keys.length} 个节点`);
|
|
1048
|
+
}
|
|
1049
|
+
},
|
|
1050
|
+
{
|
|
1051
|
+
key: 'disable',
|
|
1052
|
+
label: '批量停用',
|
|
1053
|
+
danger: true,
|
|
1054
|
+
onClick: ({ selectedRowKeys: keys }) => {
|
|
1055
|
+
message.warning(`已停用 ${keys.length} 个节点`);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
]}
|
|
1059
|
+
loader={({ data, requestParams }) => {
|
|
1060
|
+
const filtered = applyOrgTreeFilters(orgTreeListData, data, requestParams);
|
|
1061
|
+
return Promise.resolve({
|
|
1062
|
+
pageData: filtered,
|
|
1063
|
+
totalCount: filtered.length
|
|
1064
|
+
});
|
|
1065
|
+
}}
|
|
1066
|
+
/>
|
|
1067
|
+
<div style={{ marginTop: 8, color: '#666', fontSize: 13 }}>已选 key:{selectedRowKeys.join(', ') || '无'}</div>
|
|
1068
|
+
</div>
|
|
1069
|
+
|
|
1070
|
+
<div>
|
|
1071
|
+
<div style={{ marginBottom: 8, color: '#666' }}>dataType="tree"(嵌套 children)</div>
|
|
1072
|
+
<TablePage
|
|
1073
|
+
name="demo-table-page-tree"
|
|
1074
|
+
pagination={{ open: false }}
|
|
1075
|
+
columns={treeColumns}
|
|
1076
|
+
dataType="tree"
|
|
1077
|
+
defaultExpandedKeys
|
|
1078
|
+
controllerOpen={false}
|
|
1079
|
+
renderMobile
|
|
1080
|
+
renderCard={renderOrgTreeCard}
|
|
1081
|
+
buttonGroup={{
|
|
1082
|
+
list: [{ type: 'primary', children: '新建', onClick: () => message.success('新建') }]
|
|
1083
|
+
}}
|
|
1084
|
+
loader={() =>
|
|
1085
|
+
Promise.resolve({
|
|
1086
|
+
pageData: orgTreeData,
|
|
1087
|
+
totalCount: orgTreeData.length
|
|
1088
|
+
})
|
|
1089
|
+
}
|
|
1090
|
+
/>
|
|
1091
|
+
</div>
|
|
1092
|
+
|
|
1093
|
+
<div>
|
|
1094
|
+
<div style={{ marginBottom: 8, color: '#666' }}>懒加载:hasChildren + onLoadChildren + mergeTreeChildren</div>
|
|
1095
|
+
<TablePage
|
|
1096
|
+
ref={lazyPageRef}
|
|
1097
|
+
name="demo-table-page-tree-lazy"
|
|
1098
|
+
pagination={{ open: false }}
|
|
1099
|
+
columns={treeColumns}
|
|
1100
|
+
dataType="treeList"
|
|
1101
|
+
controllerOpen={false}
|
|
1102
|
+
onLoadChildren={handleLoadChildren}
|
|
1103
|
+
loader={() =>
|
|
1104
|
+
Promise.resolve({
|
|
1105
|
+
pageData: lazyDataRef.current,
|
|
1106
|
+
totalCount: lazyDataRef.current.length
|
|
1107
|
+
})
|
|
1108
|
+
}
|
|
1109
|
+
/>
|
|
1110
|
+
</div>
|
|
1111
|
+
</Flex>
|
|
1112
|
+
);
|
|
1113
|
+
};
|
|
1114
|
+
|
|
1115
|
+
|
|
773
1116
|
render(
|
|
774
1117
|
<Flex vertical gap={32}>
|
|
775
1118
|
<BaseExample />
|
|
1119
|
+
<TreePageExample />
|
|
776
1120
|
<SearchMobileExample />
|
|
777
1121
|
</Flex>
|
|
778
1122
|
);
|
|
1123
|
+
|
|
779
1124
|
```
|
|
780
1125
|
|
|
781
1126
|
- TablePage sticky scroll
|
|
@@ -1368,6 +1713,162 @@ render(<BaseExample />);
|
|
|
1368
1713
|
|
|
1369
1714
|
```
|
|
1370
1715
|
|
|
1716
|
+
- tree
|
|
1717
|
+
- 树状数据:Table / TableView 支持 dataType 为 tree / treeList,含展开收起、懒加载、checkRelation 勾选关联
|
|
1718
|
+
- _TablePage(@kne/current-lib_table-page)[import * as _TablePage from "@kne/table-page"],(@kne/current-lib_table-page/dist/index.css),antd(antd)
|
|
1719
|
+
|
|
1720
|
+
```jsx
|
|
1721
|
+
const { Table, TableView, mergeTreeChildren } = _TablePage;
|
|
1722
|
+
const { Space, Button, Radio } = antd;
|
|
1723
|
+
const { useState } = React;
|
|
1724
|
+
|
|
1725
|
+
const columns = [
|
|
1726
|
+
{ name: 'name', title: '名称', renderType: 'main' },
|
|
1727
|
+
{ name: 'code', title: '编码', width: 120 },
|
|
1728
|
+
{ name: 'owner', title: '负责人', width: 100 }
|
|
1729
|
+
];
|
|
1730
|
+
|
|
1731
|
+
const treeData = [
|
|
1732
|
+
{
|
|
1733
|
+
id: '1',
|
|
1734
|
+
name: '华东区',
|
|
1735
|
+
code: 'EAST',
|
|
1736
|
+
owner: '张三',
|
|
1737
|
+
children: [
|
|
1738
|
+
{
|
|
1739
|
+
id: '1-1',
|
|
1740
|
+
name: '上海',
|
|
1741
|
+
code: 'SH',
|
|
1742
|
+
owner: '李四',
|
|
1743
|
+
children: [
|
|
1744
|
+
{ id: '1-1-1', name: '浦东分部', code: 'SH-PD', owner: '王五' },
|
|
1745
|
+
{ id: '1-1-2', name: '徐汇分部', code: 'SH-XH', owner: '赵六' }
|
|
1746
|
+
]
|
|
1747
|
+
},
|
|
1748
|
+
{ id: '1-2', name: '杭州', code: 'HZ', owner: '钱七' }
|
|
1749
|
+
]
|
|
1750
|
+
},
|
|
1751
|
+
{
|
|
1752
|
+
id: '2',
|
|
1753
|
+
name: '华北区',
|
|
1754
|
+
code: 'NORTH',
|
|
1755
|
+
owner: '孙八',
|
|
1756
|
+
children: [{ id: '2-1', name: '北京', code: 'BJ', owner: '周九' }]
|
|
1757
|
+
}
|
|
1758
|
+
];
|
|
1759
|
+
|
|
1760
|
+
const treeListData = [
|
|
1761
|
+
{ id: '1', name: '华东区', code: 'EAST', owner: '张三', parentId: null },
|
|
1762
|
+
{ id: '1-1', name: '上海', code: 'SH', owner: '李四', parentId: '1' },
|
|
1763
|
+
{ id: '1-1-1', name: '浦东分部', code: 'SH-PD', owner: '王五', parentId: '1-1' },
|
|
1764
|
+
{ id: '1-1-2', name: '徐汇分部', code: 'SH-XH', owner: '赵六', parentId: '1-1' },
|
|
1765
|
+
{ id: '1-2', name: '杭州', code: 'HZ', owner: '钱七', parentId: '1' },
|
|
1766
|
+
{ id: '2', name: '华北区', code: 'NORTH', owner: '孙八', parentId: '' },
|
|
1767
|
+
{ id: '2-1', name: '北京', code: 'BJ', owner: '周九', parentId: '2' }
|
|
1768
|
+
];
|
|
1769
|
+
|
|
1770
|
+
const lazyRootData = [
|
|
1771
|
+
{ id: 'org-1', name: '集团总部', code: 'HQ', owner: '张三', parentId: null, hasChildren: true },
|
|
1772
|
+
{ id: 'org-2', name: '分公司', code: 'BR', owner: '李四', parentId: null, hasChildren: true }
|
|
1773
|
+
];
|
|
1774
|
+
|
|
1775
|
+
const lazyChildrenMap = {
|
|
1776
|
+
'org-1': [
|
|
1777
|
+
{ id: 'org-1-1', name: '研发中心', code: 'RD', owner: '王五', hasChildren: true },
|
|
1778
|
+
{ id: 'org-1-2', name: '市场部', code: 'MKT', owner: '赵六', hasChildren: false }
|
|
1779
|
+
],
|
|
1780
|
+
'org-1-1': [
|
|
1781
|
+
{ id: 'org-1-1-1', name: '前端组', code: 'FE', owner: '钱七', hasChildren: false },
|
|
1782
|
+
{ id: 'org-1-1-2', name: '后端组', code: 'BE', owner: '孙八', hasChildren: false }
|
|
1783
|
+
],
|
|
1784
|
+
'org-2': [{ id: 'org-2-1', name: '华南办', code: 'SC', owner: '周九', hasChildren: false }]
|
|
1785
|
+
};
|
|
1786
|
+
|
|
1787
|
+
const TreeExample = () => {
|
|
1788
|
+
const { selectedRowKeys, getRowSelection, clearSelectedRows } = Table.useSelectedRow({ rowKey: 'id' });
|
|
1789
|
+
const treeListSelection = Table.useSelectedRow({ rowKey: 'id' });
|
|
1790
|
+
const [expandedKeys, setExpandedKeys] = useState(false);
|
|
1791
|
+
const [checkRelation, setCheckRelation] = useState('parent');
|
|
1792
|
+
const [lazyData, setLazyData] = useState(lazyRootData);
|
|
1793
|
+
|
|
1794
|
+
const handleLoadChildren = (item, { key }) =>
|
|
1795
|
+
new Promise(resolve => {
|
|
1796
|
+
setTimeout(() => {
|
|
1797
|
+
const children = lazyChildrenMap[key] || [];
|
|
1798
|
+
setLazyData(prev =>
|
|
1799
|
+
mergeTreeChildren(prev, children, {
|
|
1800
|
+
parentKeyValue: key,
|
|
1801
|
+
dataType: 'treeList',
|
|
1802
|
+
rowKey: 'id',
|
|
1803
|
+
parentKey: 'parentId',
|
|
1804
|
+
hasChildrenKey: 'hasChildren'
|
|
1805
|
+
})
|
|
1806
|
+
);
|
|
1807
|
+
resolve();
|
|
1808
|
+
}, 800);
|
|
1809
|
+
});
|
|
1810
|
+
|
|
1811
|
+
return (
|
|
1812
|
+
<Space direction="vertical" size="large" style={{ width: '100%' }}>
|
|
1813
|
+
<div>
|
|
1814
|
+
<div style={{ marginBottom: 8 }}>Table:dataType="tree"(嵌套 children)</div>
|
|
1815
|
+
<Table dataSource={treeData} columns={columns} dataType="tree" defaultExpandedKeys controllerOpen={false} />
|
|
1816
|
+
</div>
|
|
1817
|
+
|
|
1818
|
+
<div>
|
|
1819
|
+
<div style={{ marginBottom: 8 }}>Table:dataType="treeList" + 勾选(checkRelation)</div>
|
|
1820
|
+
<Space style={{ marginBottom: 8 }} wrap>
|
|
1821
|
+
<Radio.Group
|
|
1822
|
+
value={checkRelation}
|
|
1823
|
+
optionType="button"
|
|
1824
|
+
options={[
|
|
1825
|
+
{ label: 'parent', value: 'parent' },
|
|
1826
|
+
{ label: 'all', value: 'all' },
|
|
1827
|
+
{ label: 'independent', value: 'independent' }
|
|
1828
|
+
]}
|
|
1829
|
+
onChange={e => {
|
|
1830
|
+
setCheckRelation(e.target.value);
|
|
1831
|
+
clearSelectedRows();
|
|
1832
|
+
}}
|
|
1833
|
+
/>
|
|
1834
|
+
</Space>
|
|
1835
|
+
<Table dataSource={treeListData} columns={columns} dataType="treeList" defaultExpandedKeys controllerOpen={false} rowSelection={getRowSelection(treeListData, { allowSelectedAll: true, checkRelation })} />
|
|
1836
|
+
<div style={{ marginTop: 8 }}>已选 key:{selectedRowKeys.join(', ') || '无'}</div>
|
|
1837
|
+
</div>
|
|
1838
|
+
|
|
1839
|
+
<div>
|
|
1840
|
+
<div style={{ marginBottom: 8 }}>Table:懒加载(hasChildren + onLoadChildren + mergeTreeChildren)</div>
|
|
1841
|
+
<Table dataSource={lazyData} columns={columns} dataType="treeList" onLoadChildren={handleLoadChildren} controllerOpen={false} />
|
|
1842
|
+
</div>
|
|
1843
|
+
|
|
1844
|
+
<div>
|
|
1845
|
+
<div style={{ marginBottom: 8 }}>Table:受控展开 true / false / key 数组</div>
|
|
1846
|
+
<Space style={{ marginBottom: 8 }}>
|
|
1847
|
+
<Button size="small" onClick={() => setExpandedKeys(true)}>
|
|
1848
|
+
全部展开
|
|
1849
|
+
</Button>
|
|
1850
|
+
<Button size="small" onClick={() => setExpandedKeys(false)}>
|
|
1851
|
+
全部收起
|
|
1852
|
+
</Button>
|
|
1853
|
+
<Button size="small" onClick={() => setExpandedKeys(['1', '1-1'])}>
|
|
1854
|
+
展开指定节点
|
|
1855
|
+
</Button>
|
|
1856
|
+
</Space>
|
|
1857
|
+
<Table dataSource={treeData} columns={columns} dataType="tree" expandedKeys={expandedKeys} onExpandedKeysChange={setExpandedKeys} controllerOpen={false} />
|
|
1858
|
+
</div>
|
|
1859
|
+
|
|
1860
|
+
<div>
|
|
1861
|
+
<div style={{ marginBottom: 8 }}>TableView:同样 API(CSS Grid 树形)</div>
|
|
1862
|
+
<TableView dataSource={treeListData} columns={columns} dataType="treeList" defaultExpandedKeys rowSelection={treeListSelection.getRowSelection(treeListData, { allowSelectedAll: true, checkRelation: 'parent' })} />
|
|
1863
|
+
</div>
|
|
1864
|
+
</Space>
|
|
1865
|
+
);
|
|
1866
|
+
};
|
|
1867
|
+
|
|
1868
|
+
render(<TreeExample />);
|
|
1869
|
+
|
|
1870
|
+
```
|
|
1871
|
+
|
|
1371
1872
|
- useSelectedRow
|
|
1372
1873
|
- 行选择 Hook,配合 Table / TableView 实现多选、全选、批量操作与单选
|
|
1373
1874
|
- _TablePage(@kne/current-lib_table-page)[import * as _TablePage from "@kne/table-page"],(@kne/current-lib_table-page/dist/index.css),antd(antd)
|
|
@@ -3093,7 +3594,7 @@ render(<BaseExample />);
|
|
|
3093
3594
|
| selectedRows | array | - | 已选行数据,传给 `batchActions` 的 `onClick` 上下文 |
|
|
3094
3595
|
| className | string | - | 自定义类名 |
|
|
3095
3596
|
| ...fetchProps | - | - | 其余属性透传给 `@kne/react-fetch`(如 `url`、`params`、`auto` 等) |
|
|
3096
|
-
| ...tableProps | - | - | 其余属性透传给内部 `Table` / `TableView`(如 `rowKey`、`rowSelection`、`scroll`、`size`、`renderMobile`、`sortRender`、`mobileSortToolbar
|
|
3597
|
+
| ...tableProps | - | - | 其余属性透传给内部 `Table` / `TableView`(如 `rowKey`、`rowSelection`、`scroll`、`size`、`renderMobile`、`sortRender`、`mobileSortToolbar`、`dataType`、`expandedKeys`、`onLoadChildren` 等树形属性) |
|
|
3097
3598
|
|
|
3098
3599
|
#### pagination
|
|
3099
3600
|
|
|
@@ -3280,6 +3781,16 @@ render(<BaseExample />);
|
|
|
3280
3781
|
| sortRender | function | - | 排序按钮渲染,由 `useSort` 提供(桌面端表头) |
|
|
3281
3782
|
| mobileSortToolbar | function | - | 移动端排序工具栏,由 `useSort` 提供;传入 TableView 后由 `renderToolbar` / 默认卡片复用 |
|
|
3282
3783
|
| size | `'small'` \| `'large'` | - | 单元格内边距:默认 `8px`,`small` 为 `4px`,`large` 为 `14px 8px`;可通过 CSS 变量覆盖 |
|
|
3784
|
+
| dataType | `'list'` \| `'tree'` \| `'treeList'` | `'list'` | 数据形态。`list` 扁平;`tree` 使用 `childrenKey` 嵌套;`treeList` 按 `parentKey` 组装为树 |
|
|
3785
|
+
| parentKey | string | `'parentId'` | `treeList` 父子关联字段 |
|
|
3786
|
+
| childrenKey | string | `'children'` | 子节点字段名 |
|
|
3787
|
+
| hasChildrenKey | string | `'hasChildren'` | 懒加载标记;为 `true` 时即使尚无子节点也显示展开图标 |
|
|
3788
|
+
| treeTitleKey | string \| function | `'name'` | 移动端树形面包屑文案字段(委托 TableView) |
|
|
3789
|
+
| onLoadChildren | function | - | 懒加载:`(item, { key }) => void \| Promise`;请用 `mergeTreeChildren` 合并回 dataSource |
|
|
3790
|
+
| expandedKeys | `true` \| `false` \| `Array` | - | 受控展开。`true` 全开,`false` 全关,数组为展开 key |
|
|
3791
|
+
| defaultExpandedKeys | `true` \| `false` \| `Array` | `false` | 非受控初始展开 |
|
|
3792
|
+
| onExpandedKeysChange | function | - | 展开变化回调 `(keys) => void` |
|
|
3793
|
+
| indentSize | number | `16` | 树形每层缩进(px),映射 antd `expandable.indentSize` |
|
|
3283
3794
|
|
|
3284
3795
|
`renderMobile` 为 function 时,TableView 会传入已接好 `rowSelection` / `mobileSortToolbar` 的能力,自定义布局只需选用:
|
|
3285
3796
|
|
|
@@ -3347,6 +3858,7 @@ renderMobile={({ dataSource, renderToolbar, getSelectionProps, getRowKey }) => (
|
|
|
3347
3858
|
| allowSelectedAll | boolean | - | 是否允许全选(仅 checkbox 模式) |
|
|
3348
3859
|
| isSelectedAll | boolean | - | 是否全选状态 |
|
|
3349
3860
|
| onIsSelectAllChange | function | - | 全选状态变化回调 |
|
|
3861
|
+
| checkRelation | `'parent'` \| `'all'` \| `'independent'` | `'parent'` | 树形 checkbox 父子勾选关联(仅 `dataType` 为 `tree` / `treeList`):`parent` 勾父级时子级 UI 全勾但值只留父级;`all` 值含父级与子孙;`independent` 互不影响 |
|
|
3350
3862
|
|
|
3351
3863
|
### useSelectedRow
|
|
3352
3864
|
|
|
@@ -3456,7 +3968,12 @@ const sortedData = useMemo(() => Table.sortDataSource(dataSource, sort, columns)
|
|
|
3456
3968
|
| controllerOpen | boolean | `true` | 是否开启列宽拖动与列配置面板 |
|
|
3457
3969
|
| tableServerApis | object | - | 自定义列配置存储 API,默认使用 `localStorage` |
|
|
3458
3970
|
| size | `'small'` \| `'large'` | - | 单元格内边距:默认 `8px`,`small` 为 `4px`,`large` 为 `14px 8px`;可通过 CSS 变量覆盖(同 TableView) |
|
|
3459
|
-
|
|
|
3971
|
+
| dataType | `'list'` \| `'tree'` \| `'treeList'` | `'list'` | 同 TableView;`tree` / `treeList` 时使用与 TableView 一致的自绘展开列(三角 + 缩进),并隐藏 antd 默认展开列 |
|
|
3972
|
+
| parentKey / childrenKey / hasChildrenKey | string | 见 TableView | 同 TableView |
|
|
3973
|
+
| onLoadChildren | function | - | 同 TableView;展开时触发懒加载,三角显示 loading |
|
|
3974
|
+
| expandedKeys / defaultExpandedKeys / onExpandedKeysChange | - | - | 同 TableView |
|
|
3975
|
+
| indentSize | number | `16` | 同 TableView;作用于自绘展开列缩进 |
|
|
3976
|
+
| ...antdTableProps | - | - | 其余属性透传给 antd `Table`(如 `scroll`、`bordered`);树形下内部会合并 `expandable`(`showExpandColumn: false`) |
|
|
3460
3977
|
|
|
3461
3978
|
#### 与 TableView 的差异
|
|
3462
3979
|
|