index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import React, { ReactNode, useEffect, useRef, useState } from 'react'
  2. import { Table, Tag, message } from 'antd';
  3. import { Excle } from '@/utils/Excle'
  4. import style from './index.less'
  5. import './index.less'
  6. import { Resizable } from 'react-resizable'
  7. import { SortOrder } from 'antd/lib/table/interface';
  8. export interface DataType {
  9. key: React.Key;
  10. name: string;
  11. age: string;
  12. address: string;
  13. }
  14. const ResizableHeader = (props: { [x: string]: any; onResize: any; width: any }) => {
  15. const { onResize, width, ...restProps } = props;
  16. if (!width) {
  17. return <th {...restProps} />
  18. }
  19. return <Resizable width={width} height={0} onResize={onResize} draggableOpts={{ enableUserSelectHack: false }}>
  20. <th {...restProps} />
  21. </Resizable>
  22. }
  23. /**
  24. * https://ant-design.gitee.io/components/table-cn/#API
  25. */
  26. interface Props {
  27. dataSource: any[],
  28. columns: any[],
  29. excle?: {
  30. fillName: string,//文件名称
  31. filter?: string[]//需要排除的数据,输入数据key
  32. },
  33. total?: number,
  34. current?: number,
  35. pageSize?: number,
  36. rowSelection?: any,//选择
  37. onRow?: {//行操作事件
  38. onClick?: (event?: any) => void, // 点击行
  39. onDoubleClick?: (event?: any) => void,
  40. onContextMenu?: (event?: any) => void,
  41. onMouseEnter?: (event?: any) => void, // 鼠标移入行
  42. onMouseLeave?: (event?: any) => void,
  43. }
  44. pageChange?: (page: number, pageSize?: number) => void,
  45. sizeChange?: (current: number, size?: number) => void,
  46. rowClassName?: string | ((record: any, index: any) => string),//样式
  47. expandedRowRender?: any,//子table
  48. scroll?: { x?: number, y?: number },
  49. summary?: (data: readonly any[]) => ReactNode,
  50. size?: 'small' | 'middle' | 'large',
  51. loading?: boolean,
  52. defaultPageSize?: 10 | 20 | 30 | 100,
  53. bordered?: boolean,
  54. onChange?: (pagination: any, filters: any, sorter: any) => void//服务端排序回调
  55. pagination?: boolean,
  56. showHeader?: 1,
  57. hideOnSinglePage?: boolean,
  58. className?: string,
  59. handelResize?: (columns: any) => void//当表头被拖动改变宽度时回调设置对应的本地保存
  60. sortDirections?: SortOrder[],
  61. isShowTotal?: boolean, // 是否展示总计
  62. myKey?: any,//自定义要用哪个值做key
  63. }
  64. function Tables(props: Props) {
  65. // //导出数据
  66. let { columns, dataSource, excle, total, handelResize, rowSelection, onRow, isShowTotal = true, hideOnSinglePage, rowClassName, className, expandedRowRender, scroll, summary, showHeader, bordered, size = 'small', onChange, sortDirections, pagination, myKey, ...prop } = props
  67. let handleExcle = () => {
  68. if (dataSource.length < 1) {
  69. message.error('请先搜索再导出');
  70. return
  71. }
  72. let thName: any = {};
  73. let obj = columns;
  74. obj.forEach((item: any) => {
  75. thName[item.key] = item.title
  76. })
  77. let fillName = excle?.fillName || ''
  78. let filter = excle?.filter || []
  79. Excle(thName, dataSource, fillName, filter)
  80. }
  81. let ww = document.body.clientWidth < 415
  82. // 拖动宽逻辑
  83. const [cols, setCols] = useState<any>(columns)
  84. const colsRef = useRef<any[]>([])
  85. const components = {
  86. header: {
  87. cell: ResizableHeader
  88. },
  89. }
  90. useEffect(() => {
  91. setCols(columns)
  92. }, [columns])
  93. const handleResize = (index: any) => (e: any, { size }: any) => {
  94. const nextColumns = [...cols]
  95. nextColumns[index] = {
  96. ...nextColumns[index],
  97. width: size.width
  98. }
  99. setCols(nextColumns)
  100. handelResize && handelResize(nextColumns)
  101. }
  102. colsRef.current = (cols || []).map((col: any, index: any) => ({
  103. ...col,
  104. onHeaderCell: (column: any) => ({
  105. width: column.width,
  106. onResize: handleResize(index)
  107. })
  108. }))
  109. return <div className='components-table-resizable-column'>
  110. {
  111. excle && <Tag color='#f50' style={{ margin: '10px 0', float: 'right' }} onClick={handleExcle}><a>导出数据</a></Tag>
  112. }
  113. <Table
  114. components={components}
  115. columns={colsRef?.current}
  116. sortDirections={sortDirections || ['descend', 'ascend', null]}
  117. onChange={(pagination, filters, sorter) => {
  118. onChange && onChange(pagination, filters, sorter)
  119. }}
  120. bordered={bordered ? bordered : false}
  121. dataSource={dataSource}
  122. showHeader={showHeader === 1 ? false : true}
  123. scroll={scroll ? { ...scroll, scrollToFirstRowOnChange: true } : undefined}
  124. size={size}
  125. rowKey={(a: any) => {
  126. if (myKey) {
  127. return JSON.stringify(a[myKey])
  128. }
  129. return (JSON.stringify(a?.id) || JSON.stringify(a?.key))
  130. }}
  131. rowSelection={rowSelection ? rowSelection : undefined}
  132. onRow={record => {
  133. return {
  134. onClick: onRow?.onClick && onRow?.onClick.bind('', record) || undefined,
  135. }
  136. }}
  137. summary={summary}
  138. className={className}
  139. expandable={expandedRowRender ? {
  140. defaultExpandedRowKeys: ['0'],
  141. expandRowByClick: true, expandedRowRender: (data) => {
  142. return expandedRowRender(data)
  143. }
  144. } : {}}
  145. pagination={
  146. {
  147. total: total || 0,//总共多少条数据,服务器给,设置后分页自动计算页数
  148. current: props.current,//当前页数,需state控制配合翻页
  149. pageSize: props?.pageSize,
  150. defaultCurrent: 1,//默认初始的当前页数
  151. defaultPageSize: props?.defaultPageSize || 20,//默认初始的每页条数
  152. pageSizeOptions: ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100'],
  153. showTotal: (total) => isShowTotal ? <Tag color="cyan">总共{total}数据</Tag> : null,
  154. showSizeChanger: true, //手动开启条数筛选器,默认超过50条开启
  155. // size:'small',//设置分页尺寸
  156. //responsive:true,//当 size 未指定时,根据屏幕宽度自动调整尺寸
  157. onChange: props.pageChange, //点击页码或条数筛选时触发获取当前页数和每页条数
  158. onShowSizeChange: props.sizeChange,//点击条数筛选器触发
  159. simple: ww ? true : false,//开启简单分页
  160. hideOnSinglePage: hideOnSinglePage ? hideOnSinglePage : false,//只有一页数据隐藏分页
  161. showLessItems: true
  162. }
  163. }
  164. {...prop}
  165. {...{
  166. rowClassName: typeof rowClassName === 'string' ? (record: any) => {
  167. if (rowClassName && record[rowClassName as string] === 2) {
  168. return style.unfollow
  169. }
  170. if (rowClassName && !record[rowClassName as string]) {
  171. return style.unfollow
  172. }
  173. } : rowClassName
  174. }}
  175. />
  176. </div>
  177. }
  178. export default Tables