index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import A1look from './A1look'
  4. import { useDispatch, useSelector } from 'react-redux'
  5. import { A1_APIdel, A1_APIgetList } from '@/store/action/A1record'
  6. import { RootState } from '@/store'
  7. import { MessageFu } from '@/utils/message'
  8. import { Button, Input, Table } from 'antd'
  9. import MyPopconfirm from '@/components/MyPopconfirm'
  10. import { A1ListType, jiGuanFu, LookInfoType } from './data'
  11. import A1add from './A1add'
  12. import ImageLazy from '@/components/ImageLazy'
  13. const baseFromData = {
  14. pageNum: 1,
  15. pageSize: 10,
  16. searchKey: ''
  17. }
  18. function A1record() {
  19. const dispatch = useDispatch()
  20. const [fromData, setFromData] = useState(baseFromData)
  21. const getListFu = useCallback(() => {
  22. dispatch(A1_APIgetList(fromData))
  23. }, [dispatch, fromData])
  24. useEffect(() => {
  25. getListFu()
  26. }, [getListFu])
  27. // 作品名称的输入
  28. const timeRef = useRef(-1)
  29. const [inputKey, setInputKey] = useState(0)
  30. const fromKeyChangeFu = useCallback(
  31. (e: React.ChangeEvent<HTMLInputElement>, key: 'searchKey') => {
  32. clearTimeout(timeRef.current)
  33. timeRef.current = window.setTimeout(() => {
  34. setFromData({ ...fromData, [key]: e.target.value, pageNum: 1 })
  35. }, 500)
  36. },
  37. [fromData]
  38. )
  39. const tableInfo = useSelector((state: RootState) => state.A1record.tableInfo)
  40. // 点击重置
  41. const resetSelectFu = useCallback(() => {
  42. setInputKey(Date.now())
  43. setFromData({ ...baseFromData })
  44. }, [])
  45. // 点击查看
  46. const [lookInfo, setLookInfo] = useState<LookInfoType>({
  47. id: 0,
  48. txt: ''
  49. })
  50. const delTableFu = useCallback(
  51. async (id: number) => {
  52. const res = await A1_APIdel(id)
  53. if (res.code === 0) {
  54. MessageFu.success('删除成功!')
  55. getListFu()
  56. }
  57. },
  58. [getListFu]
  59. )
  60. // 表格相关
  61. const columns = useMemo(() => {
  62. const arr: any = [
  63. { title: 'ID', dataIndex: 'num', width: 100 },
  64. {
  65. title: '封面图',
  66. width: 80,
  67. render: (item: A1ListType) => (
  68. <div className='tableImgAuto'>
  69. <ImageLazy
  70. width={60}
  71. height={60}
  72. src={item.thumb || item.thumbPc}
  73. srcBig={item.thumbPc || item.thumb}
  74. />
  75. </div>
  76. )
  77. },
  78. { title: '姓名', render: (item: A1ListType) => item.name, width: 100 },
  79. {
  80. title: '性别',
  81. width: 100,
  82. render: (item: A1ListType) => (item.gender === 1 ? '男' : '女')
  83. },
  84. { title: '民族', width: 100, render: (item: A1ListType) => item.nation || '(空)' },
  85. { title: '番号', render: (item: A1ListType) => item.intro || '(空)' },
  86. {
  87. title: '籍贯',
  88. render: (item: A1ListType) => jiGuanFu(item)
  89. },
  90. {
  91. title: '生卒',
  92. width: 200,
  93. render: (item: A1ListType) =>
  94. (item.dateStart || '(空)') + ' ~ ' + (item.dateEnd || '(空)')
  95. },
  96. {
  97. title: '备注',
  98. render: (item: A1ListType) =>
  99. item.remark ? (
  100. item.remark.length >= 50 ? (
  101. <span style={{ cursor: 'pointer' }} title={item.remark}>
  102. {item.remark.substring(0, 50) + '...'}
  103. </span>
  104. ) : (
  105. item.remark
  106. )
  107. ) : (
  108. '(空)'
  109. )
  110. },
  111. { title: '编辑时间', width: 150, render: (item: A1ListType) => item.updateTime },
  112. { title: '编辑人', width: 100, render: (item: A1ListType) => item.creatorName },
  113. {
  114. title: '操作',
  115. fixed: 'right',
  116. width: 150,
  117. render: (item: A1ListType) => (
  118. <>
  119. <Button
  120. size='small'
  121. type='text'
  122. onClick={() => setLookInfo({ id: item.id, txt: '查看' })}
  123. >
  124. 查看
  125. </Button>
  126. <Button
  127. size='small'
  128. type='text'
  129. onClick={() => setLookInfo({ id: item.id, txt: '编辑' })}
  130. >
  131. 编辑
  132. </Button>
  133. <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.id)} />
  134. </>
  135. )
  136. }
  137. ]
  138. return arr
  139. }, [delTableFu])
  140. return (
  141. <div className={styles.A1record}>
  142. <div className='pageTitle'>
  143. 烈士档案{lookInfo.txt ? ` - ${lookInfo.txt}` : ''}{' '}
  144. {lookInfo.txt === '查看' ? (
  145. <div onClick={() => setLookInfo({ id: 0, txt: '' })} className='A1LookBack'>
  146. <Button>返回</Button>
  147. </div>
  148. ) : null}
  149. </div>
  150. {/* 顶部筛选 */}
  151. <div className='A1top'>
  152. <div className='A1topLeft'>
  153. <div>
  154. <Input
  155. key={inputKey}
  156. maxLength={30}
  157. showCount
  158. style={{ width: 300 }}
  159. placeholder='请输入ID/姓名/番号/籍贯'
  160. allowClear
  161. onChange={e => fromKeyChangeFu(e, 'searchKey')}
  162. />
  163. </div>
  164. </div>
  165. <div>
  166. <Button onClick={resetSelectFu}>重置</Button>
  167. &emsp;
  168. <Button type='primary' onClick={() => setLookInfo({ id: -1, txt: '新增' })}>
  169. 新增
  170. </Button>
  171. </div>
  172. </div>
  173. {/* 表格主体 */}
  174. <div className='A1tableBox'>
  175. <Table
  176. scroll={{ x: 'max-content', y: 645 }}
  177. columns={columns}
  178. dataSource={tableInfo.list}
  179. rowKey='id'
  180. pagination={{
  181. showQuickJumper: true,
  182. position: ['bottomCenter'],
  183. showSizeChanger: true,
  184. current: fromData.pageNum,
  185. pageSize: fromData.pageSize,
  186. total: tableInfo.total,
  187. onChange: (pageNum, pageSize) => setFromData({ ...fromData, pageNum, pageSize })
  188. }}
  189. />
  190. </div>
  191. {['新增', '编辑'].includes(lookInfo.txt) ? (
  192. <A1add
  193. sId={lookInfo.id}
  194. closeFu={() => setLookInfo({ id: 0, txt: '' })}
  195. addTableFu={resetSelectFu}
  196. upTableFu={getListFu}
  197. />
  198. ) : null}
  199. {lookInfo.txt === '查看' ? <A1look sId={lookInfo.id} /> : null}
  200. </div>
  201. )
  202. }
  203. const MemoA1record = React.memo(A1record)
  204. export default MemoA1record