import React, { useCallback, useEffect, useMemo } from 'react' import styles from './index.module.scss' import { Table, Button } from 'antd' import ImageLazy from '../ImageLazy' import { A1_moveUp, A1_moveDown } from '../../store/action/A1banner' import { useDispatch } from 'react-redux' type Props = { myKey?: string //表格的key yHeight?: number //设置表格的高度 list: any //表格数据 columnsTemp: any[][] //表格展示 total?: number //总数 pageNum?: number pageSize?: number pagingInfo?: any | boolean onChange?: (pageNum: number, pageSize: number) => void lastBtn?: any classKey?: string //一个组件多次使用的时候要传递,分别设置style // 表格简单的合并 merge?: { type: string; num: number; loc: 'rowSpan' | 'colSpan' } // 定制化表头 myTitle?: { name: string; Com: React.ReactNode } // 为空的定制字段 isNull?: string } // 表格内容定制化 const tableComObj = (key: string, val: string[]) => { const obj = { A: ( {val[0]} ) } return Reflect.get(obj, key) } function MyTable({ yHeight, list, columnsTemp, total, pageNum = 1, pageSize = 10, pagingInfo = { showQuickJumper: true, position: ['bottomCenter'], showSizeChanger: true }, onChange, lastBtn = [], classKey = '', merge, myTitle, isNull = '(空)', myKey }: Props) { const dispatch = useDispatch() useEffect(() => { const dom = document.querySelector(`.MyTable${classKey} .ant-table-body`) as HTMLDivElement if (dom && yHeight) dom.style.height = yHeight + 'px' }, [classKey, yHeight]) // 页码变化 const paginationChange = useCallback( () => (pageNum: number, pageSize: number) => { if (onChange) { onChange(pageNum, pageSize) } }, [onChange] ) const dataChangeFu = useCallback( (v: any) => { /** * index:序号 * txtNormal:正常数据 * txt:判断显示不同字段 * img:图片 * txtChange:判断显示不同字段 * text:文字比较多的情况 * sort:排序 */ const obj = { index: (_: any, __: any, index: number) => index + 1 + (pageNum - 1) * pageSize, txtNormal: (item: any) => item[v[2]] || isNull, txt: (item: any) => { return item.status === 0 ? item[v[3]] || isNull : item[v[2]] || isNull }, img: (item: any) => { console.log(item.status, item[v[2]], v[2], v, 'txt') return (
) }, txtChange: (item: any) => Reflect.get(v[3], item[v[2]]) || v[4] || isNull, text: (item: any) => { let tempCom: any = item[v[2]] || isNull if (tempCom.length >= v[3]) { tempCom = tempCom.substring(0, v[3]) + '...' } if (v[4]) { tempCom = tableComObj(v[4], [tempCom, item[v[2]]]) } else if (item[v[2]].length >= v[3]) { tempCom = ( {tempCom} ) } return tempCom }, sort: (item: any, record: any, index: number) => { const isFirst = index === 0 const isLast = index === list.length - 1 const handlesort = (type: 'up' | 'down') => { if (type === 'up') { dispatch(A1_moveUp(index)) } else { dispatch(A1_moveDown(index)) } } return (
) } } return Reflect.get(obj, v[0]) }, [dispatch, isNull, list.length, pageNum, pageSize] ) const columns = useMemo(() => { const arr: any = columnsTemp.map((v: any) => ({ title: myTitle && v.includes(myTitle.name) ? myTitle.Com : v[1], render: dataChangeFu(v), onCell: merge && v.includes(merge.type) ? // {rowSpan:3} (item: any, index: number) => ({ rowSpan: index === 0 ? merge.num : 0 }) : '' })) return arr }, [columnsTemp, dataChangeFu, merge, myTitle]) return ( ) } const MemoMyTable = React.memo(MyTable) export default MemoMyTable