问题:
el-table中实现单选,选中一个其他的取消选中
代码:
javascript
<template>
<div>
<el-table
:data="tableData"
@selection-change="handleSelectionChange"
ref="singleTable"
highlight-current-row
>
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
prop="name"
label="姓名">
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [{
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
name: '张三',
address: '上海市普陀区金沙江路 1517 弄'
}],
selectedRow: null // 用于存储当前选中的行
};
},
methods: {
handleSelectionChange(selection) {
if (Array.isArray(selection) && selection.length > 1) {//点击勾选框
this.$refs.singleTable.toggleRowSelection(selection[0],false);
this.$refs.singleTable.toggleRowSelection(selection[1],true);
this.selectedRow = selection[1];
}else if (Array.isArray(selection) && selection.length === 1){
this.selectedRow = selection[0];
}else {
this.selectedRow = null;
}
},
}
};
</script>```