dataPreview.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 { fetchFunction, dataList, visibleBox, hideBox } = 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. width={`${100 * boxW}%`}
  48. height={`${100 * boxH}%`}
  49. visible={visibleBox}
  50. footer={null}
  51. onCancel={hideBox}
  52. maskClosable={false}
  53. >
  54. <Table
  55. bordered
  56. columns={columns.map((c, i) => {
  57. let obj = {
  58. title: c.label,
  59. dataIndex: c.name,
  60. onCell: () => {
  61. return {
  62. style: {
  63. whiteSpace: 'nowrap',
  64. maxWidth: 150,
  65. }
  66. }
  67. },
  68. };
  69. if(c.type === 'time') {
  70. obj.render = v => {
  71. let text = moment(v).isValid() ? moment(v).format('YYYY-MM-DD') : v
  72. return <EllipsisTooltip key={i} title={text}>{text}</EllipsisTooltip>
  73. }
  74. }else {
  75. obj.render = v => {
  76. let text = v === null ? '空' : v
  77. return <EllipsisTooltip key={i} title={text}>{text}</EllipsisTooltip>
  78. }
  79. }
  80. if(i !== columns.length - 1) {
  81. obj.width = columnWidth
  82. }
  83. return obj;
  84. })}
  85. dataSource={dataSource.map((d, i) => ({
  86. ...d,
  87. key: i
  88. }))}
  89. size='small'
  90. loading={loading}
  91. pagination={{
  92. pageSize,
  93. total,
  94. // simple: true,
  95. showTotal: (total, range) => {
  96. return `第${range[0]}到第${range[1]}条数据,共${total}条数据`;
  97. },
  98. onChange: (page, pageSize) => {
  99. typeof fetchFunction === 'function' && fetchFunction(page, pageSize);
  100. }
  101. }}
  102. scroll={{ x: columns ? columns.length * columnWidth : tableBodyWidth, y: tableBodyHeight }}
  103. />
  104. </Modal>
  105. }
  106. }
  107. export default connect(({ present: { dataList } }) => ({ dataList }))(DataPreview);