index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import React, { useCallback, useEffect, useMemo } from 'react'
  2. import styles from './index.module.scss'
  3. import { Table, Button } from 'antd'
  4. import ImageLazy from '../ImageLazy'
  5. import { A1_moveUp, A1_moveDown } from '../../store/action/A1banner'
  6. import { useDispatch } from 'react-redux'
  7. type Props = {
  8. myKey?: string //表格的key
  9. yHeight?: number //设置表格的高度
  10. list: any //表格数据
  11. columnsTemp: any[][] //表格展示
  12. total?: number //总数
  13. pageNum?: number
  14. pageSize?: number
  15. pagingInfo?: any | boolean
  16. onChange?: (pageNum: number, pageSize: number) => void
  17. lastBtn?: any
  18. classKey?: string //一个组件多次使用的时候要传递,分别设置style
  19. // 表格简单的合并
  20. merge?: { type: string; num: number; loc: 'rowSpan' | 'colSpan' }
  21. // 定制化表头
  22. myTitle?: { name: string; Com: React.ReactNode }
  23. // 为空的定制字段
  24. isNull?: string
  25. }
  26. // 表格内容定制化
  27. const tableComObj = (key: string, val: string[]) => {
  28. const obj = {
  29. A: (
  30. <a href={val[1]} target='_blank' title={val[1]} rel='noreferrer'>
  31. {val[0]}
  32. </a>
  33. )
  34. }
  35. return Reflect.get(obj, key)
  36. }
  37. function MyTable({
  38. yHeight,
  39. list,
  40. columnsTemp,
  41. total,
  42. pageNum = 1,
  43. pageSize = 10,
  44. pagingInfo = {
  45. showQuickJumper: true,
  46. position: ['bottomCenter'],
  47. showSizeChanger: true
  48. },
  49. onChange,
  50. lastBtn = [],
  51. classKey = '',
  52. merge,
  53. myTitle,
  54. isNull = '(空)',
  55. myKey
  56. }: Props) {
  57. const dispatch = useDispatch()
  58. useEffect(() => {
  59. const dom = document.querySelector(`.MyTable${classKey} .ant-table-body`) as HTMLDivElement
  60. if (dom && yHeight) dom.style.height = yHeight + 'px'
  61. }, [classKey, yHeight])
  62. // 页码变化
  63. const paginationChange = useCallback(
  64. () => (pageNum: number, pageSize: number) => {
  65. if (onChange) {
  66. onChange(pageNum, pageSize)
  67. }
  68. },
  69. [onChange]
  70. )
  71. const dataChangeFu = useCallback(
  72. (v: any) => {
  73. /**
  74. * index:序号
  75. * txtNormal:正常数据
  76. * txt:判断显示不同字段
  77. * img:图片
  78. * txtChange:判断显示不同字段
  79. * text:文字比较多的情况
  80. * sort:排序
  81. */
  82. const obj = {
  83. index: (_: any, __: any, index: number) => index + 1 + (pageNum - 1) * pageSize,
  84. txtNormal: (item: any) => item[v[2]] || isNull,
  85. txt: (item: any) => {
  86. return item.status === 0 ? item[v[3]] || isNull : item[v[2]] || isNull
  87. },
  88. img: (item: any) => {
  89. console.log(item.status, item[v[2]], v[2], v, 'txt')
  90. return (
  91. <div className='tableImgAuto'>
  92. <ImageLazy
  93. width={60}
  94. height={60}
  95. srcBig={item.imgB}
  96. src={item.status === 0 ? item[v[3]] || item.imgThB : item[v[2]] || item.imgThB}
  97. offline={(item.status === 0 ? item[v[3]] : item[v[2]] || '').includes('http')}
  98. />
  99. </div>
  100. )
  101. },
  102. txtChange: (item: any) => Reflect.get(v[3], item[v[2]]) || v[4] || isNull,
  103. text: (item: any) => {
  104. let tempCom: any = item[v[2]] || isNull
  105. if (tempCom.length >= v[3]) {
  106. tempCom = tempCom.substring(0, v[3]) + '...'
  107. }
  108. if (v[4]) {
  109. tempCom = tableComObj(v[4], [tempCom, item[v[2]]])
  110. } else if (item[v[2]].length >= v[3]) {
  111. tempCom = (
  112. <span style={{ cursor: 'pointer' }} title={item[v[2]]}>
  113. {tempCom}
  114. </span>
  115. )
  116. }
  117. return tempCom
  118. },
  119. sort: (item: any, record: any, index: number) => {
  120. const isFirst = index === 0
  121. const isLast = index === list.length - 1
  122. const handlesort = (type: 'up' | 'down') => {
  123. if (type === 'up') {
  124. dispatch(A1_moveUp(index))
  125. } else {
  126. dispatch(A1_moveDown(index))
  127. }
  128. }
  129. return (
  130. <div className='tableSort'>
  131. <Button
  132. style={{ display: isFirst ? 'none' : 'block', backgroundColor: 'transparent' }}
  133. onClick={() => handlesort('up')}
  134. >
  135. 上移
  136. </Button>
  137. <Button
  138. style={{ display: isLast ? 'none' : 'block', backgroundColor: 'transparent' }}
  139. onClick={() => handlesort('down')}
  140. >
  141. 下移
  142. </Button>
  143. </div>
  144. )
  145. }
  146. }
  147. return Reflect.get(obj, v[0])
  148. },
  149. [dispatch, isNull, list.length, pageNum, pageSize]
  150. )
  151. const columns = useMemo(() => {
  152. const arr: any = columnsTemp.map((v: any) => ({
  153. title: myTitle && v.includes(myTitle.name) ? myTitle.Com : v[1],
  154. render: dataChangeFu(v),
  155. onCell:
  156. merge && v.includes(merge.type)
  157. ? // {rowSpan:3}
  158. (item: any, index: number) => ({
  159. rowSpan: index === 0 ? merge.num : 0
  160. })
  161. : ''
  162. }))
  163. return arr
  164. }, [columnsTemp, dataChangeFu, merge, myTitle])
  165. return (
  166. <Table
  167. className={`${styles.MyTable} MyTable${classKey}`}
  168. scroll={{ y: yHeight ? yHeight : '' }}
  169. dataSource={list}
  170. columns={[...columns, ...lastBtn]}
  171. rowKey={myKey ? myKey : 'id'}
  172. pagination={
  173. pagingInfo
  174. ? {
  175. ...pagingInfo,
  176. current: pageNum,
  177. pageSize: pageSize,
  178. total: total,
  179. onChange: paginationChange()
  180. }
  181. : false
  182. }
  183. />
  184. )
  185. }
  186. const MemoMyTable = React.memo(MyTable)
  187. export default MemoMyTable