|
@@ -0,0 +1,291 @@
|
|
|
+import React, {
|
|
|
+ useCallback,
|
|
|
+ useEffect,
|
|
|
+ useMemo,
|
|
|
+ useRef,
|
|
|
+ useState,
|
|
|
+} from "react";
|
|
|
+import styles from "./index.module.scss";
|
|
|
+import { Button, Input, Popconfirm, Table, Tooltip } from "antd";
|
|
|
+import { useDispatch } from "react-redux";
|
|
|
+import { A2_APIgetList, A2_APIsort } from "@/store/action/A2Country";
|
|
|
+import { A2TableType } from "@/types";
|
|
|
+import { ExclamationCircleFilled } from "@ant-design/icons";
|
|
|
+
|
|
|
+// 表格拖动排序-----------------
|
|
|
+import { DndProvider, useDrag, useDrop } from "react-dnd";
|
|
|
+import { HTML5Backend } from "react-dnd-html5-backend";
|
|
|
+
|
|
|
+type Props = {
|
|
|
+ setEditId: (id: number, flag: boolean) => void; //点击新增/编辑/查看
|
|
|
+ myType: "national" | "history" | "custom" | "project"; //区分各个不同板块
|
|
|
+ tableInfo: any; //表格数据
|
|
|
+};
|
|
|
+
|
|
|
+function ZA_Table({ myType, setEditId, tableInfo }: Props) {
|
|
|
+ const dispatch = useDispatch();
|
|
|
+
|
|
|
+ // 发送请求的数据
|
|
|
+ const [getData, setGetData] = useState({
|
|
|
+ pageNum: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ searchKey: "",
|
|
|
+ myType,
|
|
|
+ });
|
|
|
+
|
|
|
+ // 每次数据变化的时候发送请求
|
|
|
+
|
|
|
+ const getListFu = useCallback(
|
|
|
+ (data: any) => {
|
|
|
+ if (myType === "custom") dispatch(A2_APIgetList(data));
|
|
|
+ },
|
|
|
+ [dispatch, myType]
|
|
|
+ );
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ getListFu(getData);
|
|
|
+ }, [getData, getListFu]);
|
|
|
+
|
|
|
+ const [inputKey, setInputKey] = useState(1);
|
|
|
+
|
|
|
+ // 标题的输入
|
|
|
+ const nameTime = useRef(-1);
|
|
|
+ const nameChange = useCallback(
|
|
|
+ (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
+ clearTimeout(nameTime.current);
|
|
|
+ nameTime.current = window.setTimeout(() => {
|
|
|
+ setGetData({ ...getData, searchKey: e.target.value });
|
|
|
+ }, 500);
|
|
|
+ },
|
|
|
+ [getData]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 点击重置
|
|
|
+ const resetSelectFu = useCallback(() => {
|
|
|
+ setInputKey(Date.now());
|
|
|
+ setGetData({
|
|
|
+ pageNum: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ searchKey: "",
|
|
|
+ myType,
|
|
|
+ });
|
|
|
+ }, [myType]);
|
|
|
+
|
|
|
+ // 点击删除
|
|
|
+ const delTableFu = useCallback((id: number) => {}, []);
|
|
|
+
|
|
|
+ // 页码变化
|
|
|
+ const paginationChange = useCallback(
|
|
|
+ () => (pageNum: number, pageSize: number) => {
|
|
|
+ setGetData({ ...getData, pageNum, pageSize });
|
|
|
+ },
|
|
|
+ [getData]
|
|
|
+ );
|
|
|
+ const columns = useMemo(() => {
|
|
|
+ return [
|
|
|
+ {
|
|
|
+ width: 100,
|
|
|
+ title: (
|
|
|
+ <div className="moveTit">
|
|
|
+ 序号
|
|
|
+ <Tooltip title="按住鼠标可拖动表格调整顺序">
|
|
|
+ <div className="inco" hidden={tableInfo.list.length < 2}>
|
|
|
+ <ExclamationCircleFilled />
|
|
|
+ </div>
|
|
|
+ </Tooltip>
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ render: (_1: any, _2: any, index: number) =>
|
|
|
+ index + 1 + (getData.pageNum - 1) * getData.pageSize,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "账号名",
|
|
|
+ dataIndex: "userName",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "用户昵称",
|
|
|
+ dataIndex: "nickName",
|
|
|
+ },
|
|
|
+
|
|
|
+ {
|
|
|
+ title: "真实姓名",
|
|
|
+ dataIndex: "realName",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "注册时间",
|
|
|
+ dataIndex: "createTime",
|
|
|
+ },
|
|
|
+
|
|
|
+ {
|
|
|
+ title: "操作",
|
|
|
+ render: (item: A2TableType) => (
|
|
|
+ <>
|
|
|
+ <>
|
|
|
+ <Button
|
|
|
+ size="small"
|
|
|
+ type="text"
|
|
|
+ onClick={() => setEditId(item.id, false)}
|
|
|
+ >
|
|
|
+ 编辑
|
|
|
+ </Button>
|
|
|
+ <Popconfirm
|
|
|
+ title="删除后无法恢复,是否删除?"
|
|
|
+ okText="删除"
|
|
|
+ cancelText="取消"
|
|
|
+ onConfirm={() => delTableFu(item.id!)}
|
|
|
+ >
|
|
|
+ <Button size="small" type="text" danger>
|
|
|
+ 删除
|
|
|
+ </Button>
|
|
|
+ </Popconfirm>
|
|
|
+ </>
|
|
|
+ </>
|
|
|
+ ),
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ }, [
|
|
|
+ delTableFu,
|
|
|
+ getData.pageNum,
|
|
|
+ getData.pageSize,
|
|
|
+ setEditId,
|
|
|
+ tableInfo.list.length,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 表格拖动排序
|
|
|
+ interface DraggableBodyRowProps
|
|
|
+ extends React.HTMLAttributes<HTMLTableRowElement> {
|
|
|
+ index: number;
|
|
|
+ moveRow: (dragIndex: number, hoverIndex: number) => void;
|
|
|
+ }
|
|
|
+
|
|
|
+ const type = "DraggableBodyRow";
|
|
|
+
|
|
|
+ const DraggableBodyRow = useCallback(
|
|
|
+ ({
|
|
|
+ index,
|
|
|
+ moveRow,
|
|
|
+ className,
|
|
|
+ style,
|
|
|
+ ...restProps
|
|
|
+ }: DraggableBodyRowProps) => {
|
|
|
+ // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
+ const ref = useRef<HTMLTableRowElement>(null);
|
|
|
+ // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
+ const [{ isOver, dropClassName }, drop] = useDrop({
|
|
|
+ accept: type,
|
|
|
+ collect: (monitor) => {
|
|
|
+ const { index: dragIndex } = monitor.getItem() || {};
|
|
|
+ if (dragIndex === index) {
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ isOver: monitor.isOver(),
|
|
|
+ dropClassName:
|
|
|
+ dragIndex < index ? " drop-over-downward" : " drop-over-upward",
|
|
|
+ };
|
|
|
+ },
|
|
|
+ drop: (item: { index: number }) => {
|
|
|
+ console.log("pppppppppp", moveRow);
|
|
|
+ if (moveRow) moveRow(item.index, index);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
+ const [, drag] = useDrag({
|
|
|
+ type,
|
|
|
+ item: { index },
|
|
|
+ collect: (monitor) => ({
|
|
|
+ isDragging: monitor.isDragging(),
|
|
|
+ }),
|
|
|
+ });
|
|
|
+ drop(drag(ref));
|
|
|
+
|
|
|
+ return (
|
|
|
+ <tr
|
|
|
+ ref={ref}
|
|
|
+ className={`${className}${isOver ? dropClassName : ""}`}
|
|
|
+ style={{ cursor: "move", ...style }}
|
|
|
+ {...restProps}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ },
|
|
|
+ []
|
|
|
+ );
|
|
|
+
|
|
|
+ const components = {
|
|
|
+ body: {
|
|
|
+ row: DraggableBodyRow,
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ const moveRow = useCallback(
|
|
|
+ async (dragIndex: number, hoverIndex: number) => {
|
|
|
+ if (dragIndex === hoverIndex) return;
|
|
|
+ // 交互位置-之前的id
|
|
|
+ const beforeId = tableInfo.list[dragIndex].id;
|
|
|
+ const afterId = tableInfo.list[hoverIndex].id;
|
|
|
+ console.log("交换位置", beforeId, afterId);
|
|
|
+
|
|
|
+ const res = await A2_APIsort(beforeId, afterId);
|
|
|
+
|
|
|
+ if (res.code === 0) {
|
|
|
+ if (myType === "custom") dispatch(A2_APIgetList(getData));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [dispatch, getData, myType, tableInfo.list]
|
|
|
+ );
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className={styles.ZA_Table}>
|
|
|
+ {/* 顶部搜索 */}
|
|
|
+ <div className="top">
|
|
|
+ <span>标题:</span>
|
|
|
+ <Input
|
|
|
+ key={inputKey}
|
|
|
+ maxLength={50}
|
|
|
+ style={{ width: 300 }}
|
|
|
+ placeholder="请输入关键字"
|
|
|
+ allowClear
|
|
|
+ onChange={(e) => nameChange(e)}
|
|
|
+ />
|
|
|
+   
|
|
|
+ <Button onClick={resetSelectFu}>重置</Button>
|
|
|
+   
|
|
|
+ <Button type="primary" onClick={() => setEditId(-1, false)}>
|
|
|
+ 新增
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ {/* 表格主体 */}
|
|
|
+ <div className="tableBox">
|
|
|
+ <DndProvider backend={HTML5Backend}>
|
|
|
+ <Table
|
|
|
+ scroll={{ y: 617 }}
|
|
|
+ columns={columns}
|
|
|
+ dataSource={tableInfo.list}
|
|
|
+ components={components}
|
|
|
+ rowKey="id"
|
|
|
+ pagination={{
|
|
|
+ showQuickJumper: true,
|
|
|
+ position: ["bottomCenter"],
|
|
|
+ showSizeChanger: true,
|
|
|
+ current: getData.pageNum,
|
|
|
+ pageSize: getData.pageSize,
|
|
|
+ total: tableInfo.total,
|
|
|
+ onChange: paginationChange(),
|
|
|
+ }}
|
|
|
+ onRow={(_, index) => {
|
|
|
+ const attr = {
|
|
|
+ index,
|
|
|
+ moveRow,
|
|
|
+ };
|
|
|
+ return attr as React.HTMLAttributes<any>;
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </DndProvider>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+const MemoZA_Table = React.memo(ZA_Table);
|
|
|
+
|
|
|
+export default MemoZA_Table;
|