index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2023-03-03 11:30:33
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2024-09-09 10:39:38
  6. * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/pubDicTypeMana/index.tsx
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import { createFromIconfontCN } from '@ant-design/icons';
  10. import { ActionType, DrawerForm, EditableFormInstance, EditableProTable, ProFormInstance, ProFormSelect } from '@ant-design/pro-components';
  11. import { ModalForm, ProFormDigit, ProFormText, ProFormTextArea } from '@ant-design/pro-form'
  12. import { ProColumns } from '@ant-design/pro-table';
  13. import { Input, message, Popconfirm } from 'antd';
  14. import { useEffect, useRef, useState } from 'react'
  15. import { addData, delData, editData, getData } from './service';
  16. import './style.less';
  17. import KCIMPagecontainer from '@/components/KCIMPageContainer';
  18. import { KCIMTable } from '@/components/KCIMTable';
  19. import KCIMDrawerForm from '@/components/KCIMDrawerForm';
  20. const IconFont = createFromIconfontCN({
  21. scriptUrl: '',
  22. });
  23. type TableDataResponse = {
  24. data: any[];
  25. success: boolean;
  26. total: number;
  27. pageSize: number;
  28. totalPage: number;
  29. };
  30. export default function FenyeTemplate() {
  31. const [tableDataFilterParams, set_tableDataFilterParams] = useState<any | undefined>();
  32. const [tableDataSearchKeywords, set_tableDataSearchKeywords] = useState<string>('');
  33. const tableRef = useRef<ActionType>();
  34. const columns: ProColumns[] = [
  35. {
  36. title: 'ID',
  37. dataIndex: 'id',
  38. },
  39. {
  40. title: '模板名称',
  41. dataIndex: 'name',
  42. },
  43. {
  44. title: '创建时间',
  45. dataIndex: 'date',
  46. },
  47. {
  48. title: '创建人',
  49. dataIndex: 'createUserName',
  50. },
  51. {
  52. title: '备注说明',
  53. dataIndex: 'remark',
  54. },
  55. {
  56. title: '操作',
  57. key: 'option',
  58. width: 120,
  59. valueType: 'option',
  60. render: (_: any, record: any) => {
  61. return [
  62. <UpDataActBtn key={'act'} record={record} type='EDIT' />,
  63. <Popconfirm
  64. title="是否确认作废?"
  65. key="del"
  66. onConfirm={() => delTableData(record)}
  67. >
  68. <a>作废</a>
  69. </Popconfirm>
  70. ]
  71. },
  72. },
  73. ]
  74. const getTableData = async (params: any):Promise<TableDataResponse|any[]> => {
  75. const resp = await getData(params);
  76. if (resp) {
  77. if(resp.totalCount == 0 && resp.currPage != 1){
  78. return getTableData({...params,current:resp.currPage - 1});
  79. }else{
  80. return {
  81. data: resp.list,
  82. success: true,
  83. total: resp.totalCount,
  84. pageSize: resp.pageSize,
  85. totalPage: resp.totalPage,
  86. }
  87. }
  88. }
  89. return []
  90. }
  91. const delTableData = async (record: any) => {
  92. const resp = await delData(record.id);
  93. if (resp) {
  94. message.success('操作成功!');
  95. tableRef.current?.reload();
  96. // message.success('操作成功!');
  97. }
  98. }
  99. const updateTable = async (formVal: any, type: 'EDIT' | "ADD") => {
  100. const content = formVal.table.map((a: any) => ({
  101. }))
  102. if (type == 'ADD') {
  103. const result = {
  104. name: formVal.name,
  105. content: JSON.stringify(formVal.table),
  106. remark: formVal.remark
  107. }
  108. const resp = await addData({ ...result });
  109. if (resp) {
  110. tableRef.current?.reload();
  111. message.success('操作成功!');
  112. }
  113. }
  114. if (type == 'EDIT') {
  115. const result = {
  116. id: formVal.id,
  117. name: formVal.name,
  118. content: JSON.stringify(formVal.table),
  119. remark: formVal.remark
  120. }
  121. const resp = await editData({ ...result });
  122. if (resp) {
  123. tableRef.current?.reload();
  124. message.success('操作成功!');
  125. }
  126. }
  127. return true;
  128. }
  129. const UpDataActBtn = ({ record, type }: { record: any, type: 'EDIT' | 'ADD' }) => {
  130. const editableFormRef = useRef<EditableFormInstance>();
  131. const formRef = useRef<ProFormInstance<any>>();
  132. const editorFormRef = useRef<EditableFormInstance<any>>();
  133. const editableColumns: ProColumns[] = [
  134. {
  135. title: '标题',
  136. dataIndex: 'title',
  137. editable: () => true
  138. },
  139. {
  140. title: '类型',
  141. dataIndex: 'filedType',
  142. editable: () => false
  143. },
  144. {
  145. title: '是否必填',
  146. dataIndex: 'required',
  147. request: async () => [
  148. {
  149. value: 1,
  150. label: '是',
  151. },
  152. {
  153. value: 0,
  154. label: '否',
  155. },
  156. ],
  157. fieldProps: (_, { rowIndex }) => {
  158. return {
  159. onSelect: () => {
  160. // 每次选中重置参数
  161. editableFormRef.current?.setRowData?.(rowIndex, { fraction: [] });
  162. },
  163. };
  164. },
  165. renderText(text) {
  166. return text ? '是' : '否'
  167. },
  168. },
  169. {
  170. title: '是否展示信息',
  171. dataIndex: 'showFiled',
  172. request: async () => [
  173. {
  174. value: 1,
  175. label: '是',
  176. },
  177. {
  178. value: 0,
  179. label: '否',
  180. },
  181. ],
  182. fieldProps: (_, { rowIndex }) => {
  183. return {
  184. onSelect: () => {
  185. // 每次选中重置参数
  186. editableFormRef.current?.setRowData?.(rowIndex, { fraction: [] });
  187. },
  188. };
  189. },
  190. renderText(text) {
  191. return text ? '是' : '否'
  192. },
  193. },
  194. {
  195. title: '操作',
  196. valueType: 'option',
  197. render: (text, record, _, action) => [
  198. <a
  199. key="editable"
  200. onClick={() => {
  201. console.log({record});
  202. action?.startEditable?.(record.tempId);
  203. }}
  204. >
  205. 编辑
  206. </a>,
  207. <a
  208. key="delete"
  209. onClick={() => {
  210. const tableDataSource = formRef.current?.getFieldValue(
  211. 'table',
  212. ) as any[];
  213. formRef.current?.setFieldsValue({
  214. table: tableDataSource.filter((item) => item.tempId !== record.tempId),
  215. });
  216. }}
  217. >
  218. 删除
  219. </a>,
  220. ],
  221. },
  222. ];
  223. let defaultTableData = JSON.parse(record.content ? record.content : '[]');
  224. defaultTableData = defaultTableData.map((a: any) => ({ ...a, tempId: (Math.random() * 1000000).toFixed(0) }));
  225. const [tempData, setTempData] = useState<any[]>(defaultTableData);
  226. // console.log({defaultTableData});
  227. return (
  228. <KCIMDrawerForm
  229. title={`${type == 'EDIT' ? '编辑' : '新增'}模板数据`}
  230. width={800}
  231. initialValues={type == 'EDIT' ? { ...record, table: defaultTableData, filedType: 'Text' } : { filedType: 'Text' }}
  232. trigger={
  233. type == 'EDIT' ? <a key="edit" >编辑</a> : <span className='add'>新增</span>
  234. }
  235. onFinish={(val: any) => {
  236. return updateTable(type == 'EDIT' ? { ...record, ...val } : { ...val }, type);
  237. }}
  238. formRef={formRef}
  239. drawerProps={{ destroyOnClose: true }}
  240. colProps={{ span: 24 }}
  241. grid
  242. >
  243. <ProFormText
  244. name="name"
  245. label="模板名称:"
  246. placeholder="请输入"
  247. width={300}
  248. rules={[{ required: true, message: '名称不能为空!' }]}
  249. />
  250. <ProFormTextArea
  251. name="remark"
  252. label="备注说明:"
  253. placeholder="请输入"
  254. />
  255. <EditableProTable
  256. className='pfm-EditableProTable'
  257. name='table'
  258. style={{margin:'0 4px'}}
  259. rowKey={'tempId'}
  260. editableFormRef={editorFormRef}
  261. tableStyle={{ padding: 0 }}
  262. columns={editableColumns}
  263. // controlled
  264. editable={{}}
  265. recordCreatorProps={{
  266. record: () => ({ tempId: (Math.random() * 1000000).toFixed(0), filedType: 'Text' }),
  267. }}
  268. />
  269. </KCIMDrawerForm>
  270. )
  271. }
  272. const tableDataSearchHandle = (paramName: string) => {
  273. set_tableDataFilterParams({
  274. ...tableDataFilterParams,
  275. [`${paramName}`]: tableDataSearchKeywords
  276. })
  277. }
  278. useEffect(() => {
  279. }, [])
  280. return (
  281. <KCIMPagecontainer className='FenyeTemplate' title={false}>
  282. <div className='toolBar'>
  283. <div className='filter'>
  284. <div className='filterItem' style={{ marginRight: 16, width: 205 }}>
  285. <span className='label' style={{ whiteSpace: 'nowrap' }}> 检索:</span>
  286. <Input placeholder={'请输入模板名称'} allowClear autoComplete='off'
  287. suffix={
  288. <IconFont type="iconsousuo" style={{ color: '#99A6BF' }} onClick={() => tableDataSearchHandle('filter')} />
  289. }
  290. onChange={(e) => {
  291. set_tableDataSearchKeywords(e.target.value);
  292. if (e.target.value.length == 0) {
  293. set_tableDataFilterParams({
  294. ...tableDataFilterParams,
  295. filter: ''
  296. });
  297. }
  298. }}
  299. onPressEnter={(e) => {
  300. set_tableDataFilterParams({
  301. ...tableDataFilterParams,
  302. filter: (e.target as HTMLInputElement).value
  303. });
  304. }}
  305. />
  306. </div>
  307. </div>
  308. <div className='btnGroup'>
  309. <UpDataActBtn record type='ADD' />
  310. </div>
  311. </div>
  312. <div style={{ marginTop: 16 }}>
  313. <KCIMTable columns={columns as ProColumns[]} actionRef={tableRef} rowKey='id' params={tableDataFilterParams} request={(params) => getTableData(params)} />
  314. </div>
  315. </KCIMPagecontainer>
  316. )
  317. }