index.js 3.11 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;
41 42 43 44
    needTotalList = needTotalList.map(item => ({
      ...item,
      total: selectedRows.reduce((sum, val) => sum + parseFloat(val[item.dataIndex], 10), 0),
    }));
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
45 46 47
    const { onSelectRow } = this.props;
    if (onSelectRow) {
      onSelectRow(selectedRows);
48 49
    }

ddcat1115's avatar
ddcat1115 committed
50
    this.setState({ selectedRowKeys, needTotalList });
jim's avatar
jim committed
51
  };
52 53

  handleTableChange = (pagination, filters, sorter) => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
54 55 56 57
    const { onChange } = this.props;
    if (onChange) {
      onChange(pagination, filters, sorter);
    }
jim's avatar
jim committed
58
  };
59 60 61

  cleanSelectedKeys = () => {
    this.handleRowSelectChange([], []);
jim's avatar
jim committed
62
  };
63 64

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

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

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

    return (
      <div className={styles.standardTable}>
        <div className={styles.tableAlert}>
          <Alert
jim's avatar
jim committed
87
            message={
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
88
              <Fragment>
ddcat1115's avatar
ddcat1115 committed
89
                ๅทฒ้€‰ๆ‹ฉ <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> ้กน&nbsp;&nbsp;
jim's avatar
jim committed
90 91
                {needTotalList.map(item => (
                  <span style={{ marginLeft: 8 }} key={item.dataIndex}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
92 93
                    {item.title}
                    ๆ€ป่ฎก&nbsp;
jim's avatar
jim committed
94 95
                    <span style={{ fontWeight: 600 }}>
                      {item.render ? item.render(item.total) : item.total}
ddcat1115's avatar
ddcat1115 committed
96
                    </span>
jim's avatar
jim committed
97 98 99 100 101
                  </span>
                ))}
                <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>
                  ๆธ…็ฉบ
                </a>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
102
              </Fragment>
jim's avatar
jim committed
103
            }
104 105 106 107 108
            type="info"
            showIcon
          />
        </div>
        <Table
109
          rowKey={rowKey || 'key'}
110 111 112 113
          rowSelection={rowSelection}
          dataSource={list}
          pagination={paginationProps}
          onChange={this.handleTableChange}
114
          {...rest}
115 116 117 118 119 120 121
        />
      </div>
    );
  }
}

export default StandardTable;