diff --git a/frontend/src/pages/OrgChart.tsx b/frontend/src/pages/OrgChart.tsx index e67a6eb..d432f83 100644 --- a/frontend/src/pages/OrgChart.tsx +++ b/frontend/src/pages/OrgChart.tsx @@ -51,6 +51,28 @@ export default function OrgChart() { onError: (err: any) => toast.error(err?.response?.data?.error?.message || '删除失败'), }) + // 构建部门层级路径映射,如 "总公司 / 技术部 / 前端组" + const buildDeptPath = (departments: any[]): Record => { + const map: Record = {} + const byId: Record = {} + departments.forEach(d => { byId[d.id] = d }) + const getPath = (d: any): string => { + if (map[d.id]) return map[d.id] + const parts: string[] = [d.name] + let cur = d + while (cur.parentId && byId[cur.parentId]) { + cur = byId[cur.parentId] + parts.unshift(cur.name) + } + const path = parts.join(' / ') + map[d.id] = path + return path + } + departments.forEach(d => getPath(d)) + return map + } + const deptPathMap = buildDeptPath(departments) + const toggleExpand = (id: string) => { setExpanded(prev => { const next = new Set(prev) @@ -180,9 +202,9 @@ export default function OrgChart() {
@@ -215,6 +237,28 @@ function PositionTab({ positions, departments }: { positions: any[]; departments const [editing, setEditing] = useState(null) const [form, setForm] = useState({ name: '', departmentId: '', headcount: 0, level: '', description: '' }) + // 构建部门层级路径 + const buildDeptPath = (depts: any[]): Record => { + const map: Record = {} + const byId: Record = {} + depts.forEach(d => { byId[d.id] = d }) + const getPath = (d: any): string => { + if (map[d.id]) return map[d.id] + const parts: string[] = [d.name] + let cur = d + while (cur.parentId && byId[cur.parentId]) { + cur = byId[cur.parentId] + parts.unshift(cur.name) + } + const path = parts.join(' / ') + map[d.id] = path + return path + } + depts.forEach(d => getPath(d)) + return map + } + const deptPathMap = buildDeptPath(departments) + const createMutation = useMutation({ mutationFn: (data: any) => editing ? api.put(`/positions/${editing.id}`, data) : api.post('/positions', data), onSuccess: () => { @@ -288,7 +332,7 @@ function PositionTab({ positions, departments }: { positions: any[]; departments