Files
pqs-9100_client/frontend/src/views/home/components/tree.vue

321 lines
9.6 KiB
Vue
Raw Normal View History

2024-08-23 13:19:20 +08:00
<template>
2025-08-20 08:51:01 +08:00
<div class="plan_tree">
<div class="search_view">
<el-input
placeholder="请输入计划名称"
clearable
v-model="searchForm.planName"
show-word-limit
maxlength="32"
></el-input>
<el-tooltip content="检测计划列表" placement="top">
<Menu
style="width: 26px; height: 26px; margin-left: 8px; cursor: pointer; color: var(--el-color-primary)"
@click.stop="detail()"
/>
</el-tooltip>
2025-08-20 08:51:01 +08:00
</div>
<div class="tree_container">
<el-tree
:data="data"
ref="treeRef"
:filter-node-method="filterNode"
:props="defaultProps"
node-key="id"
class="filter-tree"
:highlight-current="true"
default-expand-all
:default-checked-keys="defaultChecked"
@node-click="handleNodeClick"
>
<template #default="{ node, data }">
<span class="custom-tree-node" style="display: flex; align-items: center">
<!-- 父节点图标 -->
<Platform
v-if="!data.pid"
style="width: 18px; height: 18px; margin-right: 8px"
:style="{
color: node.label == '未检' ? '#fac858' : node.label == '检测中' ? '#ee6666' : '#91cc75'
}"
/>
<!-- 节点名称 -->
<span>{{ node.label }}</span>
<!-- 子节点右侧图标 + tooltip -->
<el-tooltip
v-if="
node.label != '未检' &&
node.label != '检测中' &&
node.label != '检测完成' &&
hasChildrenInPlanTable(node.data)
"
placement="top"
:manual="true"
content="子计划信息"
>
<List
v-auth.plan="'add_subplan'"
@click.stop="childDetail(node.data)"
style="
width: 16px;
height: 16px;
margin-left: 8px;
cursor: pointer;
color: var(--el-color-primary);
"
/>
</el-tooltip>
</span>
</template>
</el-tree>
</div>
</div>
2025-08-20 08:51:01 +08:00
<SourceOpen ref="openSourceView" :width="width" :height="height + 175"></SourceOpen>
2024-08-23 13:19:20 +08:00
</template>
2025-08-20 08:51:01 +08:00
<script lang="ts" setup>
import { type Plan } from '@/api/plan/interface'
import { List, Menu, Platform } from '@element-plus/icons-vue'
import { nextTick, onMounted, ref, watch } from 'vue'
2024-10-11 08:49:31 +08:00
import { useRouter } from 'vue-router'
2025-08-20 08:51:01 +08:00
import { useCheckStore } from '@/stores/modules/check'
import { ElTooltip } from 'element-plus'
2025-07-21 13:47:56 +08:00
import SourceOpen from '@/views/plan/planList/components/childrenPlan.vue'
2025-08-20 08:51:01 +08:00
import { getPlanList } from '@/api/plan/plan.ts'
2025-08-06 15:18:27 +08:00
import { useModeStore } from '@/stores/modules/mode' // 引入模式 store
import { useDictStore } from '@/stores/modules/dict'
2025-07-21 13:47:56 +08:00
const openSourceView = ref()
2024-10-11 08:49:31 +08:00
const router = useRouter()
2024-12-31 19:03:52 +08:00
const checkStore = useCheckStore()
2025-01-06 09:21:24 +08:00
const filterText = ref('')
const treeRef = ref()
2024-10-11 08:49:31 +08:00
const data: any = ref([])
2025-08-06 15:18:27 +08:00
const modeStore = useModeStore()
const dictStore = useDictStore()
2025-08-05 10:37:40 +08:00
2024-08-23 13:19:20 +08:00
const defaultProps = {
2025-08-20 08:51:01 +08:00
children: 'children',
label: 'name',
pid: 'pid'
2024-10-11 08:49:31 +08:00
}
2024-08-23 13:19:20 +08:00
const searchForm = ref({
2025-08-20 08:51:01 +08:00
planName: ''
2024-10-11 08:49:31 +08:00
})
2025-08-20 08:51:01 +08:00
const defaultChecked = ref<string[]>([]) // 明确类型为 number[]
const tree = ref(false) //确保左侧树高凉只执行一次
2024-08-23 13:19:20 +08:00
const getTreeData = (val: any) => {
2025-08-20 08:51:01 +08:00
defaultChecked.value = []
// 遍历 val 的每个 children过滤掉 pid !== '0'
data.value = val
for (let item of data.value) {
if (item.children.length > 0) {
let node = item.children[0]
defaultChecked.value.push(node.id)
checkStore.setPlan(node)
// 高亮显示第一个节点
// 使用 nextTick 确保在 DOM 更新后调用 setCurrentKey
nextTick(() => {
treeRef.value?.setCurrentKey(node.id)
idd.value = node.id
})
// 找到第一个符合条件的 children 后跳出循环
break
}
2025-02-11 10:32:09 +08:00
}
2024-10-11 08:49:31 +08:00
}
2025-01-06 09:21:24 +08:00
2025-01-16 14:24:55 +08:00
//点击表格后左侧树刷新,高亮显示对应节点
2025-08-20 08:51:01 +08:00
const clickTableToTree = (val: any, id: any) => {
defaultChecked.value = []
data.value = val
let node = ref('')
if (data.value.length > 0) {
for (let i = 0; i < data.value.length; i++) {
for (let j = 0; j < data.value[i].children.length; j++) {
if (data.value[i].children[j].id == id) {
node.value = data.value[i].children[j].id
break
}
2025-01-16 14:24:55 +08:00
}
}
2025-08-20 08:51:01 +08:00
// 使用 nextTick 确保在 DOM 更新后调用 setCurrentKey
nextTick(() => {
treeRef.value?.setCurrentKey(node.value)
idd.value = node.value
})
2025-01-16 14:24:55 +08:00
}
}
2025-08-20 08:51:01 +08:00
const { updateSelectedTreeNode, width, height, planTable } = defineProps<{
updateSelectedTreeNode: Function
width: number
height: number
planTable?: Array<[]>
}>()
2025-08-05 10:37:40 +08:00
2024-08-23 13:19:20 +08:00
watch(
2025-08-20 08:51:01 +08:00
() => searchForm.value.planName,
val => {
treeRef.value!.filter(val)
},
{
deep: true
}
2024-10-11 08:49:31 +08:00
)
2025-01-14 19:05:08 +08:00
2025-08-06 15:18:27 +08:00
const hasChildrenInPlanTable = (nodeData: Plan.ResPlan) => {
2025-08-20 08:51:01 +08:00
try {
// 在 planTable 中查找对应的节点数据
const foundItem = tableData.value.find((item: any) => item.id === nodeData.id)
// 检查是否有 children 且 children 数组不为空
return foundItem && Array.isArray(foundItem.children) && foundItem.children.length > 0
} catch (error) {
console.error('检查子节点时出错:', error)
return false
}
}
2025-08-06 15:18:27 +08:00
2025-01-14 19:05:08 +08:00
const idd = ref('')
2024-12-13 16:35:27 +08:00
const handleNodeClick = (data: Plan.ResPlan) => {
2025-02-11 10:32:09 +08:00
if (data.name === '未检' || data.name === '检测中' || data.name === '检测完成') {
2025-08-20 08:51:01 +08:00
// 如果是父节点,不执行任何操作
//console.log('父节点不执行任何操作');
// 设置当前高亮节点
nextTick(() => {
treeRef.value?.setCurrentKey(idd.value)
})
return
}
idd.value = data.id
2025-08-20 08:51:01 +08:00
checkStore.setPlan(data)
updateSelectedTreeNode(data.id)
2024-10-11 08:49:31 +08:00
}
2024-12-13 16:35:27 +08:00
const filterNode = (value: string, data: any) => {
2025-08-20 08:51:01 +08:00
if (!value) return true
return data.name.includes(value)
2024-10-11 08:49:31 +08:00
}
2025-01-06 09:21:24 +08:00
2024-08-27 15:40:58 +08:00
// 点击详情
2024-10-11 08:49:31 +08:00
const detail = () => {
2025-08-20 08:51:01 +08:00
router.push('/plan/planList')
2024-10-11 08:49:31 +08:00
}
2025-07-21 13:47:56 +08:00
const childDetail = (data: Plan.ResPlan) => {
2025-08-20 08:51:01 +08:00
const filteredPlans = tableData.value.filter(item => item.id === data.id)
2025-08-06 15:18:27 +08:00
2025-08-20 08:51:01 +08:00
// 确保有匹配项再访问 [0]
if (filteredPlans.length > 0 && openSourceView.value) {
openSourceView.value.open('检测计划详情', filteredPlans[0])
}
2025-08-06 15:18:27 +08:00
}
function buildTree(flatList: any[]): any[] {
2025-08-20 08:51:01 +08:00
const map = new Map()
const tree: any[] = []
// First, create a map of all items by id for fast lookup
flatList.forEach(item => {
map.set(item.id, { ...item, children: [] })
})
// Then, assign each item to its parent's children array
flatList.forEach(item => {
if (item.fatherPlanId && map.has(item.fatherPlanId)) {
map.get(item.fatherPlanId).children.push(map.get(item.id))
} else if (item.fatherPlanId === '0') {
// Items with fatherPlanId '0' are root nodes
tree.push(map.get(item.id))
}
})
2025-08-06 15:18:27 +08:00
2025-08-20 08:51:01 +08:00
return tree
2025-07-21 13:47:56 +08:00
}
2025-08-06 15:18:27 +08:00
const tableData = ref<any[]>([])
onMounted(async () => {
2025-08-20 08:51:01 +08:00
if (modeStore.currentMode != '比对式') return
const patternId = dictStore.getDictData('Pattern').find(item => item.name === modeStore.currentMode)?.id
const result = await getPlanList({ patternId: patternId })
tableData.value = buildTree(result.data as any[])
2024-10-11 08:49:31 +08:00
})
2025-08-06 15:18:27 +08:00
2025-08-20 08:51:01 +08:00
defineExpose({ getTreeData, clickTableToTree })
2024-08-23 13:19:20 +08:00
</script>
2025-08-20 08:51:01 +08:00
<style lang="scss" scoped>
2024-08-23 13:19:20 +08:00
.plan_tree {
2025-08-20 08:51:01 +08:00
height: 100%;
2024-08-27 18:37:46 +08:00
display: flex;
2025-08-20 08:51:01 +08:00
flex-direction: column;
background-color: #fff;
.search_view {
width: 100%;
height: auto;
display: flex;
justify-content: space-between;
padding: 0 5px;
box-sizing: border-box;
align-items: center;
.el-input {
margin-top: 6px;
}
}
2024-10-11 08:49:31 +08:00
.el-input {
2025-08-20 08:51:01 +08:00
width: 100%;
margin: 0 10px 10px 0;
2024-08-27 18:37:46 +08:00
}
2024-10-11 08:49:31 +08:00
2025-08-20 08:51:01 +08:00
.tree_container {
height: 100%;
width: 100%;
flex: 1;
overflow-y: auto;
overflow-x: auto;
2024-08-27 15:40:58 +08:00
2025-08-20 08:51:01 +08:00
.el-tree {
// height: 100%;
width: auto;
}
}
2024-08-23 13:19:20 +08:00
}
2025-01-06 09:21:24 +08:00
2024-12-05 18:14:43 +08:00
.filter-tree {
2025-08-20 08:51:01 +08:00
// border: 1px solid #dcdfe6;
min-width: 100%;
height: 97%;
display: inline-block;
overflow: auto;
margin-top: 12px;
2024-12-05 18:14:43 +08:00
}
2024-12-05 21:33:52 +08:00
//.filter-tree span {
// font-size: 16px;
// display:block;
// overflow:hidden;
2025-08-20 08:51:01 +08:00
// word-break:keep-all;
// white-space:nowrap;
// text-overflow:ellipsis;
2024-12-05 21:33:52 +08:00
// padding-right: 12px;
//}
2024-12-05 18:14:43 +08:00
.leftBox {
2025-08-20 08:51:01 +08:00
// float: left;
// width: 20%;
height: 100%;
width: 100%;
2024-12-05 18:14:43 +08:00
}
.left {
2025-08-20 08:51:01 +08:00
height: calc(100% - 45px);
overflow: auto;
2024-12-05 18:14:43 +08:00
}
/* 设置滚动条宽度 */
:deep(.bodyTwo ::-webkit-scrollbar) {
2025-08-20 08:51:01 +08:00
width: 3px !important;
height: 6px !important;
}
2024-08-23 13:19:20 +08:00
</style>