el-table 获取选择行的 案例
时间: 2023-10-17 14:22:13 浏览: 50
以下是一个示例代码,演示如何在 element-ui 的 el-table 组件中获取当前选中的行数据:
```html
<template>
<el-table ref="table" :data="tableData" @selection-change="handleSelectionChange">
<el-table-column type="selection"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="gender" label="性别"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '小明', age: 18, gender: '男' },
{ name: '小红', age: 20, gender: '女' },
{ name: '小刚', age: 22, gender: '男' },
{ name: '小丽', age: 24, gender: '女' }
],
selectedRows: []
}
},
methods: {
handleSelectionChange(selection) {
this.selectedRows = selection;
}
}
}
</script>
```
在上述代码中,我们使用了 element-ui 的 el-table 组件,并且在该组件中添加了一个 type="selection" 的列,表示该列为选中列。我们在 el-table 标签上通过 @selection-change 监听事件,在事件处理函数 handleSelectionChange 中获取当前选中的行数据,然后将其存储在 selectedRows 中。
在 Vue.js 组件中,我们可以通过 $refs 来访问 el-table 组件,然后调用 getSelection 方法来获取当前选中的行数据。例如:
```js
const selectedRows = this.$refs.table.getSelection();
```
通过以上代码,我们可以在 Vue.js 中获取到当前选中的行数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)