index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {PlusOutlined} from '@ant-design/icons';
  2. import {Button, Divider, message, Modal, Radio} from 'antd';
  3. import React, {useRef, useState} from 'react';
  4. import {PageHeaderWrapper} from '@ant-design/pro-layout';
  5. import ProTable, {ActionType, ProColumns} from '@ant-design/pro-table';
  6. import {SorterResult} from 'antd/es/table/interface';
  7. import {TableListItem} from './data.d';
  8. import service from './service';
  9. /**
  10. * 添加节点
  11. * @param fields
  12. */
  13. const handleAdd = async (fields: TableListItem) => {
  14. const hide = message.loading('正在添加');
  15. try {
  16. await service.addModel({...fields});
  17. hide();
  18. message.success('添加成功');
  19. return true;
  20. } catch (error) {
  21. hide();
  22. message.error('添加失败请重试!');
  23. return false;
  24. }
  25. };
  26. /**
  27. * 更新节点
  28. * @param fields
  29. */
  30. const handleUpdate = async (fields: TableListItem) => {
  31. const hide = message.loading('正在配置');
  32. try {
  33. await service.updateModel(fields);
  34. hide();
  35. message.success('配置成功');
  36. return true;
  37. } catch (error) {
  38. hide();
  39. message.error('配置失败请重试!');
  40. return false;
  41. }
  42. };
  43. /**
  44. * 删除节点
  45. * @param fields
  46. */
  47. const handleRemove = async (fields: TableListItem) => {
  48. const hide = message.loading('正在删除');
  49. try {
  50. await service.removeModel(fields);
  51. hide();
  52. message.success('删除成功,即将刷新');
  53. return true;
  54. } catch (error) {
  55. hide();
  56. message.error('删除失败,请重试');
  57. return false;
  58. }
  59. };
  60. const TableList: React.FC<{}> = () => {
  61. const [sorter, setSorter] = useState<string>('');
  62. const [modelVisible, setModalVisible] = useState<boolean>(false);
  63. const [formValues, setFormValues] = useState({});
  64. const actionRef = useRef<ActionType>();
  65. const columns: ProColumns<TableListItem>[] = [
  66. {
  67. title: '模型类型',
  68. dataIndex: 'modelType',
  69. hideInSearch: true,
  70. rules: [
  71. {
  72. required: true,
  73. message: '模型类型为必填项',
  74. },
  75. ],
  76. renderFormItem: () => {
  77. return (
  78. <Radio.Group>
  79. <Radio value={0}>albert</Radio>
  80. <Radio value={1}>Blstm</Radio>
  81. </Radio.Group>
  82. );
  83. },
  84. valueEnum: {
  85. 0: {text: 'albert'},
  86. 1: {text: 'Blstm'}
  87. },
  88. },
  89. {
  90. title: '模型名称',
  91. dataIndex: 'modelName',
  92. rules: [
  93. {
  94. required: true,
  95. message: '模型名称为必填项',
  96. },
  97. ],
  98. },
  99. {
  100. title: '模型文件',
  101. dataIndex: 'modelFile',
  102. hideInSearch: true,
  103. rules: [
  104. {
  105. required: true,
  106. message: '模型名称为必填项',
  107. },
  108. ],
  109. },
  110. {
  111. title: '词向量文件',
  112. dataIndex: 'vocabFile',
  113. hideInSearch: true,
  114. rules: [
  115. {
  116. required: true,
  117. message: '词向量文件为必填项',
  118. },
  119. ],
  120. },
  121. {
  122. title: '标签文件',
  123. dataIndex: 'labelFile',
  124. hideInSearch: true,
  125. rules: [
  126. {
  127. required: true,
  128. message: '标签文件为必填项',
  129. },
  130. ],
  131. },
  132. {
  133. title: '必包含',
  134. dataIndex: 'mustTag',
  135. hideInSearch: true,
  136. },
  137. {
  138. title: '必不包含',
  139. dataIndex: 'mustTag',
  140. hideInSearch: true,
  141. },
  142. {
  143. title: '纯文本',
  144. dataIndex: 'onlyWord',
  145. hideInSearch: true,
  146. renderFormItem: () => {
  147. return (
  148. <Radio.Group>
  149. <Radio value="true">是</Radio>
  150. <Radio value="false">否</Radio>
  151. </Radio.Group>
  152. );
  153. },
  154. valueEnum: {
  155. "true": {text: '是'},
  156. "false": {text: '否'}
  157. },
  158. },
  159. {
  160. title: '操作',
  161. dataIndex: 'option',
  162. valueType: 'option',
  163. render: (_, record) => (
  164. <>
  165. <a
  166. onClick={() => {
  167. setModalVisible(true);
  168. setFormValues(record);
  169. console.log(record)
  170. }}
  171. >
  172. 配置
  173. </a>
  174. <Divider type="vertical"/>
  175. <a
  176. onClick={() => {
  177. handleRemove(record);
  178. console.log(record)
  179. }}
  180. >
  181. 删除
  182. </a>
  183. </>
  184. ),
  185. },
  186. ];
  187. const [searchForm,setSearchForm] = useState({});
  188. return (
  189. <PageHeaderWrapper>
  190. <ProTable<TableListItem>
  191. headerTitle="查询表格"
  192. actionRef={actionRef}
  193. rowKey="id"
  194. onChange={(_, _filter, _sorter) => {
  195. const sorterResult = _sorter as SorterResult<TableListItem>;
  196. if (sorterResult.field) {
  197. setSorter(`${sorterResult.field}_${sorterResult.order}`);
  198. }
  199. }}
  200. pagination={{
  201. defaultPageSize: 10
  202. }}
  203. params={{
  204. sorter,
  205. }}
  206. /* eslint-disable */
  207. toolBarRender={(action, {selectedRows}) => [
  208. <Button type="primary" onClick={() => {
  209. setModalVisible(true);
  210. setFormValues({});
  211. }}>
  212. <PlusOutlined/> 新建
  213. </Button>
  214. ]}
  215. request={(params) => service.queryModel(params)}
  216. columns={columns}
  217. rowSelection={{}}
  218. />
  219. <Modal
  220. destroyOnClose
  221. title={"id" in formValues ? "编辑" : "新建"}
  222. visible={modelVisible}
  223. onCancel={() => setModalVisible(false)}
  224. footer={null}
  225. >
  226. <ProTable<TableListItem, TableListItem>
  227. onSubmit={async (value) => {
  228. let success = false;
  229. if (value.id) {
  230. success = await handleAdd(value);
  231. } else {
  232. success = await handleUpdate(value);
  233. }
  234. if (success) {
  235. setModalVisible(false);
  236. if (actionRef.current) {
  237. actionRef.current.reload();
  238. }
  239. }
  240. }}
  241. rowKey="key"
  242. type="form"
  243. columns={columns}
  244. rowSelection={{}}
  245. form={{initialValues: formValues}}
  246. />
  247. </Modal>
  248. </PageHeaderWrapper>
  249. );
  250. };
  251. export default TableList;