Files
admin-govern/src/views/govern/device/planData/components/schemeTree.vue

310 lines
9.2 KiB
Vue
Raw Normal View History

2024-06-18 16:35:53 +08:00
<template>
<div>
2024-06-24 14:38:42 +08:00
<div
:style="{ width: menuCollapse ? '40px' : '280px' }"
style="transition: all 0.3s; overflow: hidden; height: 100%"
>
2024-06-18 16:35:53 +08:00
<Icon
v-show="menuCollapse"
@click="onMenuCollapse"
:name="menuCollapse ? 'el-icon-Expand' : 'el-icon-Fold'"
:class="menuCollapse ? 'unfold' : ''"
size="18"
class="fold ml10 mt20 menu-collapse"
style="cursor: pointer"
/>
<div class="cn-tree" :style="{ opacity: menuCollapse ? 0 : 1 }">
<div style="display: flex; align-items: center" class="mb10">
<el-input v-model="filterText" placeholder="请输入内容" clearable>
<template #prefix>
<Icon name="el-icon-Search" style="font-size: 16px" />
</template>
</el-input>
<Icon
@click="onMenuCollapse"
:name="menuCollapse ? 'el-icon-Expand' : 'el-icon-Fold'"
:class="menuCollapse ? 'unfold' : ''"
size="18"
class="fold ml10 menu-collapse"
style="cursor: pointer"
v-if="true"
/>
</div>
<el-tree
style="flex: 1; overflow: auto"
:props="defaultProps"
highlight-current
:filter-node-method="filterNode"
node-key="id"
v-bind="$attrs"
default-expand-all
:data="tree"
ref="treRef"
@node-click="clickNode"
:expand-on-click-node="false"
2024-06-24 14:38:42 +08:00
2024-06-18 16:35:53 +08:00
>
<template #default="{ node, data }">
<span class="custom-tree-node">
<div class="left">
<Icon
:name="data.icon"
style="font-size: 16px"
:style="{ color: data.color }"
v-if="data.icon"
/>
2024-06-24 14:38:42 +08:00
<span>{{ node.label }}</span>
2024-06-18 16:35:53 +08:00
</div>
<div class="right">
<a :style="{ marginRight: '0.5rem' }" v-if="data.children">
<el-icon :style="{ color: '#0000FF' }">
<Plus @click="add(node, data)" />
</el-icon>
</a>
<a :style="{ marginRight: '0.5rem' }">
<el-icon :style="{ color: '#DA3434' }">
<Delete @click="del(node, data)" />
</el-icon>
</a>
<a :style="{ marginRight: '0.5rem' }">
<el-icon :style="{ color: '#0000FF' }">
<Edit @click="edit(node, data)" />
</el-icon>
</a>
</div>
</span>
</template>
</el-tree>
</div>
</div>
<popup ref="dialogRef" @onSubmit="getTreeList"></popup>
</div>
</template>
<script lang="ts" setup>
import { ref, nextTick, watch, defineProps, defineEmits, inject, onMounted } from 'vue'
import { getSchemeTree } from '@/api/cs-device-boot/planData'
import { useConfig } from '@/stores/config'
import useCurrentInstance from '@/utils/useCurrentInstance'
import { ElTree } from 'element-plus'
import { Plus, Edit, Delete } from '@element-plus/icons-vue'
import { delRecord } from '@/api/cs-device-boot/planData'
import popup from './popup.vue'
2024-06-24 14:38:42 +08:00
import { ElMessage, ElMessageBox } from 'element-plus'
2024-06-18 16:35:53 +08:00
defineOptions({
name: 'govern/schemeTree'
})
interface Props {
width?: string
canExpand?: boolean
}
const { proxy } = useCurrentInstance()
const menuCollapse = ref(false)
const filterText = ref('')
const treeRef = ref<InstanceType<typeof ElTree>>()
watch(filterText, val => {
2024-06-24 14:38:42 +08:00
treRef.value!.filter(val)
2024-06-18 16:35:53 +08:00
})
const onMenuCollapse = () => {
menuCollapse.value = !menuCollapse.value
proxy.eventBus.emit('cnTreeCollapse', menuCollapse)
}
const filterNode = (value: string, data: any) => {
if (!value) return true
return data.name.includes(value)
}
/** 树形结构数据 */
const defaultProps = {
children: 'children',
label: 'name',
value: 'id'
}
const props = withDefaults(
defineProps<{
showCheckbox?: boolean
defaultCheckedKeys?: any
}>(),
{
showCheckbox: false,
defaultCheckedKeys: []
}
)
2024-06-24 14:38:42 +08:00
const emit = defineEmits(['init', 'checkChange', 'nodeChange', 'editNode', 'getChart'])
2024-06-18 16:35:53 +08:00
const config = useConfig()
const tree = ref()
const treRef = ref()
//获取方案树形数据
const getTreeList = () => {
getSchemeTree().then(res => {
let arr: any[] = []
res.data.forEach((item: any) => {
2024-06-24 14:38:42 +08:00
item.icon = 'el-icon-Menu'
2024-06-18 16:35:53 +08:00
item.color = config.getColorVal('elementUiPrimary')
item.children.forEach((item2: any) => {
2024-06-24 14:38:42 +08:00
item2.icon = 'el-icon-Document'
2024-06-18 16:35:53 +08:00
item2.color = config.getColorVal('elementUiPrimary')
arr.push(item2)
})
})
tree.value = res.data
nextTick(() => {
if (arr.length) {
treRef.value.setCurrentKey(arr[0].id)
// 注册父组件事件
emit('init', {
level: 2,
...arr[0]
})
} else {
emit('init')
}
})
})
}
getTreeList()
const dialogRef = ref()
const handleOpen = (val: any, id: any) => {
dialogRef.value.open(val, id)
}
2024-06-24 14:38:42 +08:00
//方案id
2024-06-18 16:35:53 +08:00
const planId: any = ref('')
2024-06-24 14:38:42 +08:00
//测试项id
const monitorId: any = ref('')
2024-06-18 16:35:53 +08:00
const planData: any = ref({})
const getPlanData = (row: any) => {
planData.value = {}
planData.value = JSON.parse(JSON.stringify(row))
}
/** 添加树节点 */
2024-06-24 14:38:42 +08:00
// 0 新增方案 1 修改方案 2 新增测试项 3 修改测试项 4 设备信息
2024-06-18 16:35:53 +08:00
const add = (node: any, data: any) => {
planId.value = data.id
//添加测试项
if (data.children) {
handleOpen(2, planId.value)
}
}
/** 编辑树节点 */
const edit = async (node: Node, data: any) => {
planId.value = data.id
//修改方案
if (data.children) {
await handleOpen(1, planId.value)
}
//修改测试项
else {
2024-06-24 14:38:42 +08:00
monitorId.value = data.id
2024-06-18 16:35:53 +08:00
await handleOpen(3, planId.value)
}
}
/** 删除树节点 */
const del = (node: Node, data: any) => {
planId.value = data.id
//删除方案/测试项
2024-06-24 14:38:42 +08:00
ElMessageBox.confirm('是否确认删除?', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
2024-06-18 16:35:53 +08:00
})
2024-06-24 14:38:42 +08:00
.then(() => {
delRecord({ id: data.id }).then(res => {
if (res.code == 'A0000') {
ElMessage.success('删除成功')
getTreeList()
}
})
})
.catch(() => {
ElMessage({
type: 'info',
message: '已取消'
})
})
2024-06-18 16:35:53 +08:00
}
2024-06-24 14:38:42 +08:00
//取消删除
const cancelDel = () => {}
2024-06-18 16:35:53 +08:00
const clickNode = (e: anyObj) => {
2024-06-24 14:38:42 +08:00
e.children ? (planId.value = e.id) : (planId.value = e.pid)
2024-06-18 16:35:53 +08:00
emit('nodeChange', e)
}
watch(
() => planData.value,
(val, oldVal) => {
if (val && dialogRef.value) {
2024-06-24 14:38:42 +08:00
const obj = JSON.parse(JSON.stringify(val))
console.log(obj,"88888888888");
obj.records = [
val.records.find(item => {
return item.id == monitorId.value
})
]
dialogRef.value.details(obj)
2024-06-18 16:35:53 +08:00
}
},
{
immediate: true,
deep: true
}
)
2024-06-24 14:38:42 +08:00
defineExpose({ treeRef, getPlanData, getTreeList })
2024-06-18 16:35:53 +08:00
</script>
<style lang="scss" scoped>
.cn-tree {
flex-shrink: 0;
display: flex;
flex-direction: column;
box-sizing: border-box;
padding: 10px;
height: 100%;
width: 100%;
:deep(.el-tree) {
border: 1px solid var(--el-border-color);
}
:deep(.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content) {
background-color: var(--el-color-primary-light-7);
}
.menu-collapse {
color: var(--el-color-primary);
}
}
2024-06-24 14:38:42 +08:00
2024-06-18 16:35:53 +08:00
.ml10 {
margin-bottom: 0 !important;
}
.add_plan {
width: 100%;
height: 40px;
display: flex;
justify-content: flex-end;
align-items: center;
}
.custom-tree-node {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
2024-06-24 14:38:42 +08:00
.left,
.right {
display: flex;
align-items: center;
}
.left{
span{
margin-left: 2px;
}
}
.right{
a{
margin-left: 2px;
}
}
2024-06-18 16:35:53 +08:00
}
</style>