index.js 3.09 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2
import React, { PureComponent } from 'react';
import { Table, Alert } from 'antd';
3 4
import styles from './index.less';

ddcat1115's avatar
ddcat1115 committed
5 6 7 8 9 10 11 12 13 14
function initTotalList(columns) {
  const totalList = [];
  columns.forEach((column) => {
    if (column.needTotal) {
      totalList.push({ ...column, total: 0 });
    }
  });
  return totalList;
}

15
class StandardTable extends PureComponent {
ddcat1115's avatar
ddcat1115 committed
16 17 18 19 20 21 22 23 24 25
  constructor(props) {
    super(props);
    const { columns } = props;
    const needTotalList = initTotalList(columns);

    this.state = {
      selectedRowKeys: [],
      needTotalList,
    };
  }
26 27 28 29

  componentWillReceiveProps(nextProps) {
    // clean state
    if (nextProps.selectedRows.length === 0) {
ddcat1115's avatar
ddcat1115 committed
30
      const needTotalList = initTotalList(nextProps.columns);
31 32
      this.setState({
        selectedRowKeys: [],
ddcat1115's avatar
ddcat1115 committed
33
        needTotalList,
34 35 36 37 38
      });
    }
  }

  handleRowSelectChange = (selectedRowKeys, selectedRows) => {
ddcat1115's avatar
ddcat1115 committed
39 40 41 42 43 44 45 46 47
    let needTotalList = [...this.state.needTotalList];
    needTotalList = needTotalList.map((item) => {
      return {
        ...item,
        total: selectedRows.reduce((sum, val) => {
          return sum + parseFloat(val[item.dataIndex], 10);
        }, 0),
      };
    });
48 49 50 51 52

    if (this.props.onSelectRow) {
      this.props.onSelectRow(selectedRows);
    }

ddcat1115's avatar
ddcat1115 committed
53
    this.setState({ selectedRowKeys, needTotalList });
54 55 56 57 58 59 60 61 62 63 64
  }

  handleTableChange = (pagination, filters, sorter) => {
    this.props.onChange(pagination, filters, sorter);
  }

  cleanSelectedKeys = () => {
    this.handleRowSelectChange([], []);
  }

  render() {
ddcat1115's avatar
ddcat1115 committed
65 66
    const { selectedRowKeys, needTotalList } = this.state;
    const { data: { list, pagination }, loading, columns } = this.props;
67 68 69 70 71 72 73 74 75 76

    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      ...pagination,
    };

    const rowSelection = {
      selectedRowKeys,
      onChange: this.handleRowSelectChange,
nikogu's avatar
nikogu committed
77 78 79
      getCheckboxProps: record => ({
        disabled: record.disabled,
      }),
80 81 82 83 84 85 86
    };

    return (
      <div className={styles.standardTable}>
        <div className={styles.tableAlert}>
          <Alert
            message={(
afc163's avatar
afc163 committed
87
              <div>
ddcat1115's avatar
ddcat1115 committed
88
                已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> 项&nbsp;&nbsp;
ddcat1115's avatar
ddcat1115 committed
89 90 91 92 93 94 95 96 97 98
                {
                  needTotalList.map(item => (
                    <span style={{ marginLeft: 8 }} key={item.dataIndex}>{item.title}总计&nbsp;
                      <span style={{ fontWeight: 600 }}>
                        {item.render ? item.render(item.total) : item.total}
                      </span>
                    </span>
                    )
                  )
                }
niko's avatar
niko committed
99
                <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>清空</a>
afc163's avatar
afc163 committed
100
              </div>
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            )}
            type="info"
            showIcon
          />
        </div>
        <Table
          loading={loading}
          rowKey={record => record.key}
          rowSelection={rowSelection}
          dataSource={list}
          columns={columns}
          pagination={paginationProps}
          onChange={this.handleTableChange}
        />
      </div>
    );
  }
}

export default StandardTable;