Files
admin-govern/src/views/auth/menu/menu.vue

161 lines
4.5 KiB
Vue
Raw Normal View History

2024-01-18 18:19:59 +08:00
<template>
<div>
<div class="menu-index-header">
<div style="flex: 1; font-weight: 700">菜单列表</div>
<el-input
v-model="tableStore.table.params.searchValue"
style="width: 240px"
placeholder="请输入菜单名称"
class="ml10"
clearable
@input="search"
/>
<el-button :icon="Plus" type="primary" @click="addMenu" class="ml10">新增</el-button>
</div>
<Table
:tree-config="{ reserve: true, expandAll: !!tableStore.table.params.searchValue }"
@currentChange="currentChange"
/>
<popupMenu ref="popupRef" @init="emits('init')"></popupMenu>
</div>
</template>
<script setup lang="ts">
import { Plus } from '@element-plus/icons-vue'
import { ref, nextTick, inject, provide, watch } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import popupMenu from './popupMenu.vue'
import { delMenu } from '@/api/systerm'
defineOptions({
name: 'auth/menu/menu'
})
const emits = defineEmits<{
(e: 'init'): void
(e: 'select', row: any): void
}>()
const props = withDefaults(
defineProps<{
menuData: treeData[]
}>(),
{
menuData: () => {
return []
}
}
)
const popupRef = ref()
const tableStore = new TableStore({
showPage: false,
url: '/user-boot/function/functionTree',
column: [
{ title: '菜单名称', field: 'title', align: 'left', treeNode: true },
{
title: '图标',
field: 'icon',
align: 'center',
width: '60',
render: 'icon'
},
{
title: '操作',
align: 'center',
width: '130',
render: 'buttons',
buttons: [
{
name: 'edit',
title: '新增菜单',
type: 'primary',
icon: 'el-icon-Plus',
render: 'tipButton',
click: row => {
popupRef.value.open('新增菜单', { pid: row.id })
}
},
{
name: 'edit',
title: '编辑菜单',
type: 'primary',
icon: 'el-icon-EditPen',
render: 'tipButton',
click: row => {
popupRef.value.open('编辑菜单', row)
}
},
{
name: 'del',
title: '删除菜单',
type: 'danger',
icon: 'el-icon-Delete',
render: 'confirmButton',
popconfirm: {
confirmButtonText: '确认',
cancelButtonText: '取消',
confirmButtonType: 'danger',
title: '确定删除该菜单吗?'
},
click: row => {
delMenu(row.id).then(() => {
emits('init')
})
}
}
]
}
]
})
tableStore.table.loading = false
watch(
() => props.menuData,
() => {
search()
}
)
provide('tableStore', tableStore)
const addMenu = () => {
console.log(popupRef)
popupRef.value.open('新增菜单')
}
const currentChange = (newValue: any) => {
emits('select', newValue.row.id)
}
const search = () => {
tableStore.table.data = filterData(JSON.parse(JSON.stringify(props.menuData)))
if (tableStore.table.params.searchValue) {
nextTick(() => {
tableStore.table.ref?.setAllTreeExpand(true)
})
} else {
nextTick(() => {
tableStore.table.ref?.setAllTreeExpand(false)
})
}
}
// 过滤
const filterData = (arr: treeData[]): treeData[] => {
if (!tableStore.table.params.searchValue) {
return arr
}
return arr.filter((item: treeData) => {
if (item.title.includes(tableStore.table.params.searchValue)) {
return true
} else if (item.children?.length > 0) {
item.children = filterData(item.children)
return item.children.length > 0
} else {
return false
}
})
}
</script>
<style lang="scss">
.menu-index-header {
display: flex;
padding: 13px 15px;
align-items: center;
}
</style>