diff --git a/packages/table/__tests__/table-column.spec.ts b/packages/table/__tests__/table-column.spec.ts index 8d0adc5816..0fb0853c53 100644 --- a/packages/table/__tests__/table-column.spec.ts +++ b/packages/table/__tests__/table-column.spec.ts @@ -1047,4 +1047,141 @@ describe('table column', () => { wrapper.unmount() }) }) + + describe('tree table', () => { + + const getTableData = () => { + return [{ + id: 1, + date: '2016-05-02', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 1, + }, { + id: 2, + date: '2016-05-04', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 2, + }, { + id: 3, + date: '2016-05-01', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 3, + children: [{ + id: 31, + date: '2016-05-01', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 4, + children: [ + { + id: 311, + date: '2016-05-01', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 5, + }, + { + id: 312, + date: '2016-05-01', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 6, + }, + { + id: 313, + date: '2016-05-01', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 7, + disabled: true, + }, + ], + }, { + id: 32, + date: '2016-05-01', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 8, + }], + }, { + id: 4, + date: '2016-05-03', + name: 'Wangxiaohu', + address: '1518 Jinshajiang Road, Putuo District, Shanghai', + index: 9, + }] + } + + const createTable = function(methods) { + return mount( + Object.assign( + { + components: { + ElTable, + ElTableColumn, + }, + template: ` + + + + + + + + + + + + `, + methods: { + selectable(row) { + return !row.disabled + }, + ...methods, + }, + data() { + return { + testData: getTableData(), + } + }, + }, + ), + ) + } + + it('selectable index parameter should be correct', async() => { + const result = [] + const wrapper = createTable({ + selectable(row, index) { + result.push((row.index - 1) === index) + return !row.disabled + }, + }) + await nextTick() + wrapper.vm.$refs.table.toggleAllSelection() + expect(result.every(item => item)).toBeTruthy() + wrapper.unmount() + }) + + }) }) diff --git a/packages/table/src/store/watcher.ts b/packages/table/src/store/watcher.ts index 0a2c35888b..b49893d4ef 100644 --- a/packages/table/src/store/watcher.ts +++ b/packages/table/src/store/watcher.ts @@ -193,10 +193,13 @@ function useWatcher() { isAllSelected.value = value let selectionChanged = false + let childrenCount = 0 + const rowKey = instance?.store?.states?.rowKey.value data.value.forEach((row, index) => { + const rowIndex = index + childrenCount if (selectable.value) { if ( - selectable.value.call(null, row, index) && + selectable.value.call(null, row, rowIndex) && toggleRowStatus(selection.value, row, value) ) { selectionChanged = true @@ -206,6 +209,7 @@ function useWatcher() { selectionChanged = true } } + childrenCount += getChildrenCount(getRowIdentity(row, rowKey)) }) if (selectionChanged) { @@ -248,10 +252,13 @@ function useWatcher() { } let isAllSelected_ = true let selectedCount = 0 + let childrenCount = 0 for (let i = 0, j = (data.value || []).length; i < j; i++) { + const keyProp = instance?.store?.states?.rowKey.value + const rowIndex = i + childrenCount const item = data.value[i] const isRowSelectable = - selectable.value && selectable.value.call(null, item, i) + selectable.value && selectable.value.call(null, item, rowIndex) if (!isSelected(item)) { if (!selectable.value || isRowSelectable) { isAllSelected_ = false @@ -260,12 +267,30 @@ function useWatcher() { } else { selectedCount++ } + childrenCount += getChildrenCount(getRowIdentity(item, keyProp)) } if (selectedCount === 0) isAllSelected_ = false isAllSelected.value = isAllSelected_ } + // gets the number of all child nodes by rowKey + const getChildrenCount = (rowKey: string) => { + if (!instance || !instance.store) return 0 + const { + treeData, + } = instance.store.states + let count = 0 + const children = treeData.value[rowKey]?.children + if (children) { + count += children.length + children.forEach(childKey => { + count += getChildrenCount(childKey) + }) + } + return count + } + // 过滤与排序 const updateFilters = (columns, values) => { if (!Array.isArray(columns)) {