index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { PureComponent, Fragment } from 'react';
  2. import { Table, Alert } from 'antd';
  3. import styles from './index.less';
  4. function initTotalList(columns) {
  5. const totalList = [];
  6. columns.forEach(column => {
  7. if (column.needTotal) {
  8. totalList.push({ ...column, total: 0 });
  9. }
  10. });
  11. return totalList;
  12. }
  13. class StandardTable extends PureComponent {
  14. constructor(props) {
  15. super(props);
  16. const { columns } = props;
  17. const needTotalList = initTotalList(columns);
  18. this.state = {
  19. selectedRowKeys: [],
  20. needTotalList,
  21. };
  22. }
  23. componentWillReceiveProps(nextProps) {
  24. // clean state
  25. if (nextProps.selectedRows.length === 0) {
  26. const needTotalList = initTotalList(nextProps.columns);
  27. this.setState({
  28. selectedRowKeys: [],
  29. needTotalList,
  30. });
  31. }
  32. }
  33. handleRowSelectChange = (selectedRowKeys, selectedRows) => {
  34. const { needTotalList: list } = this.state;
  35. const { onSelectRow } = this.props;
  36. let needTotalList = [...list];
  37. needTotalList = needTotalList.map(item => {
  38. return {
  39. ...item,
  40. total: selectedRows.reduce((sum, val) => {
  41. return sum + parseFloat(val[item.dataIndex], 10);
  42. }, 0),
  43. };
  44. });
  45. if (onSelectRow) {
  46. onSelectRow(selectedRows);
  47. }
  48. this.setState({ selectedRowKeys, needTotalList });
  49. };
  50. handleTableChange = (pagination, filters, sorter) => {
  51. const { onChange } = this.props;
  52. onChange(pagination, filters, sorter);
  53. };
  54. cleanSelectedKeys = () => {
  55. this.handleRowSelectChange([], []);
  56. };
  57. render() {
  58. const { selectedRowKeys, needTotalList } = this.state;
  59. const {
  60. data: { list, pagination },
  61. loading,
  62. columns,
  63. rowKey,
  64. } = this.props;
  65. const paginationProps = {
  66. showSizeChanger: true,
  67. showQuickJumper: true,
  68. ...pagination,
  69. };
  70. const rowSelection = {
  71. selectedRowKeys,
  72. onChange: this.handleRowSelectChange,
  73. getCheckboxProps: record => ({
  74. disabled: record.disabled,
  75. }),
  76. };
  77. return (
  78. <div className={styles.standardTable}>
  79. <div className={styles.tableAlert}>
  80. <Alert
  81. message={
  82. <Fragment>
  83. 已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> 项&nbsp;&nbsp;
  84. {needTotalList.map(item => (
  85. <span style={{ marginLeft: 8 }} key={item.dataIndex}>
  86. {item.title}
  87. 总计&nbsp;
  88. <span style={{ fontWeight: 600 }}>
  89. {item.render ? item.render(item.total) : item.total}
  90. </span>
  91. </span>
  92. ))}
  93. <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>
  94. 清空
  95. </a>
  96. </Fragment>
  97. }
  98. type="info"
  99. showIcon
  100. />
  101. </div>
  102. <Table
  103. loading={loading}
  104. rowKey={rowKey || 'key'}
  105. rowSelection={rowSelection}
  106. dataSource={list}
  107. columns={columns}
  108. pagination={paginationProps}
  109. onChange={this.handleTableChange}
  110. />
  111. </div>
  112. );
  113. }
  114. }
  115. export default StandardTable;