29 lines
779 B
TypeScript
29 lines
779 B
TypeScript
import type { CategoryTree } from '@/types'
|
|
|
|
/** 在分类树中找目标分类的祖先路径(含自身) */
|
|
export function findCategoryPath(nodes: CategoryTree[], targetId: string): string[] {
|
|
for (const n of nodes) {
|
|
if (n.id === targetId) return [n.id]
|
|
if (n.children?.length) {
|
|
const sub = findCategoryPath(n.children, targetId)
|
|
if (sub.length) return [n.id, ...sub]
|
|
}
|
|
}
|
|
return []
|
|
}
|
|
|
|
export interface CascadeNode {
|
|
value: string
|
|
label: string
|
|
children?: CascadeNode[]
|
|
}
|
|
|
|
/** 分类树 → el-cascader 选项 */
|
|
export function buildCascader(tree: CategoryTree[]): CascadeNode[] {
|
|
return tree.map((n) => ({
|
|
value: n.id,
|
|
label: n.categoryName,
|
|
children: n.children?.length ? buildCascader(n.children) : undefined,
|
|
}))
|
|
}
|