index.tsx
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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;