<template>
<div>
<el-button @click="handleOpen">open</el-button>
<el-table :data="outerList" style="width: 400px">
<el-table-column prop="id" label="id" />
<el-table-column prop="name" label="name" />
</el-table>
<el-dialog v-model="dialogVisible" title="Tips" width="500">
<el-table
ref="multipleTableRef"
v-loading="loading"
:data="tableData"
style="width: 100%"
@select="handleSingleSelect"
@select-all="handleAllSelect"
>
<el-table-column type="selection" width="55" />
<el-table-column prop="id" label="id" />
<el-table-column prop="name" label="name" />
</el-table>
<el-button @click="getList(1)">1</el-button>
<el-button @click="getList(2)">2</el-button>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="handleConfirm">
Confirm
</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup>
import { nextTick, ref } from "vue";
const loading = ref(false);
const multipleTableRef = ref();
const dialogVisible = ref(false);
// 外层表哥数据
const outerList = ref([
{ id: 1, name: "1" },
{ id: 2, name: "2" },
]);
// checkbox已选数据
const chosenList = ref([]);
// 里层表哥数据
const tableData = ref([]);
const handleOpen = () => {
dialogVisible.value = true;
chosenList.value = JSON.parse(JSON.stringify(outerList.value));
getList(1);
};
// 获取列表数据
const getList = (page) => {
loading.value = true;
setTimeout(() => {
if (page === 1) {
tableData.value = [
{ id: 1, name: "1" },
{ id: 2, name: "2" },
];
} else {
tableData.value = [
{ id: 3, name: "3" },
{ id: 4, name: "4" },
];
}
loading.value = false;
// 等待tableData.value被赋值,DOM更新后再设置默认勾选
nextTick(() => {
toggleSelection(tableData.value);
});
}, 500);
};
const toggleSelection = (rows) => {
if (!rows) return multipleTableRef.value.clearSelection();
rows.forEach((row) => {
let has = chosenList.value.some((it) => it.id === row.id);
if (has) {
multipleTableRef.value.toggleRowSelection(row, true);
} else {
multipleTableRef.value.toggleRowSelection(row, false);
}
});
};
const handleSingleSelect = (selection, row) => {
// 是否选中/取消选中
let isAddRow = selection.some((it) => it.id === row.id);
// 取消选中
if (!isAddRow) {
// 移除之前选中的当前页的所有数据
let index = chosenList.value.findIndex((it) => it.id === row.id);
if (index > -1) chosenList.value.splice(index, 1);
return;
}
// 有值就是选中 且 没添加过则添加
for (let i of selection) {
let has = chosenList.value.some((it) => it.id === i.id);
if (!has) chosenList.value.push(i);
}
};
const handleAllSelect = (selection) => {
// 有值就是全选
if (selection.length) {
// 没添加过此条数据则添加
for (let i of selection) {
let has = chosenList.value.some((it) => it.id === i.id);
if (!has) chosenList.value.push(i);
}
}
// 清空
else {
// 循环当前列表数据,删除之前添加过的每一项
for (let i of tableData.value) {
let index = chosenList.value.findIndex((it) => it.id === i.id);
if (index > -1) chosenList.value.splice(index, 1);
}
}
};
// 确认
const handleConfirm = () => {
outerList.value = chosenList.value;
dialogVisible.value = false;
};
</script>
el-table selection 分页切换/默认勾选
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原以为js中即便是学到了map可实际上也不会用到map,可我今天就遇到一个el-table分页查询,然后需要勾选表...
- 设置 reserve-selection 为 true,这样才能存储翻页后选中的数据 reserve-select...
- 前言 最近一直在跟表格里的多选框做斗争,一开始觉得el-table本身的多选框不满足我的需求,想要自定义el-ch...
- 分页多选,分页进行切换的时候,ids存储 <el-table height="calc(100vh - 280px...
- 背景介绍:是一个寄样登记的表格,初始进来是空的,在右上角有一个添加寄样明细的按钮,点了添加寄样明细之后,会弹出一个...