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

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

15
class StandardTable extends PureComponent {
jim's avatar
jim 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,
    };
  }
陈帅's avatar
陈帅 committed
26

jim's avatar
jim committed
27 28 29 30 31 32 33 34 35 36 37
  static getDerivedStateFromProps(nextProps) {
    // clean state
    if (nextProps.selectedRows.length === 0) {
      const needTotalList = initTotalList(nextProps.columns);
      return {
        selectedRowKeys: [],
        needTotalList,
      };
    }
    return null;
  }
陈帅's avatar
陈帅 committed
38

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

ddcat1115's avatar
ddcat1115 committed
54
    this.setState({ selectedRowKeys, needTotalList });
jim's avatar
jim committed
55
  };
56 57

  handleTableChange = (pagination, filters, sorter) => {
陈帅's avatar
陈帅 committed
58 59 60 61
    const { onChange } = this.props;
    if (onChange) {
      onChange(pagination, filters, sorter);
    }
jim's avatar
jim committed
62
  };
63 64 65

  cleanSelectedKeys = () => {
    this.handleRowSelectChange([], []);
jim's avatar
jim committed
66
  };
67 68

  render() {
ddcat1115's avatar
ddcat1115 committed
69
    const { selectedRowKeys, needTotalList } = this.state;
jim's avatar
jim committed
70 71 72 73 74 75
    const {
      data: { list, pagination },
      loading,
      columns,
      rowKey,
    } = this.props;
76 77 78 79 80 81 82 83 84 85

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

    const rowSelection = {
      selectedRowKeys,
      onChange: this.handleRowSelectChange,
nikogu's avatar
nikogu committed
86 87 88
      getCheckboxProps: record => ({
        disabled: record.disabled,
      }),
89 90 91 92 93 94
    };

    return (
      <div className={styles.standardTable}>
        <div className={styles.tableAlert}>
          <Alert
jim's avatar
jim committed
95
            message={
陈帅's avatar
陈帅 committed
96
              <Fragment>
ddcat1115's avatar
ddcat1115 committed
97
                已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> 项&nbsp;&nbsp;
jim's avatar
jim committed
98 99 100 101 102
                {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}
ddcat1115's avatar
ddcat1115 committed
103
                    </span>
jim's avatar
jim committed
104 105 106 107 108
                  </span>
                ))}
                <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>
                  清空
                </a>
陈帅's avatar
陈帅 committed
109
              </Fragment>
jim's avatar
jim committed
110
            }
111 112 113 114 115 116
            type="info"
            showIcon
          />
        </div>
        <Table
          loading={loading}
117
          rowKey={rowKey || 'key'}
118 119 120 121 122 123 124 125 126 127 128 129
          rowSelection={rowSelection}
          dataSource={list}
          columns={columns}
          pagination={paginationProps}
          onChange={this.handleTableChange}
        />
      </div>
    );
  }
}

export default StandardTable;