dataPreview.jsx 4.6 KB

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