diff --git a/frontend/src/pages/OrgChart.tsx b/frontend/src/pages/OrgChart.tsx index d432f83..2a4e094 100644 --- a/frontend/src/pages/OrgChart.tsx +++ b/frontend/src/pages/OrgChart.tsx @@ -51,27 +51,15 @@ 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 deptOptions: { id: string; label: string; level: number }[] = [] + const buildDeptOptions = (items: any[], parentId: string | null, level: number) => { + items.filter(d => d.parentId === parentId).sort((a, b) => a.sortOrder - b.sortOrder).forEach(d => { + deptOptions.push({ id: d.id, label: d.name, level }) + buildDeptOptions(items, d.id, level + 1) + }) } - const deptPathMap = buildDeptPath(departments) + buildDeptOptions(departments, null, 0) const toggleExpand = (id: string) => { setExpanded(prev => { @@ -202,9 +190,9 @@ export default function OrgChart() {
@@ -237,27 +225,15 @@ 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 deptOptions: { id: string; label: string; level: number }[] = [] + const buildDeptOptions = (items: any[], parentId: string | null, level: number) => { + items.filter(d => d.parentId === parentId).sort((a, b) => a.sortOrder - b.sortOrder).forEach(d => { + deptOptions.push({ id: d.id, label: d.name, level }) + buildDeptOptions(items, d.id, level + 1) + }) } - const deptPathMap = buildDeptPath(departments) + buildDeptOptions(departments, null, 0) const createMutation = useMutation({ mutationFn: (data: any) => editing ? api.put(`/positions/${editing.id}`, data) : api.post('/positions', data), @@ -332,7 +308,7 @@ function PositionTab({ positions, departments }: { positions: any[]; departments