vue 怎么拿到table表格的某一行元素
时间: 2024-03-02 15:51:51 浏览: 86
可以通过以下步骤拿到表格的某一行元素:
1. 在表格中给每一行元素绑定一个唯一的标识符,例如 `id` 或者 `key`。
2. 在 Vue 组件中使用 `ref` 属性获取到表格元素,例如 `<el-table ref="table">`。
3. 在 Vue 组件的方法中,通过 `this.$refs.table.$el.querySelectorAll('tbody tr')` 获取到所有行元素。
4. 遍历所有行元素,通过 `getAttribute('row-key')` 获取到该行元素的标识符,并与需要获取的行元素的标识符进行比较。
5. 找到需要获取的行元素后,可以通过 `querySelector` 方法获取到该行元素中的其他元素。
以下是一个示例代码:
```html
<template>
<el-table ref="table" :data="tableData" :row-key="row => row.id">
<el-table-column prop="name" label="Name"></el-table-column>
<el-table-column prop="age" label="Age"></el-table-column>
<el-table-column prop="gender" label="Gender"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ id: 1, name: 'John', age: 28, gender: 'Male' },
{ id: 2, name: 'Jane', age: 24, gender: 'Female' },
{ id: 3, name: 'Bob', age: 32, gender: 'Male' },
],
};
},
methods: {
getTableRow(id) {
const rows = this.$refs.table.$el.querySelectorAll('tbody tr');
for (let i = 0; i < rows.length; i++) {
const rowId = rows[i].getAttribute('row-key');
if (rowId === id.toString()) {
// 找到需要获取的行元素
const nameElement = rows[i].querySelector('.el-table_1_column_1');
const ageElement = rows[i].querySelector('.el-table_1_column_2');
const genderElement = rows[i].querySelector('.el-table_1_column_3');
console.log('Name:', nameElement.textContent);
console.log('Age:', ageElement.textContent);
console.log('Gender:', genderElement.textContent);
break;
}
}
},
},
};
</script>
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)