index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import React, { useEffect, useState } from 'react';
  2. import { PageContainer } from '@ant-design/pro-components';
  3. import { Button, Card, Form, Input, Modal, Space, Table } from 'antd';
  4. import { PlusCircleOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  5. import type { ColumnsType } from 'antd/es/table';
  6. import moment from 'moment';
  7. import Edit from '@/pages/RoleManagement/edit';
  8. interface DataType {
  9. name: string;
  10. sequence: number;
  11. creator: string;
  12. record_id: string;
  13. }
  14. const RoleManagement: React.FC = () => {
  15. const [form] = Form.useForm();
  16. const [loading, setLoading] = useState(false);
  17. const pagination = useState({});
  18. const [dataList, setDataList] = useState([]);
  19. const [editData, setEditData] = useState<object | null>({});
  20. const [visible, setVisible] = useState(false);
  21. const [searchData, setSearchData] = useState<object | null>({});
  22. const getList = () => {
  23. const data: any = [
  24. {
  25. name: '角色名称',
  26. sequence: 1,
  27. creator: '创建者',
  28. created_at: '2023-01-04T14:31:23.664+08:00',
  29. memo: '备注备注备注备注',
  30. },
  31. ];
  32. setDataList(data);
  33. };
  34. useEffect(() => {
  35. getList();
  36. }, []);
  37. // 搜索
  38. const onFinish = () => {
  39. form.validateFields().then((data) => {
  40. setLoading(true);
  41. setSearchData(data);
  42. });
  43. };
  44. useEffect(() => {
  45. getList();
  46. }, [searchData]);
  47. // 重置
  48. const onReset = () => {
  49. form.resetFields();
  50. setLoading(true);
  51. setSearchData(null);
  52. };
  53. // 新增
  54. const onAdd = () => {
  55. setEditData(null);
  56. setVisible(true);
  57. };
  58. // 分页切换
  59. const tableChange = (page: any) => {
  60. setLoading(true);
  61. const params = {
  62. q: 'page',
  63. current: page.current,
  64. pageSize: page.pageSize,
  65. ...searchData,
  66. };
  67. console.log(params);
  68. // 请求接口
  69. };
  70. // 编辑回调
  71. const handleEdit = () => {
  72. setVisible(false);
  73. getList();
  74. };
  75. // 编辑
  76. const toEdit = (data: any) => {
  77. setEditData(data);
  78. setVisible(true);
  79. };
  80. // 删除
  81. const toDel = (record: any) => {
  82. Modal.confirm({
  83. title: '删除',
  84. content: `确认删除角色:[${record.name}]`,
  85. onOk: () => {
  86. // 请求接口
  87. },
  88. });
  89. };
  90. const columns: ColumnsType<DataType> = [
  91. {
  92. title: '序号',
  93. align: 'center',
  94. key: 'index',
  95. render: (_: any, row: any, index: number) => index + 1,
  96. },
  97. {
  98. title: '角色名称',
  99. dataIndex: 'name',
  100. key: 'name',
  101. width: 250,
  102. },
  103. {
  104. title: '排序值',
  105. dataIndex: 'sequence',
  106. key: 'sequence',
  107. width: 250,
  108. },
  109. {
  110. title: '创建者',
  111. dataIndex: 'creator',
  112. key: 'creator',
  113. },
  114. {
  115. title: '创建时间',
  116. dataIndex: 'created_at',
  117. key: 'created_at',
  118. render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
  119. },
  120. {
  121. title: '备注',
  122. dataIndex: 'memo',
  123. key: 'memo',
  124. width: 250,
  125. },
  126. {
  127. title: '操作',
  128. key: 'action',
  129. render: (_, record) => (
  130. <Space size="middle">
  131. <a
  132. onClick={() => {
  133. toEdit(record);
  134. }}
  135. >
  136. 编辑
  137. </a>
  138. <a
  139. style={{ color: 'red' }}
  140. onClick={() => {
  141. toDel(record);
  142. }}
  143. >
  144. 删除
  145. </a>
  146. </Space>
  147. ),
  148. },
  149. ];
  150. const paginationProps = {
  151. showSizeChanger: true,
  152. showQuickJumper: true,
  153. showTotal: (total: number) => {
  154. return <span> 共 {total}条 </span>;
  155. },
  156. ...pagination,
  157. };
  158. return (
  159. <PageContainer>
  160. <div>
  161. <Card>
  162. <Form form={form} layout="inline" onFinish={onFinish}>
  163. <Form.Item name="name" label="角色名称">
  164. <Input placeholder="请输入角色名称" />
  165. </Form.Item>
  166. <Form.Item style={{ marginBottom: '10px' }}>
  167. <Space>
  168. <Button type="primary" htmlType="submit">
  169. <SearchOutlined />
  170. 查询
  171. </Button>
  172. <Button htmlType="button" onClick={onReset}>
  173. <ReloadOutlined />
  174. 重置
  175. </Button>
  176. </Space>
  177. </Form.Item>
  178. </Form>
  179. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  180. <PlusCircleOutlined />
  181. 新增角色
  182. </Button>
  183. <Table
  184. columns={columns}
  185. dataSource={dataList}
  186. rowKey={(record) => record.record_id}
  187. pagination={paginationProps}
  188. loading={loading}
  189. onChange={tableChange}
  190. />
  191. {visible && <Edit visible={visible} editCallback={handleEdit} params={editData} />}
  192. </Card>
  193. </div>
  194. </PageContainer>
  195. );
  196. };
  197. export default RoleManagement;