diff --git a/src/pages/department/index.tsx b/src/pages/department/index.tsx index c43dfb5..dc3ea5f 100644 --- a/src/pages/department/index.tsx +++ b/src/pages/department/index.tsx @@ -1,19 +1,19 @@ import React, { useState, useEffect } from "react"; -import { Button, Space, Table, Popconfirm, message } from "antd"; +import { Button, Space, Tree, Modal, message } from "antd"; import type { ColumnsType } from "antd/es/table"; import styles from "./index.module.less"; -import { PlusOutlined, ReloadOutlined } from "@ant-design/icons"; +import { PlusOutlined, ExclamationCircleFilled } from "@ant-design/icons"; import { department } from "../../api/index"; -import { dateFormat } from "../../utils/index"; import { Link, useNavigate } from "react-router-dom"; import { PerButton } from "../../compenents"; +import type { DataNode, TreeProps } from "antd/es/tree"; + +const { confirm } = Modal; interface Option { - id: string | number; - name: string; - created_at: string; + key: string | number; + title: any; children?: Option[]; - sort: number; } interface DataType { @@ -26,68 +26,14 @@ interface DataType { export const DepartmentPage: React.FC = () => { const navigate = useNavigate(); const [loading, setLoading] = useState(true); - const [list, setList] = useState([]); const [refresh, setRefresh] = useState(false); - const [tableKey, setTableKey] = useState(0); + const [treeData, setTreeData] = useState([]); + const [selectKey, setSelectKey] = useState([]); - const columns: ColumnsType = [ - { - title: "部门名", - dataIndex: "name", - render: (text: string) => {text}, - }, - { - title: "ID", - key: "id", - dataIndex: "id", - }, - { - title: "Sort", - key: "sort", - dataIndex: "sort", - }, - { - title: "时间", - dataIndex: "created_at", - render: (text: string) => {text && dateFormat(text)}, - }, - { - title: "操作", - key: "action", - fixed: "right", - width: 160, - render: (_, record) => ( - - navigate(`/department/update/${record.id}`)} - disabled={null} - /> - delUser(record.id)} - okText="确定" - cancelText="取消" - > - null} - disabled={null} - /> - - - ), - }, - ]; + const onSelect = (selectedKeys: any, info: any) => { + setSelectKey(selectedKeys); + console.log(selectedKeys); + }; useEffect(() => { getData(); @@ -99,11 +45,10 @@ export const DepartmentPage: React.FC = () => { const departments = res.data.departments; if (JSON.stringify(departments) !== "{}") { const new_arr: Option[] = checkArr(departments, 0); - setList(new_arr); + setTreeData(new_arr); } - let num = tableKey; + setLoading(false); - setTableKey(num + 1); }); }; @@ -112,18 +57,68 @@ export const DepartmentPage: React.FC = () => { for (let i = 0; i < departments[id].length; i++) { if (!departments[departments[id][i].id]) { arr.push({ - name: departments[id][i].name, - id: departments[id][i].id, - sort: departments[id][i].sort, - created_at: departments[id][i].created_at, + title: ( +
+
{departments[id][i].name}
+ + + navigate(`/department/update/${departments[id][i].id}`) + } + disabled={null} + /> +
+ delUser(departments[id][i].id)} + disabled={null} + /> +
+
+ ), + key: departments[id][i].id, }); } else { const new_arr: Option[] = checkArr(departments, departments[id][i].id); arr.push({ - name: departments[id][i].name, - id: departments[id][i].id, - created_at: departments[id][i].created_at, - sort: departments[id][i].sort, + title: ( +
+
{departments[id][i].name}
+ + + navigate(`/department/update/${departments[id][i].id}`) + } + disabled={null} + /> +
+ delUser(departments[id][i].id)} + disabled={null} + /> +
+
+ ), + key: departments[id][i].id, children: new_arr, }); } @@ -132,56 +127,131 @@ export const DepartmentPage: React.FC = () => { }; const resetData = () => { - setList([]); + setTreeData([]); setRefresh(!refresh); }; const delUser = (id: any) => { - department.destroyDepartment(id).then((res: any) => { - message.success("操作成功"); - resetData(); + if (id === 0) { + return; + } + + confirm({ + title: "操作确认", + icon: , + content: "确认删除此部门?", + okText: "确认", + okType: "danger", + cancelText: "取消", + onOk() { + department.destroyDepartment(id).then((res: any) => { + message.success("操作成功"); + resetData(); + }); + }, + onCancel() { + console.log("Cancel"); + }, }); }; + const onDragEnter: TreeProps["onDragEnter"] = (info) => { + console.log(info); + // expandedKeys 需要受控时设置 + // setExpandedKeys(info.expandedKeys) + }; + + const onDrop: TreeProps["onDrop"] = (info) => { + const dropKey = info.node.key; + const dragKey = info.dragNode.key; + const dropPos = info.node.pos.split("-"); + const dropPosition = + info.dropPosition - Number(dropPos[dropPos.length - 1]); + + const loop = ( + data: DataNode[], + key: React.Key, + callback: (node: DataNode, i: number, data: DataNode[]) => void + ) => { + for (let i = 0; i < data.length; i++) { + if (data[i].key === key) { + return callback(data[i], i, data); + } + if (data[i].children) { + loop(data[i].children!, key, callback); + } + } + }; + const data = [...treeData]; + + // Find dragObject + let dragObj: DataNode; + loop(data, dragKey, (item, index, arr) => { + arr.splice(index, 1); + dragObj = item; + }); + + if (!info.dropToGap) { + // Drop on the content + loop(data, dropKey, (item) => { + item.children = item.children || []; + // where to insert 示例添加到头部,可以是随意位置 + item.children.unshift(dragObj); + }); + } else if ( + ((info.node as any).props.children || []).length > 0 && // Has children + (info.node as any).props.expanded && // Is expanded + dropPosition === 1 // On the bottom gap + ) { + loop(data, dropKey, (item) => { + item.children = item.children || []; + // where to insert 示例添加到头部,可以是随意位置 + item.children.unshift(dragObj); + // in previous version, we use item.children.push(dragObj) to insert the + // item to the tail of the children + }); + } else { + let ar: DataNode[] = []; + let i: number; + loop(data, dropKey, (_item, index, arr) => { + ar = arr; + i = index; + }); + if (dropPosition === -1) { + ar.splice(i!, 0, dragObj!); + } else { + ar.splice(i! + 1, 0, dragObj!); + } + } + setTreeData(data); + }; + return ( <> +
+
+ + } + p="department-cud" + onClick={() => null} + disabled={null} + /> + +
+
-
-
- - } - p="department-cud" - onClick={() => null} - disabled={null} - /> - -
-
- -
-
-
- record.id} - defaultExpandAllRows={true} - /> - + );