dataPreview.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React from 'react'
  2. import { Modal, Table } from 'antd'
  3. import { connect } from 'dva'
  4. import EllipsisTooltip from '../ellipsisTooltip/index'
  5. import './dataPreview.less'
  6. class DataPreview extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. boxW: 1, // 0 ~ 1
  11. boxH: 1, // 0 ~ 1
  12. columnWidth: 200,
  13. tableHeaderHeight: 60,
  14. screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
  15. screenHeight: document.documentElement.clientHeight || document.body.clientHeight
  16. };
  17. }
  18. componentDidMount() {
  19. const { fetchFunction } = this.props;
  20. window.addEventListener('resize', this.onWindowResize);
  21. typeof fetchFunction === 'function' && fetchFunction();
  22. }
  23. componentWillUnmount() {
  24. const { dispatch } = this.props;
  25. window.removeEventListener('resize', this.onWindowResize);
  26. dispatch({ type: 'dataList/reset' });
  27. }
  28. onWindowResize = () => {
  29. const boxEl = document.getElementsByClassName('datapreview')[0];
  30. const tableHeaderEl = boxEl.getElementsByTagName('thead')[0];
  31. this.setState({
  32. screenWidth: document.documentElement.clientWidth || document.body.clientWidth, // 屏幕宽
  33. screenHeight: document.documentElement.clientHeight || document.body.clientHeight, // 屏幕高
  34. tableHeaderHeight: tableHeaderEl.offsetHeight + 2, // 表头高度(含边框)
  35. });
  36. }
  37. render() {
  38. const { fetchFunction, dataList, visibleBox, hideBox } = this.props;
  39. const { loading, columns, dataSource, pageSize, total } = dataList;
  40. const { screenWidth, screenHeight, boxW, boxH, columnWidth, tableHeaderHeight } = this.state;
  41. const tableBodyWidth = screenWidth * boxW - 10 - 10 - 18;
  42. const tableBodyHeight = screenHeight * boxH - 40 - 40 - tableHeaderHeight - 2;
  43. return <Modal
  44. className='datapreview'
  45. style={{ top: `${(1 - boxH) * 0.5 * 100}%` }}
  46. width={`${100 * boxW}%`}
  47. height={`${100 * boxH}%`}
  48. visible={visibleBox}
  49. footer={null}
  50. onCancel={hideBox}
  51. maskClosable={false}
  52. >
  53. <Table
  54. bordered
  55. columns={columns.map((c, i) => {
  56. let obj = { ...c,
  57. onCell: () => {
  58. return {
  59. style: {
  60. whiteSpace: 'nowrap',
  61. maxWidth: 150,
  62. }
  63. }
  64. },
  65. render: (text) => <EllipsisTooltip title={text}>{text}</EllipsisTooltip>,
  66. };
  67. if(i !== columns.length - 1) {
  68. obj.width = columnWidth
  69. }
  70. return obj;
  71. })}
  72. dataSource={dataSource.map((d, i) => ({
  73. ...d,
  74. key: i
  75. }))}
  76. size='small'
  77. loading={loading}
  78. pagination={{
  79. pageSize,
  80. total,
  81. // simple: true,
  82. showTotal: (total, range) => {
  83. return `第${range[0]}到第${range[1]}条数据,共${total}条数据`;
  84. },
  85. onChange: (page, pageSize) => {
  86. typeof fetchFunction === 'function' && fetchFunction(page, pageSize);
  87. }
  88. }}
  89. scroll={{ x: columns ? columns.length * columnWidth : tableBodyWidth, y: tableBodyHeight }}
  90. />
  91. </Modal>
  92. }
  93. }
  94. export default connect(({ present: { dataList } }) => ({ dataList }))(DataPreview);