修复编辑接口无法正确绑定上级接口的问题

tree树形函数新增filterList(过滤数据并返回所有父级数据)
master
zhontai 11 months ago
parent 940b382449
commit 835ffb7384

@ -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

@ -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 = []

Loading…
Cancel
Save