<template>
<el-table
ref="multipleTable"
:data="tableData"
style="width: 100%"
@select="selectFn"
@select-all="selectAllFn"
>
<el-table-column type="selection" width="55"> </el-table-column>
</el-table>
</template>
<script>
// 在一个对象数据中寻找某个对象的下标,通过key的值相等,判断数组中是否包含对象
const arrFindObjIndex = (arr, obj, key) => {
let index = -1;
arr.forEach((item, idx) => {
if (item[key] === obj[key]) {
index = idx;
}
});
return index;
};
export default {
data() {
return {
selection: [],
tableData: [],
};
},
methods: {
// 翻页之后调用该方法
toggleSelection() {
//翻页之后要勾选上一页选中的,增加延时,在列表渲染完成后选中
setTimeout(() => {
this.selection.forEach((item) => {
if (arrFindObjIndex(this.selection, item, "id") !== -1) {
this.$refs["multipleTable"].toggleRowSelection(item);
}
});
},500)
},
selectAllFn(selection) {
if (!selection.length) {
// 全选时,选中的数据为空时,要清理掉缓存中当页的数据
this.tableData.forEach((item) => {
const index = arrFindObjIndex(this.selection, item, "id");
if (index !== -1) {
this.selection.splice(index, 1);
}
});
} else {
// 全选时,选中的数据不为空时,要在缓存中添加当页的数据
selection.forEach((item) => {
const index = arrFindObjIndex(this.selection, item, "id");
if (index === -1) {
this.selection.push(item);
}
});
}
},
selectFn(selection, row) {
const flag = selection.some((item) => {
return item.id === row.id;
});
const index = arrFindObjIndex(this.selection, row, "id");
if (!flag) {
// selection中不包含row则是取消选中该行
if (index !== -1) {
this.selection.splice(index, 1);
}
} else {
// selection中不包含row则是选中该行
if (index === -1) {
this.selection.push(row);
}
}
},
},
};
</script>
ElementUi多选框表格,翻页选中有记忆功能保持默认选中
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 第一步:给表格添加 row-key 关键点::row-key="(row) => row.id" 第二步:给多选框...
- data(){ return{ listData:[],//当前的table的数据 hasSelectL...
- 切换数据的时候,需要比较data中的用户名是否相同,并且把相同的用户名勾选上,如何实现切换数据后依然保持原有的选中...
- 场景描述: element-ui的table表格数据有提供多选框的功能,只要在表格列中多加一列就可以选择表格的数据...
- 今天青石的票圈出镜率最高的,莫过于张艺谋的新片终于定档了。 一张满溢着水墨风的海报一次次的出现在票圈里,也就是老谋...