Browse Source

菜单搜索功能优化

master
陈薪名 10 months ago
parent
commit
2b1f748c2f
  1. 39
      src/components/RouterSearch/index.vue

39
src/components/RouterSearch/index.vue

@ -37,6 +37,8 @@
<script lang="ts" setup> <script lang="ts" setup>
import { usePermissionStore } from '@/store/modules/permission' import { usePermissionStore } from '@/store/modules/permission'
import { filterBreadcrumb } from '@/layout/components/Breadcrumb/src/helper' import { filterBreadcrumb } from '@/layout/components/Breadcrumb/src/helper'
import { filter, treeToList } from '@/utils/tree'
import type { RouteLocationNormalizedLoaded, RouteMeta } from 'vue-router'
defineProps({ defineProps({
isModal: { isModal: {
@ -51,9 +53,9 @@ const routersa = permissionStore.getRouters
const analyzeRouters = (routers:any) => { const analyzeRouters = (routers:any) => {
return recursion(routers, []) return recursion(routers, [])
} }
// //
const recursion = (routers:any, rs) => { const recursion = (routers:any, rs) => {
routers.forEach(item => { routers.forEach((item) => {
if (item.children?.length > 0) { if (item.children?.length > 0) {
recursion(item.children, rs) recursion(item.children, rs)
} else { } else {
@ -62,11 +64,29 @@ const recursion = (routers:any, rs) => {
}) })
return rs return rs
} }
//
const recursionParentName = (routers:any, rs) => {
let routerParent = router.getRoutes().find(itemRouter =>
itemRouter.path == routers.path.substring(0,routers.path.lastIndexOf('/'))
)
if (routerParent) {
recursionParentName(routerParent, rs)
} else {
rs.push(routers.meta.title)
}
return rs
}
const router = useRouter() // const router = useRouter() //
const showSearch = ref(false) // const showSearch = ref(false) //
const showTopSearch = ref(false) // const showTopSearch = ref(false) //
const value: Ref = ref('') // const value: Ref = ref('') //
// const routers = router.getRoutes() // const levelList = ref<AppRouteRecordRaw[]>([])
const menuRouters = computed(() => {
const routers = permissionStore.getRouters
return filterBreadcrumb(routers)
})
// 20240111 cxm // 20240111 cxm
const routers = analyzeRouters(filterBreadcrumb(routersa)) const routers = analyzeRouters(filterBreadcrumb(routersa))
const options = computed(() => { const options = computed(() => {
@ -75,14 +95,23 @@ const options = computed(() => {
return [] return []
} }
const list = routers.filter((item: any) => { let list = routers.filter((item: any) => {
if (item.meta.title?.indexOf(value.value) > -1 || item.path.indexOf(value.value) > -1) { if (item.meta.title?.indexOf(value.value) > -1 || item.path.indexOf(value.value) > -1) {
return true return true
} }
}) })
return list.map((item) => { return list.map((item) => {
let menuName = ''
levelList.value = filter<AppRouteRecordRaw>(unref(menuRouters), (node: AppRouteRecordRaw) => {
return node.path === item.path
})
const breadcrumbList = treeToList<AppRouteRecordRaw[]>(unref(levelList))
breadcrumbList.map((v) => {
menuName += v?.meta?.title + ' / '
})
return { return {
label: `${item.meta.title}${item.path}`, label: `${item.meta.title} ----> ${menuName.substring(0,menuName.length-3)}`,
value: item.path value: item.path
} }
}) })

Loading…
Cancel
Save