树形结构问题更改

This commit is contained in:
王兴凯 2026-05-07 16:06:05 +08:00
commit 3418e34766
2 changed files with 66 additions and 9 deletions

View File

@ -219,7 +219,7 @@ const isFishDataLoaded = ref(false)
async function openFishway(row: any) { async function openFishway(row: any) {
fishway.value = true fishway.value = true
userId.value = row.id userId.value = row.id
treeInput.value = ''
// //
fishhui.value = [] fishhui.value = []
tableDatafish.value = [] tableDatafish.value = []
@ -329,6 +329,13 @@ const fishProps = {
children: 'children', children: 'children',
label: 'name', label: 'name',
} }
watch(treeInput, (val) => {
fishTreeRef.value!.filter(val)
})
function filterNode(value: string, data: any) {
if (!value) return true
return data.name.includes(value)
}
const fishData: any = ref([]) const fishData: any = ref([])
// //
// const huixiandata = ref([]) // const huixiandata = ref([])
@ -338,7 +345,7 @@ function getFishTree() {
fishTreeDialog.value = true fishTreeDialog.value = true
const params = { const params = {
// type:'1', // type:'1',
engName: treeInput.value // engName: treeInput.value
} }
return getFishtree(params).then((res: any) => { return getFishtree(params).then((res: any) => {
if (res.code == 0) { if (res.code == 0) {
@ -1078,7 +1085,7 @@ function handleClearSelection() {
<el-input v-model="treeInput" style="width: 100%" placeholder="请输入电站名称" clearable <el-input v-model="treeInput" style="width: 100%" placeholder="请输入电站名称" clearable
class="input-with-select"> class="input-with-select">
<template #append> <template #append>
<el-button :icon="Search" @click="getFishTree()" /> <el-button :icon="Search" />
</template> </template>
</el-input> </el-input>
<div class="button_div"> <div class="button_div">
@ -1089,9 +1096,9 @@ function handleClearSelection() {
</div> </div>
<el-scrollbar height="450px" style="margin-top: 10px;"> <el-scrollbar height="450px" style="margin-top: 10px;">
<el-tree ref="fishTreeRef" style="max-width: 300px" :data="fishData" show-checkbox <el-tree ref="fishTreeRef" style="max-width: 300px" :data="fishData" show-checkbox :filter-node-method="filterNode"
v-loading="fishTreeDialog" node-key="code" highlight-current v-loading="fishTreeDialog" node-key="code" highlight-current :props="fishProps"
:props="fishProps" @check="handleFishCheckChange" /> @check="handleFishCheckChange" />
</el-scrollbar> </el-scrollbar>
</div> </div>
<div class="fishTable"> <div class="fishTable">

View File

@ -296,6 +296,7 @@ const fishTreeDialog = ref(false)
const isFishDataLoaded = ref(false) const isFishDataLoaded = ref(false)
async function openFishway(row: any) { async function openFishway(row: any) {
treeInput.value = ''
fishway.value = true fishway.value = true
userId.value = row.id userId.value = row.id
@ -356,6 +357,12 @@ watch([fishway, isFishDataLoaded], ([newFishway, newDataLoaded]) => {
// //
setTimeout(() => { setTimeout(() => {
fishTreeRef.value.setCheckedKeys(fishhui.value, false); fishTreeRef.value.setCheckedKeys(fishhui.value, false);
//
const parentCodesToExpand = findParentCodesForCheckedNodes(fishhui.value, fishData.value);
console.log('需要展开的父节点codes:', parentCodesToExpand);
// /
setTreeExpandState(parentCodesToExpand);
// //
const checkedKeys = fishTreeRef.value.getCheckedKeys(); const checkedKeys = fishTreeRef.value.getCheckedKeys();
@ -427,6 +434,13 @@ const fishProps = {
children: 'children', children: 'children',
label: 'name', label: 'name',
} }
watch(treeInput, (val) => {
fishTreeRef.value!.filter(val)
})
function filterNode(value: string, data: any) {
if (!value) return true
return data.name.includes(value)
}
const fishData: any = ref([]) const fishData: any = ref([])
// //
// const huixiandata = ref([]) // const huixiandata = ref([])
@ -527,7 +541,43 @@ function filterSelectedIds(checkedKeysArray: number[], allCheckedNodes: any[]):
console.log('过滤后的IDs:', resultIds); console.log('过滤后的IDs:', resultIds);
return resultIds; return resultIds;
} }
// code()
function findParentCodesForCheckedNodes(checkedKeys: number[], treeData: any[]): Set<number> {
const parentCodesToExpand = new Set<number>();
function traverse(nodes: any[], parentCode: number | null = null) {
for (const node of nodes) {
// ,
if (checkedKeys.includes(node.code) && parentCode !== null) {
parentCodesToExpand.add(parentCode);
}
//
if (node.children && node.children.length > 0) {
traverse(node.children, node.code);
}
}
}
traverse(treeData);
return parentCodesToExpand;
}
// /
function setTreeExpandState(parentCodesToExpand: Set<number>) {
if (!fishTreeRef.value || !fishTreeRef.value.store) return;
//
Object.values(fishTreeRef.value.store.nodesMap).forEach((node: any) => {
// ()/
if (node.data.children && node.data.children.length > 0) {
if (parentCodesToExpand.has(node.data.code)) {
node.expanded = true; //
} else {
node.expanded = false; //
}
}
});
}
// - // -
function handleFishCheckChange(checkedNode: any, checkedInfo: any) { function handleFishCheckChange(checkedNode: any, checkedInfo: any) {
const treeInstance = fishTreeRef.value; const treeInstance = fishTreeRef.value;
@ -1048,9 +1098,9 @@ function handleClearSelection() {
</div> </div>
<el-scrollbar height="450px" style="margin-top: 10px;"> <el-scrollbar height="450px" style="margin-top: 10px;">
<el-tree ref="fishTreeRef" style="max-width: 300px" :data="fishData" show-checkbox default-expand-all <el-tree ref="fishTreeRef" style="max-width: 300px" :data="fishData" show-checkbox
v-loading="fishTreeDialog" node-key="code" highlight-current :props="fishProps" :filter-node-method="filterNode" v-loading="fishTreeDialog" node-key="code" highlight-current
@check="handleFishCheckChange" /> :props="fishProps" @check="handleFishCheckChange" />
</el-scrollbar> </el-scrollbar>
</div> </div>
<div class="fishTable"> <div class="fishTable">