index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import React, { useEffect, useState } from 'react';
  2. import { PageContainer } from '@ant-design/pro-components';
  3. import { Button, Card, Form, Input, message, 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 { delColumn, queryColumn } from '@/services/cms/ColumnList';
  8. import Edit from '@/pages/cms/ColumnList/edit';
  9. interface DataType {
  10. name: string;
  11. record_id: string;
  12. }
  13. /**
  14. * 栏目列表
  15. * @constructor
  16. */
  17. const ColumnList: React.FC = () => {
  18. const [form] = Form.useForm();
  19. const [loading, setLoading] = useState(false);
  20. const [searchData, setSearchData] = useState<object | null>({});
  21. const [visible, setVisible] = useState(false);
  22. const [editData, setEditData] = useState(null);
  23. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  24. const [dataList, setDataList]: any = useState([]);
  25. // 获取列表数据
  26. const getList = () => {
  27. const params = {
  28. q: 'page',
  29. current: pagination.current,
  30. pageSize: pagination.pageSize,
  31. ...searchData,
  32. };
  33. queryColumn(params).then((res) => {
  34. if (res && res.code === 0) {
  35. setDataList(res.data.list);
  36. setPagination(res.data.pagination);
  37. setLoading(false);
  38. }
  39. });
  40. };
  41. useEffect(() => {
  42. setLoading(true);
  43. getList();
  44. }, []);
  45. // 搜索
  46. const onFinish = () => {
  47. form.validateFields().then((data) => {
  48. setLoading(true);
  49. setSearchData(data);
  50. });
  51. };
  52. // 重置
  53. const onReset = () => {
  54. form.resetFields();
  55. setLoading(true);
  56. setSearchData(null);
  57. };
  58. // 新增弹框
  59. const onAdd = () => {
  60. setEditData(null);
  61. setVisible(true);
  62. };
  63. // 打开编辑弹框
  64. const toEdit = (data: any) => {
  65. setEditData(data);
  66. setVisible(true);
  67. };
  68. // 分页切换
  69. const tableChange = (page: any) => {
  70. setLoading(true);
  71. const params = {
  72. q: 'page',
  73. current: page.current,
  74. pageSize: page.pageSize,
  75. ...searchData,
  76. };
  77. queryColumn(params).then((res) => {
  78. if (res.code === 0) {
  79. setDataList(res.data.list);
  80. setPagination(res.data.pagination);
  81. setLoading(false);
  82. }
  83. });
  84. };
  85. // 删除
  86. const toDel = (record: any) => {
  87. console.log(record);
  88. Modal.confirm({
  89. title: '删除',
  90. content: `确认删除项目:[${record.name}]`,
  91. onOk: () => {
  92. delColumn(record?.record_id)
  93. .then((res) => {
  94. if (res.code === 0) {
  95. message.success('删除成功');
  96. } else {
  97. message.error('删除失败');
  98. }
  99. })
  100. .catch((e) => {
  101. message.error(e?.message);
  102. });
  103. },
  104. });
  105. console.log(record);
  106. };
  107. // 编辑弹框回调
  108. const handleEdit = () => {
  109. setVisible(false);
  110. getList();
  111. };
  112. const columns: ColumnsType<DataType> = [
  113. {
  114. title: '序号',
  115. align: 'center',
  116. key: 'index',
  117. render: (_: any, row: any, index: number) => index + 1,
  118. },
  119. {
  120. title: '栏目名称',
  121. dataIndex: 'name',
  122. key: 'name',
  123. },
  124. {
  125. title: '编号',
  126. dataIndex: 'code',
  127. key: 'code',
  128. },
  129. {
  130. title: '上级栏目名称',
  131. dataIndex: 'parent_name',
  132. key: 'parent_name',
  133. },
  134. {
  135. title: '状态',
  136. dataIndex: 'status',
  137. key: 'status',
  138. render: (v) => v && <span>{{ 1: '启用', 2: '停用' }[v]}</span>,
  139. },
  140. {
  141. title: '创建者',
  142. dataIndex: 'creator',
  143. key: 'creator',
  144. },
  145. {
  146. title: '创建时间',
  147. dataIndex: 'created_at',
  148. key: 'created_at',
  149. render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
  150. },
  151. {
  152. title: '备注',
  153. dataIndex: 'memo',
  154. key: 'memo',
  155. },
  156. {
  157. title: '操作',
  158. key: 'action',
  159. render: (_, record) => (
  160. <Space size="middle">
  161. <a
  162. onClick={() => {
  163. toEdit(record);
  164. }}
  165. >
  166. 编辑
  167. </a>
  168. <a
  169. style={{ color: 'red' }}
  170. onClick={() => {
  171. toDel(record);
  172. }}
  173. >
  174. 删除
  175. </a>
  176. </Space>
  177. ),
  178. },
  179. ];
  180. const paginationProps = {
  181. showSizeChanger: true,
  182. showQuickJumper: true,
  183. showTotal: (total: number) => {
  184. return <span> 共 {total}条 </span>;
  185. },
  186. ...pagination,
  187. };
  188. return (
  189. <PageContainer>
  190. <div>
  191. <Card>
  192. <Form form={form} layout="inline" onFinish={onFinish}>
  193. <Form.Item name="name" label="栏目名称">
  194. <Input placeholder="请输入栏目名称" />
  195. </Form.Item>
  196. <Form.Item style={{ marginBottom: '10px' }}>
  197. <Space>
  198. <Button type="primary" htmlType="submit">
  199. <SearchOutlined />
  200. 查询
  201. </Button>
  202. <Button htmlType="button" onClick={onReset}>
  203. <ReloadOutlined />
  204. 重置
  205. </Button>
  206. </Space>
  207. </Form.Item>
  208. </Form>
  209. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  210. <PlusCircleOutlined />
  211. 新增栏目
  212. </Button>
  213. <Table
  214. columns={columns}
  215. dataSource={dataList}
  216. rowKey={(record) => record.record_id}
  217. pagination={paginationProps}
  218. loading={loading}
  219. onChange={tableChange}
  220. />
  221. {visible && <Edit visible={visible} editCallback={handleEdit} params={editData} />}
  222. </Card>
  223. </div>
  224. </PageContainer>
  225. );
  226. };
  227. export default ColumnList;