index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. columnWidth?: string | number
  55. onChange?: (pagination: any, filters: any, sorter: any) => void//服务端排序回调
  56. pagination?: boolean,
  57. showHeader?: boolean,
  58. hideOnSinglePage?: boolean,
  59. className?: string,
  60. handelResize?: (columns: any) => void//当表头被拖动改变宽度时回调设置对应的本地保存
  61. sortDirections?: SortOrder[],
  62. isShowTotal?: boolean, // 是否展示总计
  63. myKey?: any,//自定义要用哪个值做key
  64. }
  65. function Tables(props: Props) {
  66. // //导出数据
  67. let { columns, dataSource, excle, total, handelResize, columnWidth, rowSelection, onRow, isShowTotal = true, hideOnSinglePage, rowClassName, className, expandedRowRender, scroll, summary, showHeader, bordered, size = 'small', onChange, sortDirections, pagination, myKey, ...prop } = props
  68. let handleExcle = () => {
  69. if (dataSource.length < 1) {
  70. message.error('请先搜索再导出');
  71. return
  72. }
  73. let thName: any = {};
  74. let obj = columns;
  75. obj.forEach((item: any) => {
  76. thName[item.key] = item.title
  77. })
  78. let fillName = excle?.fillName || ''
  79. let filter = excle?.filter || []
  80. Excle(thName, dataSource, fillName, filter)
  81. }
  82. let ww = document.body.clientWidth < 415
  83. // 拖动宽逻辑
  84. const [cols, setCols] = useState<any>(columns)
  85. const colsRef = useRef<any[]>([])
  86. const components = {
  87. header: {
  88. cell: ResizableHeader
  89. },
  90. }
  91. useEffect(() => {
  92. setCols(columns)
  93. }, [columns])
  94. const handleResize = (index: any) => (e: any, { size }: any) => {
  95. const nextColumns = [...cols]
  96. nextColumns[index] = {
  97. ...nextColumns[index],
  98. width: size.width
  99. }
  100. setCols(nextColumns)
  101. handelResize && handelResize(nextColumns)
  102. }
  103. colsRef.current = (cols || []).map((col: any, index: any) => ({
  104. ...col,
  105. onHeaderCell: (column: any) => ({
  106. width: column.width,
  107. onResize: handleResize(index)
  108. })
  109. }))
  110. return <div className='components-table-resizable-column'>
  111. {
  112. excle && <Tag color='#f50' style={{ margin: '10px 0', float: 'right' }} onClick={handleExcle}><a>导出数据</a></Tag>
  113. }
  114. <Table
  115. components={components}
  116. columns={colsRef?.current}
  117. sortDirections={sortDirections || ['descend', 'ascend', null]}
  118. onChange={(pagination, filters, sorter) => {
  119. onChange && onChange(pagination, filters, sorter)
  120. }}
  121. bordered={bordered ? bordered : false}
  122. dataSource={dataSource}
  123. showHeader={showHeader}
  124. scroll={scroll ? { ...scroll, scrollToFirstRowOnChange: true } : undefined}
  125. size={size}
  126. rowKey={(a: any) => {
  127. if (myKey) {
  128. return JSON.stringify(a[myKey])
  129. }
  130. return (JSON.stringify(a?.id) || JSON.stringify(a?.key))
  131. }}
  132. rowSelection={rowSelection ? rowSelection : undefined}
  133. onRow={record => {
  134. return {
  135. onClick: onRow?.onClick && onRow?.onClick.bind('', record) || undefined,
  136. }
  137. }}
  138. summary={summary}
  139. className={className}
  140. expandable={expandedRowRender ? {
  141. defaultExpandedRowKeys: ['0'],
  142. expandRowByClick: false,
  143. columnWidth,
  144. expandedRowRender: (data) => {
  145. return expandedRowRender(data)
  146. }
  147. } : {}}
  148. pagination={
  149. {
  150. total: total || 0,//总共多少条数据,服务器给,设置后分页自动计算页数
  151. current: props.current,//当前页数,需state控制配合翻页
  152. pageSize: props?.pageSize,
  153. defaultCurrent: 1,//默认初始的当前页数
  154. defaultPageSize: props?.defaultPageSize || 20,//默认初始的每页条数
  155. pageSizeOptions: ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100'],
  156. showTotal: (total) => isShowTotal ? <Tag color="cyan">总共{total}数据</Tag> : null,
  157. showSizeChanger: true, //手动开启条数筛选器,默认超过50条开启
  158. // size:'small',//设置分页尺寸
  159. //responsive:true,//当 size 未指定时,根据屏幕宽度自动调整尺寸
  160. onChange: props.pageChange, //点击页码或条数筛选时触发获取当前页数和每页条数
  161. onShowSizeChange: props.sizeChange,//点击条数筛选器触发
  162. simple: ww ? true : false,//开启简单分页
  163. hideOnSinglePage: hideOnSinglePage ? hideOnSinglePage : false,//只有一页数据隐藏分页
  164. showLessItems: true
  165. }
  166. }
  167. {...prop}
  168. {...{
  169. rowClassName: typeof rowClassName === 'string' ? (record: any) => {
  170. if (rowClassName && record[rowClassName as string] === 2) {
  171. return style.unfollow
  172. }
  173. if (rowClassName && !record[rowClassName as string]) {
  174. return style.unfollow
  175. }
  176. } : rowClassName
  177. }}
  178. />
  179. </div>
  180. }
  181. export default Tables