Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component): [table] can't select row children #10221

Merged
merged 19 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/components/table/__tests__/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,4 +1506,52 @@ describe('Table.vue', () => {
'min-width: 0'
)
})
it('selectable tree', async () => {
const wrapper = mount({
components: {
ElTable,
ElTableColumn,
},
template: `
<el-table :data="testData" @selection-change="change">
<el-table-column type="selection" />
<el-table-column prop="name" label="name" />
<el-table-column prop="release" label="release" />
<el-table-column prop="director" label="director" />
<el-table-column prop="runtime" label="runtime" />
</el-table>
`,
data() {
const testData = getTestData() as any
testData[1].children = [
{
name: "A Bug's Life copy 1",
release: '1998-11-25-1',
director: 'John Lasseter',
runtime: 95,
},
{
name: "A Bug's Life copy 2",
release: '1998-11-25-2',
director: 'John Lasseter',
runtime: 95,
},
]
return {
testData,
selected: [],
}
},

methods: {
change(rows) {
this.selected = rows
},
},
})
await doubleWait()
wrapper.findAll('.el-checkbox')[2].trigger('click')
await doubleWait()
expect(wrapper.vm.selected.length).toEqual(3)
})
})
14 changes: 12 additions & 2 deletions packages/components/table/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { createPopper } from '@popperjs/core'
import { flatMap, get } from 'lodash-unified'
import escapeHtml from 'escape-html'
import { hasOwn, throwError } from '@element-plus/utils'
import { hasOwn, isArray, isBoolean, throwError } from '@element-plus/utils'
import { useZIndex } from '@element-plus/hooks'
import type {
IPopperOptions,
Expand Down Expand Up @@ -251,14 +251,24 @@ export function toggleRowStatus<T>(

const addRow = () => {
statusArr.push(row)
if (isArray(row.children)) {
row.children.forEach((item) => {
toggleRowStatus(statusArr, item, newVal ?? !included)
})
}
changed = true
}
const removeRow = () => {
statusArr.splice(index, 1)
if (isArray(row.children)) {
row.children.forEach((item) => {
toggleRowStatus(statusArr, item, newVal ?? !included)
})
}
changed = true
}
faga295 marked this conversation as resolved.
Show resolved Hide resolved

if (typeof newVal === 'boolean') {
if (isBoolean(newVal)) {
if (newVal && !included) {
addRow()
} else if (!newVal && included) {
Expand Down