From 6c4349a0852d16f7f11a8ef0ad5f63647986db95 Mon Sep 17 00:00:00 2001 From: sheepluo Date: Thu, 14 Jul 2022 23:49:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(table):=20=E4=BF=AE=E5=A4=8D=E6=87=92?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B=E6=94=AF=E6=8C=81=E5=90=8C=E6=97=B6=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E5=A4=9A=E4=B8=AA=E6=A0=B9=E8=8A=82=E7=82=B9=20(#1176?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/table/demos/tree.vue | 6 ++++- src/table/hooks/tree-store.ts | 42 +++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/examples/table/demos/tree.vue b/examples/table/demos/tree.vue index 3d520644a..0fd2b26fd 100644 --- a/examples/table/demos/tree.vue +++ b/examples/table/demos/tree.vue @@ -95,6 +95,7 @@ function getData(currentPage = 1) { ...obj, id: thirdIndex, key: `我是 ${thirdIndex}_${currentPage} 号`, + list: true, }; }); return secondObj; @@ -285,7 +286,7 @@ export default { type: 'Number', }, ]; - this.$refs.table.appendTo(row.key, newData); + this.$refs.table.appendTo(row?.key, newData); MessagePlugin.success(`已插入子节点我是 ${randomKey1} 和 ${randomKey2} 号,请展开查看`); }, @@ -387,6 +388,9 @@ export default { needed: key % 4 === 0 ? '是' : '否', description: '数据源', }); + + // 同时添加多个元素,示例代码有效勿删 + // this.appendMultipleDataTo(); }, onAbnormalDragSort(params) { diff --git a/src/table/hooks/tree-store.ts b/src/table/hooks/tree-store.ts index e0964d5b6..3a9e89ba8 100644 --- a/src/table/hooks/tree-store.ts +++ b/src/table/hooks/tree-store.ts @@ -264,32 +264,36 @@ class TableTreeStore { minRowIndex: firstNewChildrenIndex, rowKey: keys.rowKey, type: 'add', - count: newChildrenData.length, + count: Math.max(newChildrenData.length - 1, 0), }); } return dataSource; } - appendToRoot(newData: T, dataSource: T[], keys: KeysType) { - const rowValue = get(newData, keys.rowKey); - if (!rowValue) { - log.error('Table', '`rowKey` could be wrong, can not get rowValue from `data` by `rowKey`.'); - return; + appendToRoot(newData: T | T[], dataSource: T[], keys: KeysType) { + const newDataSource = dataSource.concat(newData); + const tmpNewData = newData instanceof Array ? newData : [newData]; + const dataSourceLen = dataSource.length; + for (let i = 0, len = tmpNewData.length; i < len; i++) { + const rowValue = get(tmpNewData[i], keys.rowKey); + if (!rowValue) { + log.error('Table', '`rowKey` could be wrong, can not get rowValue from `data` by `rowKey`.'); + continue; + } + const state: TableRowState = { + id: rowValue, + row: tmpNewData[i], + rowIndex: dataSourceLen + i, + level: 0, + expanded: false, + expandChildrenLength: 0, + disabled: false, + }; + state.path = [state]; + this.treeDataMap.set(rowValue, state); } - dataSource.push(newData); - const state: TableRowState = { - id: rowValue, - row: newData, - rowIndex: dataSource.length - 1, - level: 0, - expanded: false, - expandChildrenLength: 0, - disabled: false, - }; - state.path = [state]; - this.treeDataMap.set(rowValue, state); - return dataSource; + return newDataSource; } /**