A2Table3.tsx 5.7 KB

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