1- import React , { useState , useEffect , useRef } from 'react' ;
2- import { EnhancedTable , Radio , Space , Button , MessagePlugin , Tag } from 'tdesign-react' ;
1+ import React , { useEffect , useRef , useState } from 'react' ;
32import { cloneDeep } from 'lodash-es' ;
4- import { ErrorCircleFilledIcon , CheckCircleFilledIcon , CloseCircleFilledIcon } from 'tdesign-icons-react' ;
3+
4+ import { CheckCircleFilledIcon , CloseCircleFilledIcon , ErrorCircleFilledIcon } from 'tdesign-icons-react' ;
5+ import { Button , EnhancedTable , MessagePlugin , Radio , Space , Tag } from 'tdesign-react' ;
6+
57import type { EnhancedTableProps , TableProps } from 'tdesign-react' ;
68
79const statusNameListMap = {
@@ -15,7 +17,7 @@ const CHILDREN_KEY = 'childrenList';
1517const initData : EnhancedTableProps [ 'data' ] = [ ] ;
1618for ( let i = 0 ; i < 500 ; i ++ ) {
1719 const obj = {
18- key : `first_level_ ${ i } ` ,
20+ key : `${ i + 1 } ` ,
1921 applicant : [ '贾明' , '张三' , '王芳' ] [ i % 3 ] ,
2022 status : i % 3 ,
2123 channel : [ '电子签署' , '纸质签署' , '纸质签署' ] [ i % 3 ] ,
@@ -25,42 +27,41 @@ for (let i = 0; i < 500; i++) {
2527 createTime : [ '2022-01-01' , '2022-02-01' , '2022-03-01' , '2022-04-01' , '2022-05-01' ] [ i % 4 ] ,
2628 childrenList : [ ] ,
2729 } ;
28- obj . childrenList = new Array ( 5 ) . fill ( null ) . map ( ( t , j ) => {
29- const secondIndex = 100 * j + ( i + 1 ) * 10 ;
30+
31+ obj . childrenList = new Array ( 5 ) . fill ( null ) . map ( ( _ , j ) => {
3032 const secondObj = {
3133 ...obj ,
32- status : secondIndex % 3 ,
33- key : `second_level_${ secondIndex } ` ,
34- applicant : [ '贾明' , '张三' , '王芳' ] [ secondIndex % 3 ] ,
34+ status : ( i + j ) % 3 ,
35+ key : `${ i + 1 } -${ j + 1 } ` , // 第二层:1-1, 1-2, 1-3...
36+ applicant : [ '贾明' , '张三' , '王芳' ] [ ( i + j ) % 3 ] ,
37+ childrenList : [ ] ,
3538 } ;
36- secondObj . childrenList = new Array ( 3 ) . fill ( null ) . map ( ( m , n ) => {
37- const thirdIndex = secondIndex * 1000 + 100 * m + ( n + 1 ) * 10 ;
38- return {
39- ...obj ,
40- status : thirdIndex % 3 ,
41- key : `third_level_${ thirdIndex } ` ,
42- applicant : [ '贾明' , '张三' , '王芳' ] [ thirdIndex % 3 ] ,
43- } ;
44- } ) ;
39+
40+ secondObj . childrenList = new Array ( 3 ) . fill ( null ) . map ( ( _ , n ) => ( {
41+ ...obj ,
42+ status : ( i + j + n ) % 3 ,
43+ key : `${ i + 1 } -${ j + 1 } -${ n + 1 } ` ,
44+ applicant : [ '贾明' , '张三' , '王芳' ] [ ( i + j + n ) % 3 ] ,
45+ } ) ) ;
4546 return secondObj ;
4647 } ) ;
4748 initData . push ( obj ) ;
4849}
50+
4951const columns : TableProps [ 'columns' ] = [
5052 {
5153 colKey : 'row-select' ,
5254 type : 'multiple' ,
5355 // 禁用行选中方式一:使用 disabled 禁用行(示例代码有效,勿删)。disabled 参数:{row: RowData; rowIndex: number })
5456 // 这种方式禁用行选中,当前行会添加行类名 t-table__row--disabled,禁用行文字变灰
55- // disabled: ({ rowIndex }) => rowIndex === 1 || rowIndex === 3,
56-
57+ disabled : ( { row } ) => row . status === 1 ,
5758 // 禁用行选中方式二:使用 checkProps 禁用行(示例代码有效,勿删)
5859 // 这种方式禁用行选中,行文本不会变灰
59- checkProps : ( { row } ) => ( { disabled : ! row . childrenList && row . status !== 0 } ) ,
60+ // checkProps: ({ row }) => ({ disabled: row.status === 1 }),
6061 // 自由调整宽度,如果发现元素看不见,请加大宽度
6162 width : 50 ,
6263 } ,
63- { colKey : 'serial-number ' , title : '序号 ' } ,
64+ { colKey : 'key ' , title : '编号 ' } ,
6465 { colKey : 'applicant' , title : '申请人' } ,
6566 {
6667 colKey : 'status' ,
@@ -77,27 +78,22 @@ const columns: TableProps['columns'] = [
7778 ) ,
7879 } ,
7980 { colKey : 'matters' , title : '申请事项' } ,
80- // { colKey: 'email', title: '邮箱地址' },
8181] ;
8282
83- const defaultSelectedRowKeys : EnhancedTableProps [ 'selectedRowKeys' ] = [ ] ;
83+ const defaultSelectedRowKeys : EnhancedTableProps [ 'selectedRowKeys' ] = [ '2-1-1' , '5-2-1' ] ;
8484
8585export default function TableSingleSort ( ) {
86+ // treeTableRef.current 包含各类树形操作方法
87+ const treeTableRef = useRef ( null ) ;
88+
8689 const [ data , setData ] = useState ( [ ...initData ] ) ;
8790 const [ selectedRowKeys , setSelectedRowKeys ] = useState < EnhancedTableProps [ 'selectedRowKeys' ] > ( defaultSelectedRowKeys ) ;
8891 const [ checkStrictly , setCheckStrictly ] = useState ( false ) ;
89- const [ expandedRowKeys , setExpandedRowKeys ] = useState ( [ ] ) ;
90- const treeTableRef = useRef ( null ) ;
91-
92- useEffect ( ( ) => {
93- // 包含 treeDataMap 及各类树形操作方法
94- console . log ( treeTableRef . current ) ;
95- } , [ ] ) ;
9692
9793 useEffect (
9894 ( ) => {
9995 setSelectedRowKeys ( defaultSelectedRowKeys ) ;
100- setData ( cloneDeep ( data ) ) ;
96+ setData ( cloneDeep ( initData ) ) ;
10197 } ,
10298 // eslint-disable-next-line react-hooks/exhaustive-deps
10399 [ checkStrictly ] ,
@@ -109,10 +105,6 @@ export default function TableSingleSort() {
109105 setSelectedRowKeys ( value ) ;
110106 } ;
111107
112- const onExpandChange : EnhancedTableProps [ 'onExpandChange' ] = ( val ) => {
113- setExpandedRowKeys ( val ) ;
114- } ;
115-
116108 const getTreeExpandedRow = ( ) => {
117109 const treeExpandedRowKeys = treeTableRef . current . getTreeExpandedRow ( 'unique' ) ;
118110 console . log ( '行唯一标识值:' , treeExpandedRowKeys ) ;
@@ -126,10 +118,6 @@ export default function TableSingleSort() {
126118 MessagePlugin . success ( '获取成功,请打开控制台查看' ) ;
127119 } ;
128120
129- const onRowClick : EnhancedTableProps [ 'onRowClick' ] = ( data ) => {
130- console . log ( data ) ;
131- } ;
132-
133121 const scrollToElement = ( ) => {
134122 const treeNodeData = treeTableRef . current . getData ( 'first_level_150' ) ;
135123 console . log ( treeNodeData ) ;
@@ -144,15 +132,12 @@ export default function TableSingleSort() {
144132 } ) ;
145133 } ;
146134
147- // 树节点展开,受控示例
148- // const [expandedTreeNodes, setExpandedTreeNodes] = useState(['first_level_0']);
149-
150135 return (
151136 < Space direction = "vertical" >
152137 < Space >
153138 < Radio . Group value = { checkStrictly } onChange = { ( val : boolean ) => setCheckStrictly ( val ) } variant = "default-filled" >
154- < Radio . Button value = { true } > 父子行选中独立</ Radio . Button >
155139 < Radio . Button value = { false } > 父子行选中关联</ Radio . Button >
140+ < Radio . Button value = { true } > 父子行选中独立</ Radio . Button >
156141 </ Radio . Group >
157142 < Button onClick = { getTreeExpandedRow } > 获取树形结构展开的节点</ Button >
158143 < Button onClick = { scrollToElement } > 滚动到指定元素</ Button >
@@ -163,7 +148,6 @@ export default function TableSingleSort() {
163148 rowKey = "key"
164149 data = { data }
165150 columns = { columns }
166- // indeterminateSelectedRowKeys={[1]}
167151 selectedRowKeys = { selectedRowKeys }
168152 onSelectChange = { onSelectChange }
169153 tree = { {
@@ -174,13 +158,7 @@ export default function TableSingleSort() {
174158 } }
175159 height = { 300 }
176160 scroll = { { type : 'virtual' } }
177- expandedRow = { ( { row } ) => < div > 这是展开项数据,我是 { row . key } 号</ div > }
178- expandedRowKeys = { expandedRowKeys }
179- onExpandChange = { onExpandChange }
180- onRowClick = { onRowClick }
181161 lazyLoad
182- // expandedTreeNodes={expandedTreeNodes}
183- // onExpandedTreeNodesChange={setExpandedTreeNodes}
184162 />
185163 </ Space >
186164 ) ;
0 commit comments