From 835ffb7384600f2b8c53279cf4879c7364c15633 Mon Sep 17 00:00:00 2001 From: zhontai <361243234@qq.com> Date: Fri, 23 Feb 2024 15:15:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=BE=91=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E4=B8=8A=E7=BA=A7=E6=8E=A5=E5=8F=A3=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20tree=E6=A0=91=E5=BD=A2=E5=87=BD=E6=95=B0=E6=96=B0=E5=A2=9Efi?= =?UTF-8?q?lterList=EF=BC=88=E8=BF=87=E6=BB=A4=E6=95=B0=E6=8D=AE=E5=B9=B6?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=89=80=E6=9C=89=E7=88=B6=E7=BA=A7=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/zhontai.ui.admin.vue3/src/utils/tree.ts | 69 +++++++++++++++++++ .../src/views/admin/api/index.vue | 11 ++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/ui/zhontai.ui.admin.vue3/src/utils/tree.ts b/ui/zhontai.ui.admin.vue3/src/utils/tree.ts index 7debc5af..3cc20263 100644 --- a/ui/zhontai.ui.admin.vue3/src/utils/tree.ts +++ b/ui/zhontai.ui.admin.vue3/src/utils/tree.ts @@ -142,6 +142,75 @@ export function filterTree(tree: any = [], keyword: string, options = {}) { }) } +/** +* @description: 列表过滤数据并返回所有父级数据 +* @example +filterList(cloneDeep(list), keyword) + +filterList(cloneDeep(list), keyword, { + parentWhere: (item: any, parent: any) => { + return item.id === parent.parentId + }, + parentEndWhere: (item: any) => { + return item.parentId > 0 + }, + filterWhere: (item: any, word: string) => { + return item.label?.toLocaleLowerCase().includes(word) + }, +}) +*/ +export function filterList(list: any[], keyword: string, options = {}) { + const { parentWhere, parentEndWhere, filterWhere } = Object.assign( + { + parentWhere: (item: any, parent: any) => { + return item.id === parent.parentId + }, + parentEndWhere: (item: any) => { + return item.parentId > 0 + }, + filterWhere: (item: any, word: string) => { + return item.label?.toLocaleLowerCase().includes(word) + }, + }, + options || {} + ) + + let dataList: any[] = [] + + function searchParentList(parent: any, list: any[]): any[] { + let parentList: any[] = [] + for (let i = 0; i < list.length; i++) { + const item = list[i] + if (parentWhere(item, parent)) { + parentList.push(item) + if (parentEndWhere(item)) { + const parentNodesData = searchParentList(item, list) + parentList.push(...parentNodesData) + } + } + } + return parentList + } + + for (let i = 0; i < list.length; i++) { + const item = list[i] + if (filterWhere(item, keyword)) { + dataList.push(item) + const parentList = searchParentList(item, list) + dataList.push(...parentList) + } + } + + const uniqueNodes: { [id: string | number]: any } = {} + for (const item of dataList) { + uniqueNodes[item.id] = item + } + + dataList = Object.values(uniqueNodes) + + return dataList +} + /** * @description: 树形列表转列表包含子级 * @example diff --git a/ui/zhontai.ui.admin.vue3/src/views/admin/api/index.vue b/ui/zhontai.ui.admin.vue3/src/views/admin/api/index.vue index 29efe35b..62e1bfd0 100644 --- a/ui/zhontai.ui.admin.vue3/src/views/admin/api/index.vue +++ b/ui/zhontai.ui.admin.vue3/src/views/admin/api/index.vue @@ -54,7 +54,7 @@ import { ref, reactive, onMounted, getCurrentInstance, onBeforeMount, defineAsyn import { ApiListOutput } from '/@/api/admin/data-contracts' import { ApiApi } from '/@/api/admin/Api' import { ApiApi as ApiExtApi } from '/@/api/admin.extend/Api' -import { listToTree, treeToList, filterTree } from '/@/utils/tree' +import { listToTree, treeToList, filterTree, filterList } from '/@/utils/tree' import { cloneDeep, isArray } from 'lodash-es' import eventBus from '/@/utils/mitt' @@ -103,7 +103,14 @@ const onQuery = async () => { return item.label?.toLocaleLowerCase().indexOf(keyword) > -1 || item.path?.toLocaleLowerCase().indexOf(keyword) > -1 }, }) - state.formApiTreeData = listToTree(res.data.filter((a) => a.parentId === 0)) + + state.formApiTreeData = listToTree( + filterList(cloneDeep(res.data), '', { + filterWhere: (item: any, word: string) => { + return !item.httpMethods + }, + }) + ) } else { state.apiTreeData = [] state.formApiTreeData = []