index.tsx 5.14 KB
import React, { useEffect, useState, useCallback } from "react";
import {
  Button,
  Divider,
  Input,
  Row,
  Col,
  Popconfirm,
  Table,
  message,
  Select,
  Form,
} from "antd";
import { PlusOutlined, MinusOutlined } from "@ant-design/icons";
import Edit from "./components/Edit";
import { ITarget, IParams } from "./model";
import { TargetTypeFilter } from "@/utils/constant";
import { getTargetListApi, targetDeleteApi } from "@/api/target";
const { Column } = Table;
const User = () => {
  const [search] = Form.useForm();
  // 查询参数
  const [params, setParams] = useState<IParams>({
    pageNum: 1,
    pageSize: 10,
  });
  // 表格当前页显示的数据
  const [list, setList] = useState<ITarget[]>([]);
  // 数据总数
  const [total, setTotal] = useState(0);
  // 表格loading状态
  const [loading, setLoading] = useState(false);
  // 编辑模态窗是否显示
  const [editVisible, setEditVisible] = useState(false);
  // 当前正在编辑的分类的id
  const [targetId, setTargetId] = useState<number>(0);

  const getTargetList = useCallback(async () => {
    setLoading(true);
    const res = await getTargetListApi({ params });
    setLoading(false);
    setList(res.data.rows);
    setTotal(res.data.count);
  }, [params]);
  // 获取分类列表
  useEffect(() => {
    getTargetList();
  }, [getTargetList]);

  // 搜索
  const handleSearch = (values: any) => {
    setParams((state: any) => ({ ...state, ...values, pageNum: 1 }));
  };

  // 翻页
  const handlePagination = (pageNum: number, pageSize?: number) => {
    setParams((state: any) => ({
      ...state,
      pageNum,
      pageSize: pageSize!,
      // type: search.getFieldValue("type"),
    }));
  };

  // 新增或编辑
  const handleEdit = (id: number) => {
    setTargetId(id);
    setTimeout(() => {
      setEditVisible(true);
    }, 300);
  };

  // 单个删除
  const handleDeleteSingle = async (record: ITarget) => {
    const { id, name } = record;
    await targetDeleteApi({ id });
    message.success(`成功删除“${name}”!`);
    getTargetList();
  };
  // 取消
  const handleClose = () => {
    setEditVisible(false);
    getTargetList();
  };

  return (
    <div className="page-user">
      <div className="add-btn">
        <Button.Group>
          <Button
            type="primary"
            icon={<PlusOutlined />}
            onClick={handleEdit.bind(null, 0)}
          >
            新增
          </Button>
        </Button.Group>
      </div>
      <Row>
        <Form
          form={search}
          layout="inline"
          colon
          labelAlign="left"
          onFinish={handleSearch}
        >
          <Form.Item label="指标归属" name="type">
            <Select
              style={{ width: 220 }}
              options={TargetTypeFilter}
              fieldNames={{ label: "name", value: "id" }}
            />
          </Form.Item>
          <Form.Item wrapperCol={{ offset: 8 }}>
            <Button type="primary" htmlType="submit">
              查询
            </Button>
          </Form.Item>
        </Form>
      </Row>

      <Table<ITarget>
        dataSource={list}
        rowKey="id"
        loading={loading}
        pagination={{
          total,
          current: params.pageNum,
          pageSize: params.pageSize,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total) => `共${total}个分类`,
          onChange: handlePagination,
        }}
      >
        <Column
          title="序号"
          dataIndex="number"
          width={80}
          render={(value, record, index) =>
            (params.pageNum - 1) * params.pageSize + index + 1
          }
        />
        <Column title="指标" dataIndex="name" />
        <Column title="指标说明" dataIndex="illustrate" />
        <Column title="权重" width={80} dataIndex="weight" />
        <Column title="高度" width={80} dataIndex="height" />
        <Column title="排序" width={80} dataIndex="order" />
        <Column title="模块" width={100} dataIndex="module" />
        <Column
          title="指标归属"
          width={100}
          dataIndex="type"
          render={(value) =>
            TargetTypeFilter.find((i: any) => i.id == value)?.name
          }
        />
        <Column title="创建时间" width={180} dataIndex="createDate" />
        <Column<ITarget>
          title="操作"
          dataIndex="operate"
          width={140}
          render={(value, record) => (
            <div>
              <Button
                type="link"
                size="small"
                onClick={handleEdit.bind(null, record.id)}
              >
                编辑
              </Button>
              <Divider type="vertical" />
              <Popconfirm
                title="确定删除这条数据吗?"
                onConfirm={handleDeleteSingle.bind(null, record)}
              >
                <Button type="link" size="small" danger={true}>
                  删除
                </Button>
              </Popconfirm>
            </div>
          )}
        />
      </Table>

      <Edit visible={editVisible} id={targetId} onClose={handleClose} />
    </div>
  );
};

export default User;