<template> <div> <el-checkbox v-model="isAllSelected" @change="handleSelectAll" >全选</el-checkbox > <div v-for="(tableData, index) in tableList" :key="index"> <el-checkbox v-model="selectedTables[index]" @change="handleTableSelect(index)" > {{ tableData.tableName }} </el-checkbox> <el-table :data="tableData.list" > <el-table-column type="selection"></el-table-column> <!-- 表格其他列 --> <el-table-column label="名称" align="center" prop="name" /> <el-table-column label="code" align="center" prop="code" /> </el-table> </div> </div> </template> 加勾选事件
时间: 2023-10-11 09:08:25 浏览: 103
Vue动态生成el-checkbox点击无法赋值的解决方法
5星 · 资源好评率100%
在这段代码中,我们可以为每个el-table添加一个选中状态,然后在全选按钮选中时,将所有el-table的选中状态都设置为true,并且将每一行数据的选中状态设置为true;在全选按钮取消选中时,将所有el-table的选中状态都设置为false,并且将每一行数据的选中状态设置为false。具体的实现如下:
```
<template>
<div>
<el-checkbox v-model="isAllSelected" @change="handleSelectAll">全选</el-checkbox>
<div v-for="(tableData, index) in tableList" :key="index">
<el-checkbox v-model="tableSelected[index]" @change="handleTableSelect(index)">
{{ tableData.tableName }}
</el-checkbox>
<el-table :data="tableData.list">
<el-table-column type="selection"></el-table-column>
<el-table-column label="名称" align="center" prop="name" />
<el-table-column label="code" align="center" prop="code" />
</el-table>
</div>
</div>
</template>
<script>
export default {
data() {
return {
isAllSelected: false,
tableSelected: [],
tableList: [
{
tableName: "Table1",
list: [
{ name: "A", code: "A1" },
{ name: "B", code: "B1" }
]
},
{
tableName: "Table2",
list: [
{ name: "C", code: "C1" },
{ name: "D", code: "D1" }
]
}
]
};
},
methods: {
handleSelectAll(val) {
this.isAllSelected = val;
if (val) {
this.tableSelected = this.tableList.map(() => true);
this.tableList.forEach(tableData => {
tableData.list.forEach(item => {
item.isSelected = true;
});
});
} else {
this.tableSelected = this.tableList.map(() => false);
this.tableList.forEach(tableData => {
tableData.list.forEach(item => {
item.isSelected = false;
});
});
}
},
handleTableSelect(index) {
this.tableSelected[index] = !this.tableSelected[index];
this.tableList[index].list.forEach(item => {
item.isSelected = this.tableSelected[index];
});
this.isAllSelected = this.tableSelected.every(val => val);
}
},
created() {
this.tableSelected = this.tableList.map(() => false);
this.tableList.forEach(tableData => {
tableData.list.forEach(item => {
item.isSelected = false;
});
});
}
};
</script>
```
在上面的代码中,我们为每个el-table添加了一个tableSelected选中状态数组,并在handleSelectAll方法中更新了它们的状态。同时,我们也为每一行数据添加了一个isSelected选中状态,并在handleTableSelect方法中更新了它们的状态。最后,我们在created钩子中初始化了所有的选中状态。
阅读全文