需求:對el-table 選中行時復選框也被選中咆贬,選中復選框時觸發(fā)行的相應變化
(攏共分兩步)步驟:第一步:點擊行時觸發(fā)復選框的選擇或取消; 第二步:點擊復選框時觸發(fā)相應行的變化(問題關鍵在怎么獲取復選框選取的行)
1. 點擊行時觸發(fā)復選框的選擇或取消
// <template>
<el-table
class="right-panel-table"
:data="tableData"
@selection-change="(val) => handleSelectionChange(val,'ischeckboxTick')"
ref="multipleTable"
v-loading="planLoading"
@cell-mouse-enter="handleMouseEnter"
@cell-mouse-leave="handleMouseOut"
@row-click="(row,column,event) => handleRowClick(row,column,event,'onclumn')"
highlight-current-row
>
// <script> 添加row-click方法,點擊列表行時觸發(fā)
handleRowClick(row,column,event, onclumn){
if(onclumn === 'onclumn') {
this.isonClunm = true;
}
row.flag = !row.flag;
this.$refs.multipleTable.toggleRowSelection(row, row.flag);
if(row.flag) {
this.selectSatitleLatLon(row);
} else {
this.unselectSatitleLatLon(row);
}
}
添加完以上代碼后,點擊行阅羹,相應行前面的復選框會跟著選中或取消
2.點擊復選框時觸發(fā)相應行的變化(問題關鍵在怎么獲取復選框選取的行)
當點擊列表行時暑竟,先觸發(fā)row-click事件床蜘,然后再觸發(fā) selection-change事件晰洒,點擊復選框時只觸發(fā)selection-change事件,想要判斷出點擊復選框時觸發(fā)的是哪一行時糊闽,則需要對上一次選中的列表項和這次選中的列表項對比
// template部分同上
// <script>
handleSelectionChange(val, checkFlag) {
let that = this;
if(checkFlag === 'ischeckboxTick') {
that.ischeckboxTick = true
}
// 獲取當前增加的或者減少的那條數(shù)據(jù)
let n = {};
let currentVal = [];
val.forEach(item => {
currentVal.push(item);
})
if(val.length > that.multipleSelection.length) {
// 增加時比之前不一樣的某個值
n = (val.filter(item => !that.multipleSelection.includes(item)))[0];
} else {
// 減少時與之前不一樣的值
let oldSelect = [];
that.multipleSelection.forEach(item => {
oldSelect.push(item)
})
if(val.length === 0) {
n = that.multipleSelection[0];
} else {
that.multipleSelection.forEach((item, index) => {
let delIndex = 0;
currentVal.forEach((item1, index1) => {
if(item.sname === item1.sname && item.startTime === item1.startTime && item.endTime === item1.endTime) {
delIndex = index;
currentVal.splice(index1, 1);
oldSelect.splice(delIndex, 1, '');
}
})
})
oldSelect.forEach(item => {
if(item !== '') {
n = item
}
})
}
}
that.multipleSelection = val; // 選中的所有項
that.isonClunm = false;
that.ischeckboxTick = false;
},