同步6/16
This commit is contained in:
@@ -28,8 +28,7 @@
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- 关键:去掉 @selection-change,改用 row-click 实现单选,永不报错! -->
|
||||
<el-table ref="multipleTableRef" :data="tableData" border @row-click="handleRowClick" @selection-change="handleSelectionChange">
|
||||
<el-table ref="multipleTableRef" row-key="id" :data="tableData" border @selection-change="handleSelectionChange">
|
||||
<el-table-column type="selection" width="55" align="center" />
|
||||
<el-table-column label="序号" align="center" width="55" type="index" />
|
||||
<el-table-column label="姓名" align="center" prop="name" />
|
||||
@@ -38,6 +37,11 @@
|
||||
<dict-tag :options="com_resident_type" :value="row.type" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="与业主关系" align="center" width="95" prop="ownerRelationship">
|
||||
<template #default="{ row }">
|
||||
<dict-tag :options="com_resident_relationship" :value="row.ownerRelationship" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="状态" align="center" width="85" prop="status">
|
||||
<template #default="{ row }">
|
||||
<dict-tag :options="com_certification_status" :value="row.status" />
|
||||
@@ -45,7 +49,7 @@
|
||||
</el-table-column>
|
||||
<el-table-column label="证件号码" align="center" prop="idCard" />
|
||||
<el-table-column label="手机号码" align="center" prop="phone" />
|
||||
<el-table-column label="房间" align="center">
|
||||
<el-table-column label="房间" show-overflow-tooltip align="center">
|
||||
<template #default="{ row }">
|
||||
{{ row.housePath }}
|
||||
</template>
|
||||
@@ -75,7 +79,7 @@
|
||||
import { listResident } from '@/api/system/resident';
|
||||
import { ResidentVO } from '@/api/system/resident/type';
|
||||
import { getHousePathById } from '@/utils/house';
|
||||
import { ref, watch, nextTick, getCurrentInstance, ComponentInternalInstance, toRefs } from 'vue';
|
||||
import { ref, nextTick, getCurrentInstance, ComponentInternalInstance, toRefs } from 'vue';
|
||||
import { useHouseStore } from '@/store/modules/house';
|
||||
|
||||
const houseStore = useHouseStore();
|
||||
@@ -111,7 +115,7 @@ const props = defineProps<{
|
||||
|
||||
// ✅ 手动设置默认值(替代 withDefaults)
|
||||
const isMultiple = computed(() => props.isMultiple ?? false);
|
||||
const returnvalue = computed(() => props.returnvalue ?? 'id'); // 👈 默认 userId
|
||||
const returnvalue = computed(() => props.returnvalue ?? '*'); // 👈 默认 userId
|
||||
|
||||
const { userid } = toRefs(props);
|
||||
|
||||
@@ -140,50 +144,66 @@ const total = ref(0);
|
||||
const loading = ref(false);
|
||||
|
||||
// ==============================================
|
||||
// ✅ 新增:处理 selection-change(多选专用)
|
||||
// ✅ 优化:处理 selection-change
|
||||
// ==============================================
|
||||
function handleSelectionChange(selection: tableType[]) {
|
||||
if (isMultiple.value) {
|
||||
multipleSelection.value = selection;
|
||||
}
|
||||
}
|
||||
// 多选逻辑:合并当前页选中项和之前页选中项
|
||||
const currentSelectedIds = new Set(selection.map((item) => item.id));
|
||||
|
||||
// ==============================================
|
||||
// 最佳单选方案:点击行选中,无报错、无循环、最稳定
|
||||
// ==============================================
|
||||
function handleRowClick(row) {
|
||||
if (isMultiple.value) {
|
||||
// 多选:点击行切换选中状态
|
||||
const isSelected = multipleSelection.value.some((item) => item.id === row.id);
|
||||
if (isSelected) {
|
||||
multipleSelection.value = multipleSelection.value.filter((item) => item.id !== row.id);
|
||||
multipleTableRef.value?.toggleRowSelection(row, false);
|
||||
} else {
|
||||
multipleSelection.value.push(row);
|
||||
multipleTableRef.value?.toggleRowSelection(row, true);
|
||||
}
|
||||
// 过滤掉当前页未选中的旧数据
|
||||
const otherPageSelections = multipleSelection.value.filter((item) => {
|
||||
const isCurrentPageItem = tableData.value.some((row) => row.id === item.id);
|
||||
if (isCurrentPageItem) {
|
||||
return currentSelectedIds.has(item.id);
|
||||
}
|
||||
return true; // 保留其他页的数据
|
||||
});
|
||||
|
||||
// 合并当前页新选中的数据
|
||||
const finalSelection = [...otherPageSelections, ...selection];
|
||||
|
||||
// 去重
|
||||
const uniqueMap = new Map();
|
||||
finalSelection.forEach((item) => uniqueMap.set(item.id, item));
|
||||
multipleSelection.value = Array.from(uniqueMap.values());
|
||||
} else {
|
||||
// 单选:原有逻辑保持不变
|
||||
multipleTableRef.value?.clearSelection();
|
||||
multipleTableRef.value?.toggleRowSelection(row, true);
|
||||
multipleSelection.value = [row];
|
||||
// 单选逻辑:始终只保留最后一个选中的项
|
||||
if (selection.length > 0) {
|
||||
// 获取最后选中的那一项
|
||||
const lastSelected = selection[selection.length - 1];
|
||||
multipleSelection.value = [lastSelected];
|
||||
|
||||
// 关键:如果 selection 长度大于1(比如之前选了一个,现在又选一个,el-table可能暂时上报两个),
|
||||
// 我们需要手动清除表格中其他的勾选状态,只保留最后一个
|
||||
nextTick(() => {
|
||||
if (selection.length > 1) {
|
||||
multipleTableRef.value?.clearSelection();
|
||||
multipleTableRef.value?.toggleRowSelection(lastSelected, true);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
multipleSelection.value = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==============================================
|
||||
// ✅ 修改:支持多选回显
|
||||
// ==============================================
|
||||
// ✅ 优化:回显逻辑
|
||||
const handleUserSelection = () => {
|
||||
if (!userid.value || !tableData.value.length) return;
|
||||
if (!multipleTableRef.value) return;
|
||||
|
||||
// 先清空所有选择
|
||||
multipleTableRef.value.clearSelection();
|
||||
multipleSelection.value = [];
|
||||
|
||||
if (!props.userid && props.userid !== 0) return;
|
||||
|
||||
nextTick(() => {
|
||||
multipleTableRef.value?.clearSelection();
|
||||
|
||||
if (isMultiple.value && Array.isArray(userid.value)) {
|
||||
if (isMultiple.value && Array.isArray(props.userid)) {
|
||||
// 多选回显
|
||||
const selectedItems: tableType[] = [];
|
||||
userid.value.forEach((id) => {
|
||||
const findItem = tableData.value.find((item) => item.userId == id);
|
||||
props.userid.forEach((id) => {
|
||||
const findItem = tableData.value.find((item) => item.id == id || item.userId == id);
|
||||
if (findItem) {
|
||||
selectedItems.push(findItem);
|
||||
multipleTableRef.value?.toggleRowSelection(findItem, true);
|
||||
@@ -191,38 +211,43 @@ const handleUserSelection = () => {
|
||||
});
|
||||
multipleSelection.value = selectedItems;
|
||||
} else {
|
||||
// 单选回显(原有逻辑)
|
||||
const findItem = tableData.value.find((item) => item.id == userid.value);
|
||||
// 单选回显
|
||||
// 注意:props.userid 可能是单个 ID
|
||||
const targetId = Array.isArray(props.userid) ? props.userid[0] : props.userid;
|
||||
const findItem = tableData.value.find((item) => item.id == targetId || item.userId == targetId);
|
||||
|
||||
if (findItem) {
|
||||
multipleSelection.value = [findItem];
|
||||
multipleTableRef.value?.toggleRowSelection(findItem, true);
|
||||
multipleSelection.value = [findItem];
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
watch(userid, handleUserSelection, { immediate: true });
|
||||
watch(tableData, handleUserSelection);
|
||||
|
||||
// 打开选择框
|
||||
function choiceResidentFun() {
|
||||
// 初始化
|
||||
getlist();
|
||||
dialog.value.dialogVisible = true;
|
||||
}
|
||||
|
||||
// 获取列表
|
||||
function getlist() {
|
||||
loading.value = true;
|
||||
listResident(queryParams.value).then((res) => {
|
||||
tableData.value = res.rows;
|
||||
tableData.value.forEach((item) => {
|
||||
if (item.houseId !== null) {
|
||||
const houselist = getHousePathById(treedata.value, item.houseId, 'label');
|
||||
item.housePath = houselist?.join('-') || '';
|
||||
}
|
||||
listResident(queryParams.value)
|
||||
.then((res) => {
|
||||
tableData.value = res.rows;
|
||||
tableData.value.forEach((item) => {
|
||||
if (item.houseId !== null) {
|
||||
const houselist = getHousePathById(treedata.value, item.houseId, 'label');
|
||||
item.housePath = houselist?.join('-') || '';
|
||||
}
|
||||
});
|
||||
handleUserSelection();
|
||||
total.value = res.total;
|
||||
})
|
||||
.finally(() => {
|
||||
loading.value = false;
|
||||
});
|
||||
total.value = res.total;
|
||||
loading.value = false;
|
||||
});
|
||||
}
|
||||
|
||||
// 重置查询
|
||||
@@ -268,6 +293,7 @@ function confirm() {
|
||||
}
|
||||
} else {
|
||||
// 单选
|
||||
console.log(multipleSelection.value[0], multipleSelection.value);
|
||||
if (returnvalue.value === '*') {
|
||||
// ✅ 返回整个对象
|
||||
emit('change', multipleSelection.value[0]);
|
||||
@@ -276,13 +302,21 @@ function confirm() {
|
||||
emit('change', multipleSelection.value[0][returnvalue.value as keyof tableType]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
emit('change', null);
|
||||
}
|
||||
}
|
||||
// 初始化
|
||||
getlist();
|
||||
|
||||
defineExpose({
|
||||
open: () => {
|
||||
open: (arr) => {
|
||||
if (arr && arr.length > 0) {
|
||||
multipleSelection.value = arr.map((id) => ({
|
||||
id: id
|
||||
}));
|
||||
} else {
|
||||
multipleSelection.value = [];
|
||||
}
|
||||
getlist();
|
||||
dialog.value.dialogVisible = true;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user