feat: add dept tree select

pull/2/head
xingyu 2022-07-20 10:36:00 +08:00
parent 90db83e12e
commit 1335a7aef7
2 changed files with 20 additions and 9 deletions

View File

@ -1,8 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { ElCard, ElTree, ElTreeSelect, ElMessage, ElMessageBox } from 'element-plus' import { ElInput, ElCard, ElTree, ElTreeSelect, ElMessage, ElMessageBox } from 'element-plus'
import { handleTree } from '@/utils/tree' import { handleTree } from '@/utils/tree'
import { onMounted, ref, unref } from 'vue' import { onMounted, ref, unref, watch } from 'vue'
import * as DeptApi from '@/api/system/dept' import * as DeptApi from '@/api/system/dept'
import { Form, FormExpose } from '@/components/Form' import { Form, FormExpose } from '@/components/Form'
import { modelSchema } from './dept.data' import { modelSchema } from './dept.data'
@ -28,6 +28,7 @@ const deptParentId = ref(0) // 上级ID
const formRef = ref<FormExpose>() const formRef = ref<FormExpose>()
// ========== ========== // ========== ==========
const filterText = ref('')
const deptOptions = ref([]) // const deptOptions = ref([]) //
const treeRef = ref<InstanceType<typeof ElTree>>() const treeRef = ref<InstanceType<typeof ElTree>>()
const getTree = async () => { const getTree = async () => {
@ -38,9 +39,13 @@ const filterNode = (value: string, data: Tree) => {
if (!value) return true if (!value) return true
return data.name.includes(value) return data.name.includes(value)
} }
watch(filterText, (val) => {
treeRef.value!.filter(val)
})
// //
const handleAdd = () => { const handleAdd = (data: { id: number }) => {
// //
deptParentId.value = data.id
formTitle.value = '新增部门' formTitle.value = '新增部门'
unref(formRef)?.getElFormRef()?.resetFields() unref(formRef)?.getElFormRef()?.resetFields()
showForm.value = true showForm.value = true
@ -73,7 +78,7 @@ const submitForm = async () => {
// //
try { try {
const data = unref(formRef)?.formModel as DeptVO const data = unref(formRef)?.formModel as DeptVO
deptParentId.value = data.parentId data.parentId = deptParentId.value
// TODO: // TODO:
if (formTitle.value.startsWith('新增')) { if (formTitle.value.startsWith('新增')) {
await DeptApi.createDeptApi(data) await DeptApi.createDeptApi(data)
@ -104,6 +109,7 @@ onMounted(async () => {
<div class="custom-tree-container"> <div class="custom-tree-container">
<!-- <p>部门列表</p> --> <!-- <p>部门列表</p> -->
<!-- 操作工具栏 --> <!-- 操作工具栏 -->
<el-input v-model="filterText" placeholder="搜索部门" />
<el-tree <el-tree
ref="treeRef" ref="treeRef"
node-key="id" node-key="id"
@ -111,13 +117,13 @@ onMounted(async () => {
:props="defaultProps" :props="defaultProps"
:highlight-current="true" :highlight-current="true"
default-expand-all default-expand-all
:filter-method="filterNode" :filter-node-method="filterNode"
> >
<template #default="{ node, data }"> <template #default="{ node, data }">
<span class="custom-tree-node"> <span class="custom-tree-node">
<span>{{ node.label }}</span> <span>{{ node.label }}</span>
<span> <span>
<el-button link v-hasPermi="['system:dept:create']" @click="handleAdd()"> <el-button link v-hasPermi="['system:dept:create']" @click="handleAdd(data)">
<Icon icon="ep:plus" class="mr-5px" /> <Icon icon="ep:plus" class="mr-5px" />
</el-button> </el-button>
<el-button link v-hasPermi="['system:dept:update']" @click="handleUpdate(data)"> <el-button link v-hasPermi="['system:dept:update']" @click="handleUpdate(data)">

View File

@ -1,7 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref, unref } from 'vue' import { onMounted, ref, unref, watch } from 'vue'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { import {
ElInput,
ElMessage, ElMessage,
ElCard, ElCard,
ElTree, ElTree,
@ -55,6 +56,7 @@ const { register, tableObject, methods } = useTable<UserVO>({
const { getList, setSearchParams, delList, exportList } = methods const { getList, setSearchParams, delList, exportList } = methods
// ========== ========== // ========== ==========
const filterText = ref('')
const deptOptions = ref([]) // const deptOptions = ref([]) //
const searchForm = ref<FormExpose>() const searchForm = ref<FormExpose>()
const treeRef = ref<InstanceType<typeof ElTree>>() const treeRef = ref<InstanceType<typeof ElTree>>()
@ -73,7 +75,9 @@ const handleDeptNodeClick = (data: { [key: string]: any }) => {
tableTitle.value = data.name tableTitle.value = data.name
methods.getList() methods.getList()
} }
watch(filterText, (val) => {
treeRef.value!.filter(val)
})
// ========== CRUD ========== // ========== CRUD ==========
const loading = ref(false) // const loading = ref(false) //
const actionType = ref('') // const actionType = ref('') //
@ -265,6 +269,7 @@ getList()
<span>部门列表</span> <span>部门列表</span>
</div> </div>
</template> </template>
<el-input v-model="filterText" placeholder="搜索部门" />
<el-tree <el-tree
ref="treeRef" ref="treeRef"
node-key="id" node-key="id"
@ -272,7 +277,7 @@ getList()
:data="deptOptions" :data="deptOptions"
:props="defaultProps" :props="defaultProps"
:highlight-current="true" :highlight-current="true"
:filter-method="filterNode" :filter-node-method="filterNode"
:expand-on-click-node="false" :expand-on-click-node="false"
@node-click="handleDeptNodeClick" @node-click="handleDeptNodeClick"
/> />