index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import CustomListModel from '@/components/CustomList'
  2. import Tables from '@/components/Tables'
  3. import { quanpin } from '@/utils/fullScreen'
  4. import { FullscreenExitOutlined, FullscreenOutlined, RedoOutlined, SettingOutlined } from '@ant-design/icons'
  5. import { Button, Card, Col, Row, Space, Spin, Tooltip, } from 'antd'
  6. import { ColumnsType } from 'antd/lib/table'
  7. import React, { useEffect, useRef, useState, useCallback } from 'react'
  8. import { useModel } from 'umi'
  9. import style from './index.less'
  10. interface Prosp {
  11. // sortArr?: { label: string, value: string ,key:any}[],//排序参数名列表
  12. isZj?: boolean,//是否查总计
  13. tableTotal?: { [key: string]: string },//是个开启总计
  14. scroll?: { x?: number, y?: number },//开启行滑动并设置容器最大宽度
  15. columns: () => ColumnsType<any>,//table列表配置
  16. title?: string,//tabel的标题
  17. tooltip?: JSX.Element,//是否在标题后加问号展示说明
  18. dataSource: any[],//table的数据
  19. expandedRowRender?: (data: any) => JSX.Element,
  20. className?: string,//自定义class
  21. isdownload?: boolean,
  22. leftChild?: JSX.Element,
  23. config?: any,
  24. configName?: any,
  25. page?: number,
  26. pageSize?: number,
  27. size?: 'small' | 'middle' | 'large',
  28. total?: number,
  29. loading?: boolean,
  30. onChange?: (props: {
  31. pagination?: { current?: number, pageSize?: number, gzh?: string },
  32. filters?: any,
  33. sortData?: {
  34. column: { dataIndex: string },
  35. order?: "ascend" | "descend"
  36. }
  37. }) => void,
  38. ajax?: any,//接口刷新
  39. }
  40. function TableData(props: Prosp) {
  41. const { isZj, scroll, columns, title, dataSource, expandedRowRender, className, leftChild, page = undefined, pageSize = undefined, size = 'small', total = 0, loading = false, onChange, config, configName, ajax } = props
  42. const { state: userState } = useModel('useOperating.useUser')
  43. const { isFell } = userState
  44. const [visible, setVisible] = useState<boolean>(false)
  45. const [oldSelectData, setoldSelectData] = useState<any[]>([])
  46. const [oldFixed, setoldFixed] = useState<any>({ left: '0', right: '0' })
  47. const [newColumns, setNewColumns] = useState<any[]>([])
  48. // const [oldConfigName, setOldConfigName] = useState<any>('')//上次的配置名称
  49. const [selectData, setSelectData] = useState<{ selectData: any[], fixed: { left: string, right: string } }>({ selectData: [], fixed: { left: '0', right: '0' } })
  50. const ref = useRef(null)
  51. const oldName = useRef(null)
  52. const version = '1.0.0'
  53. // // /**重组选中的字段 */
  54. useEffect(() => {
  55. let oldConfigName = oldName.current || ''
  56. if (configName) {
  57. // if (dataSource?.length > 0) {
  58. const defSelectData = localStorage.getItem(`myAdMonitorConfig${version}_` + configName)
  59. const defFixed = localStorage.getItem(`myAdMonitorConfigFixed${version}_` + configName)
  60. const newConfig = config?.map((item: { data: any }) => item.data)?.flat()
  61. if (defSelectData && (selectData?.selectData?.length === 0 || configName !== oldConfigName)) {//首次查找个人配置是否存在,并且selectData为空,存在用个人配置设置selectData
  62. console.log('首次使用个人配置赋值')
  63. let newDefSelectData = JSON.parse(defSelectData)
  64. newDefSelectData = newDefSelectData.filter((item: any) => !!item && newConfig.some((c: { dataIndex: any }) => c.dataIndex === item.dataIndex))//去除空项
  65. setSelectData(() => ({ selectData: newDefSelectData, fixed: defFixed ? JSON.parse(defFixed) : { left: '0', right: '0' } }))
  66. }
  67. if (!defSelectData && (selectData?.selectData?.length === 0 || configName !== oldConfigName)) {//首次查找个人配置是否存在,并且selectData为空,不存在默认配置设置selectData
  68. let newSelectData: any[] = []
  69. config?.forEach((item: { data: { default: any }[] }) => {
  70. item?.data?.forEach((d: { default: any }) => {
  71. if (d.default) {
  72. newSelectData[d.default - 1] = d
  73. }
  74. })
  75. })
  76. console.log('首次使用默认配置赋值')
  77. setSelectData(() => ({ selectData: newSelectData, fixed: { left: '0', right: configName.includes('起量') ? '2' : '0' } }))
  78. }
  79. if ((JSON.stringify(oldSelectData) !== JSON.stringify(selectData?.selectData)) || (JSON.stringify(selectData.fixed) !== JSON.stringify(oldFixed))) {
  80. console.log('设置配置改变重新赋值')
  81. setoldSelectData(() => selectData.selectData)
  82. setoldFixed(() => selectData.fixed)
  83. let newArr: any = []
  84. selectData?.selectData?.forEach((data: { dataIndex: any, width: number }, index: number) => {
  85. columns()?.forEach((item: any) => {
  86. if (data.dataIndex === item.dataIndex) {
  87. if (index < Number(selectData?.fixed?.left)) {//设置左悬浮
  88. item['fixed'] = 'left'
  89. } else if (index > (selectData?.selectData?.length - Number(selectData?.fixed?.right) - 1)) {//设置右悬浮
  90. item['fixed'] = 'right'
  91. } else {
  92. item['fixed'] = false
  93. }
  94. if (data?.width) {
  95. item['width'] = data.width
  96. }
  97. newArr.push(item)
  98. }
  99. })
  100. })
  101. setNewColumns(() => newArr)
  102. }
  103. }
  104. if (configName !== oldConfigName) {
  105. oldName.current = configName
  106. }
  107. }, [selectData, oldSelectData, dataSource, oldFixed, configName, config, columns]) // selectData, oldSelectData, dataSource, oldFixed, configName, oldConfigName, config, columns
  108. //拖动宽度设置设置保存
  109. const handelResize = useCallback((columns: any) => {
  110. if (configName) {
  111. let newSelectData = selectData?.selectData?.map((item, index) => {
  112. item['width'] = columns[index]['width']
  113. return item
  114. })
  115. localStorage.setItem(`myAdMonitorConfig${version}_` + configName, JSON.stringify(newSelectData))
  116. }
  117. }, [configName, selectData])
  118. const header = <Col span={24}>
  119. <Row gutter={[0, 10]}>
  120. <Col span={24} style={{
  121. display: 'flex',
  122. justifyContent: 'space-between',
  123. alignItems: 'end'
  124. }}>
  125. <Space>
  126. {leftChild}
  127. </Space>
  128. {/*紧凑*/}
  129. <Space style={{ float: 'right' }}>
  130. {
  131. ajax && <Button
  132. size='small'
  133. type='text'
  134. onClick={() => {
  135. ajax.refresh()
  136. }}>
  137. <span style={{ fontSize: 10, color: '#999' }}>刷新时间:{ajax?.data?.reqTime}</span>
  138. <Tooltip title='刷新'><RedoOutlined /></Tooltip>
  139. </Button>
  140. }
  141. {config && <Button
  142. size='small'
  143. type='text'
  144. onClick={() => {
  145. setVisible(true)
  146. }}>
  147. <Tooltip title='设置'><SettingOutlined /></Tooltip>
  148. </Button>}
  149. <Button
  150. type='text'
  151. size='small'
  152. onClick={() => {
  153. if (ref?.current) {
  154. quanpin(ref?.current)
  155. }
  156. }}>
  157. {
  158. <Tooltip title={!isFell ? '全屏' : '退出全屏'}>{!isFell ? <FullscreenOutlined /> : <FullscreenExitOutlined />}</Tooltip>
  159. }
  160. </Button>
  161. {visible && <CustomListModel version={version} config={config} configName={configName} visible={visible} onClose={() => { setVisible(false) }} onChange={(arr: any) => { setSelectData(arr) }} columns={newColumns?.length > 0 ? newColumns : columns()} />}
  162. </Space>
  163. </Col>
  164. </Row>
  165. </Col>
  166. const content = <Row gutter={[0, 20]} ref={ref} style={isFell ? { background: '#fff' } : {}}>
  167. {/**table */}
  168. <Col span={24}>
  169. <Card
  170. hoverable
  171. title={title}
  172. headStyle={{ textAlign: 'left' }}
  173. >
  174. <Row gutter={[0, 20]}>
  175. {header}
  176. <Tab {...{ size, newColumns, handelResize, className, isZj, columns, loading, scroll, isFell, page, pageSize, dataSource, onChange, expandedRowRender, total, ajax }} />
  177. </Row>
  178. </Card>
  179. </Col>
  180. </Row >
  181. return <>
  182. {content}
  183. </>
  184. }
  185. /**表格 */
  186. const Tab = React.memo((props: any) => {
  187. const { size, newColumns, className, handelResize, columns, scroll, loading, isFell, page, pageSize, dataSource, onChange, expandedRowRender, total, ajax } = props
  188. return < Col span={24} >
  189. <div className={`${style[size]} ${className ? style[className] : ''} `}>
  190. {dataSource || !ajax?.loading ? <Tables
  191. className={`all_table ${className ? className : ''}`}
  192. bordered
  193. sortDirections={['ascend', 'descend', null]}
  194. current={page}
  195. pageSize={pageSize}
  196. columns={newColumns?.length > 0 ? newColumns : columns()}
  197. dataSource={dataSource}
  198. scroll={scroll ? isFell ? { ...scroll, y: document.body.clientHeight - 300 } : scroll : {}}
  199. onChange={(pagination: any, filters: any, sorter: any) => onChange && onChange({ pagination, filters, sortData: sorter })}
  200. rowClassName={(record: { color: string }) => style[record['color']]}
  201. expandedRowRender={expandedRowRender ? expandedRowRender : undefined}
  202. size={size}
  203. total={total}
  204. loading={loading}
  205. defaultPageSize={20}
  206. handelResize={((columns: any) => handelResize(columns))}
  207. /> : <div className={style.example}>
  208. <Spin />
  209. </div>}
  210. </div>
  211. </Col >
  212. })
  213. export default React.memo(TableData)