index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React, { useCallback, useEffect, useMemo } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Popconfirm, Table } from "antd";
  4. import { useDispatch, useSelector } from "react-redux";
  5. import { A5_APIgetList, A5_APIsort } from "@/store/action/A5Section";
  6. import { RootState } from "@/store";
  7. import { A5TableType } from "@/types";
  8. import { MessageFu } from "@/utils/message";
  9. function A5Section() {
  10. const dispatch = useDispatch();
  11. const getListFu = useCallback(() => {
  12. dispatch(A5_APIgetList());
  13. }, [dispatch]);
  14. useEffect(() => {
  15. getListFu();
  16. }, [getListFu]);
  17. // 获取表格数据
  18. const tableList = useSelector(
  19. (state: RootState) => state.A5Section.tableList
  20. );
  21. // 树型数组扁平化
  22. const arrAllArr = useMemo(() => {
  23. const arr: A5TableType[] = [...tableList];
  24. const arr1: A5TableType[] = [];
  25. arr.forEach((v) => {
  26. arr1.push(v);
  27. if (v.children && v.children.length) {
  28. v.children.forEach((v2) => {
  29. arr1.push(v2);
  30. if (v2.children && v2.children.length) {
  31. v2.children.forEach((v3) => {
  32. arr1.push(v3);
  33. if (v3.children && v3.children.length) {
  34. v3.children.forEach((v4) => {
  35. arr1.push(v4);
  36. });
  37. }
  38. });
  39. }
  40. });
  41. }
  42. });
  43. return arr1;
  44. }, [tableList]);
  45. // 拿到当前点击 级别的 数组
  46. const sonArrFu = useCallback(
  47. (fId: string) => {
  48. const arr: A5TableType[] = arrAllArr.filter((v) => v.parentId === fId);
  49. return arr;
  50. },
  51. [arrAllArr]
  52. );
  53. // 点击删除
  54. const delById = useCallback((id: string) => {}, []);
  55. // 点击新增子部门
  56. const addTree = useCallback((item: A5TableType) => {
  57. console.log("----", item);
  58. }, []);
  59. // 点击 上移 和下移
  60. const sortMoveFu = useCallback(
  61. async (flag: 1 | -1, index: number, oldId: string, fId: string) => {
  62. const arr = sonArrFu(fId);
  63. const newId = arr[index + flag].id;
  64. const res = await A5_APIsort(oldId, newId);
  65. if (res.code === 0) {
  66. getListFu();
  67. MessageFu.success("排序修改成功!");
  68. }
  69. },
  70. [getListFu, sonArrFu]
  71. );
  72. const columns = useMemo(() => {
  73. return [
  74. {
  75. title: <>&emsp;&nbsp;部门名称</>,
  76. dataIndex: "name",
  77. },
  78. {
  79. title: "说明",
  80. render: (item: A5TableType) =>
  81. item.description ? (
  82. item.description.length >= 30 ? (
  83. <span style={{ cursor: "pointer" }} title={item.description}>
  84. {item.description.substring(0, 30) + "..."}
  85. </span>
  86. ) : (
  87. item.description
  88. )
  89. ) : (
  90. "(空)"
  91. ),
  92. },
  93. {
  94. title: "同级排序",
  95. dataIndex: "sort",
  96. },
  97. {
  98. title: "操作",
  99. render: (item: A5TableType, _: any, index: number) => (
  100. <>
  101. <Button size="small" type="text" onClick={() => addTree(item)}>
  102. 新增子部门
  103. </Button>
  104. <Button size="small" type="text">
  105. 编辑
  106. </Button>
  107. {index === 0 ? null : (
  108. <Button
  109. size="small"
  110. type="text"
  111. onClick={() => sortMoveFu(-1, index, item.id, item.parentId)}
  112. >
  113. 上移
  114. </Button>
  115. )}
  116. {sonArrFu(item.parentId).length - 1 === index ? null : (
  117. <Button
  118. size="small"
  119. type="text"
  120. onClick={() => sortMoveFu(1, index, item.id, item.parentId)}
  121. >
  122. 下移
  123. </Button>
  124. )}
  125. <Popconfirm
  126. title="删除后无法恢复,是否删除?"
  127. okText="删除"
  128. cancelText="取消"
  129. onConfirm={() => delById(item.id)}
  130. okButtonProps={{ loading: false }}
  131. >
  132. <Button size="small" type="text" danger>
  133. 删除
  134. </Button>
  135. </Popconfirm>
  136. </>
  137. ),
  138. },
  139. ];
  140. }, [addTree, delById, sonArrFu, sortMoveFu]);
  141. return (
  142. <div className={styles.A5Section}>
  143. <div className="pageTitle">部门管理</div>
  144. <div className="A5Main">
  145. <div className="A5Top">
  146. <Button type="primary">新增</Button>
  147. </div>
  148. {/* 表格主体 */}
  149. <div className="A5tableBox">
  150. <Table
  151. scroll={{ y: 575 }}
  152. dataSource={tableList}
  153. columns={columns}
  154. rowKey="id"
  155. pagination={false}
  156. />
  157. </div>
  158. </div>
  159. </div>
  160. );
  161. }
  162. const MemoA5Section = React.memo(A5Section);
  163. export default MemoA5Section;