index.tsx 5.3 KB

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