前端 element-UI table的多选id如何获得 代码演示
时间: 2023-06-02 16:07:21 浏览: 49
您好!关于前端 element-UI table 的多选id如何获得,您可以通过以下方法获取选中行的数据:
1. 在 table 标签上添加 selection-change 事件,例如:
```
<el-table
:data="tableData"
@selection-change="handleSelectionChange">
```
2. 在 methods 中定义 handleSelectionChange 方法,获取选中的数据,例如:
```
methods: {
handleSelectionChange(selection) {
this.selectedData = selection.map(item => item.id)
}
}
```
其中 selectedData 就是您需要的多选 id 数组。请将 handleSelectionChange 方法中的 id 属性替换为您实际使用的列名。
希望能够帮助到您!
相关问题
前端 element-UI table的多选id如何获得
你可以使用以下代码来获取 element-UI table 多选的 id:
```
<template>
<div>
<el-table
:data="tableData"
border
style="width: 100%">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
prop="name"
label="Name"
width="180">
</el-table-column>
<el-table-column
prop="age"
label="Age"
width="180">
</el-table-column>
</el-table>
<button @click="getIds">Get Selected Ids</button>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{
id: 1,
name: 'John',
age: 20
},
{
id: 2,
name: 'Mike',
age: 25
},
{
id: 3,
name: 'Amy',
age: 30
}
],
selectedIds: []
}
},
methods: {
getIds() {
this.selectedIds = this.$refs.table.selection.map(item => item.id)
console.log(this.selectedIds)
}
}
}
</script>
```
在这个例子中,我们使用 `$refs.table.selection` 来获取所有选中的行,然后通过映射函数获取每个选中行的 `id` 值,并将它们存储在 `selectedIds` 中。
element-ui table多选CheckBox参数解析
当使用 Element UI 的 table 组件进行多选时,可以在列定义中设置 `type` 属性为 `selection`,这将会在表格中显示出一个复选框列。当用户勾选了其中的某些行时,可以通过 `@selection-change` 事件来获取当前选中的行数据,如下所示:
```html
<el-table
:data="tableData"
@selection-change="handleSelectionChange">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
prop="name"
label="姓名">
</el-table-column>
<el-table-column
prop="age"
label="年龄">
</el-table-column>
</el-table>
```
```js
methods: {
handleSelectionChange(selection) {
console.log(selection)
}
}
```
在 `handleSelectionChange` 方法中,`selection` 参数是一个包含已选择行数据的数组。如果需要获取选中行的索引,可以在 `el-table` 上设置 `row-key` 属性,并在 `selection-change` 事件中通过 `selection.map(item => this.$refs.table.$data.indexOf(item))` 获取。