element-ui中table組件的toggleRowSelection()方法使用
element-ui中table組件的toggleRowSelection()方法
最近,在做關(guān)于翻頁導(dǎo)出功能時(shí),遇到需要將前面勾選過的選項(xiàng)進(jìn)行回顯的情況,因?yàn)閠able組件在每次翻頁的時(shí)候,都會(huì)清空上一頁勾選的選項(xiàng),在切換回前一頁時(shí),勾選過的選項(xiàng)不會(huì)保存。因此需要借助toggleRowSelection()方法設(shè)置勾選項(xiàng)。
toggleRowSelection()需要頁面渲染完畢之后才有效,因此需要放在this.$nextTick中,如果只有一頁數(shù)據(jù),這樣就可以了,但是在有多頁數(shù)據(jù)的情況下,每次翻頁就會(huì)請(qǐng)求數(shù)據(jù),進(jìn)行數(shù)據(jù)更新,因此this.$nextTick需要放在updated回調(diào)中,等數(shù)據(jù)更新和頁面渲染都完成時(shí),才有效。如下所示。
updated() {
this.$nextTick(() => {
this.tableData.forEach(outerItem => {
this.selectRows[this.page.current - 1] && this.selectRows[this.page.current - 1].forEach(item => {
if(outerItem.d == item.d) {
this.$refs.caseTable.toggleRowSelection(outerItem,true);
}
})
})
})
}還有一個(gè)問題,如上所示,tableData中是傳入到table組件中的數(shù)據(jù),而selectRows是我保存的勾選的數(shù)據(jù),按理說數(shù)據(jù)的格式完全一樣,但是我向toggleRowSelection傳遞參數(shù)時(shí),只能傳遞outerItem,也就是傳入table組件的數(shù)據(jù),傳入item則無效。具體原因還不是太清楚,但我想應(yīng)該是因?yàn)閮蓚€(gè)引用類型的地址不同引起的。
element-ui中table默認(rèn)選中toggleRowSelection
一.toggleRowSelection
toggleRowSelection(row, selected)接受兩個(gè)參數(shù),row傳遞被勾選行的數(shù)據(jù),selected設(shè)置是否選中
注意:調(diào)用toggleRowSelection這個(gè)方法 需要獲取真實(shí)dom 所以需要注冊(cè) ref 來引用它
二.操作
(一).默認(rèn)選中
1.當(dāng)數(shù)據(jù)源固定在table的
<script>
export default {
mounted() {
this.$refs.multipleTable.toggleRowSelection(this.tableData3[2],true);
}
}
</script>
(二).點(diǎn)擊tr選中
1.在table中設(shè)置 @row-click="handleCurrentChange"
row-click 點(diǎn)擊行事件
<template>
<el-table :data="tableData3" ref="multipleTable" @row-click="handleCurrentChange">
</el-table>
</template>
<script>
export default {
methods: {
handleCurrentChange(row, event, column){
this.$refs.multipleTable.toggleRowSelection(row,true);//點(diǎn)擊選中
}
}
}
</script>(三).獲取選中的值
1.設(shè)置table 中@selection-change="selsChange"
2.data 中設(shè)置sels:[]
<template>
<el-table :data="tableData3" ref="multipleTable" @row-click="handleCurrentChange" @selection-change="selsChange">
</el-table>
</template>
<script>
export default {
methods: {
selsChange( val){
this.sels=val;
console.log(this.sels)
}
}
}
</script>三.案例
1.table tr 點(diǎn)擊 復(fù)選框選中 再次點(diǎn)擊 復(fù)選框取消選中
①設(shè)置一個(gè)全局函數(shù)
exports.install = function (Vue, options) {
//刪除數(shù)組 指定的元素
Vue.prototype.removeByValue=function(arr, val){
for(var i=0; i<arr.length; i++) {
if(arr[i] == val) {
arr.splice(i, 1);
break;
}
}
};
};②table.vue
<template>
<el-table :data="tableData3" ref="multipleTable" @row-click="handleCurrentChange" @selection-change="selsChange">
<el-table-column type="selection" width="55" ></el-table-column>
<el-table-column type="index" label="序號(hào)" width="60"></el-table-column>
<el-table-column prop="sex" label="性別" width="100" :formatter="formatSex"></el-table-column>
<el-table-column prop="date" label="日期"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData3: [{
id:'1',
date: '2016-05-03',
name: '嘎哈和',
address: '上海市普陀區(qū)金沙江路 1518 弄',
sex:'1'
}, {
id:'2',
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
sex:'0'
}, {
id:'3',
date: '2016-05-02',
name: '莫默模',
address: '上海市普陀區(qū)金沙江路 1518 弄',
sex:'-1'
}],
arrID:[],
}
},
methods: {
formatSex: function (row, column, cellValue, index) {
return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
},
// 點(diǎn)擊事情
handleCurrentChange(row, event, column){
var same=false;
if(this.arrID.length > 0){
for(var i=0; i<this.arrID.length ;i++){
if(this.arrID[i]==row.id){
same=true;
this.removeByValue(this.arrID, row.id);
break;
}
}
if(same==true){
this.$refs.multipleTable.toggleRowSelection(row,false);
}else{
this.$refs.multipleTable.toggleRowSelection(row,true);
this.arrID.push(row.id);
}
}else{
this.$refs.multipleTable.toggleRowSelection(row,true);
this.arrID.push(row.id);
}
},
selsChange(val){
var valId=[];
for(var i=0;i<val.length;i++){
var arrIDsame=false;
valId.push(val[i].id);
}
this.arrID=valId;
}
},
mounted() {
this.$refs.multipleTable.toggleRowSelection(this.tableData3[2],true);//默認(rèn)選中
}
}
</script>
<style>
</style>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在vant 中使用cell組件 定義圖標(biāo)該圖片和位置操作
這篇文章主要介紹了在vant 中使用cell組件 定義圖標(biāo)該圖片和位置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)
最近在項(xiàng)目中需要對(duì)數(shù)據(jù)進(jìn)行可視化處理,而眾所周知echarts是非常強(qiáng)大的插件,下面這篇文章主要給大家介紹了關(guān)于Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)的相關(guān)資料,需要的朋友可以參考下2022-05-05
詳解在Vue中如何使用axios跨域訪問數(shù)據(jù)
本篇文章主要介紹了在Vue中如何使用axios跨域訪問數(shù)據(jù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
vue.js實(shí)現(xiàn)單選框、復(fù)選框和下拉框示例
本篇文章主要介紹了vue.js實(shí)現(xiàn)單選框、復(fù)選框和下拉框示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
vue實(shí)現(xiàn)點(diǎn)擊按鈕“查看詳情”彈窗展示詳情列表操作
這篇文章主要介紹了vue實(shí)現(xiàn)點(diǎn)擊按鈕“查看詳情”彈窗展示詳情列表操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue頁面渲染數(shù)組中數(shù)據(jù)文案后添加逗號(hào)最后不加
這篇文章主要為大家介紹了vue頁面渲染數(shù)組中數(shù)據(jù)文案后添加逗號(hào)最后不加逗號(hào)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
記一次Vue中$route序列號(hào)報(bào)錯(cuò)
本文主要介紹了記一次Vue中$route序列號(hào)報(bào)錯(cuò),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

