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

nikogu's avatar
nikogu committed
6
const statusMap = ['default', 'processing', 'success', 'error'];
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
class StandardTable extends PureComponent {
  state = {
    selectedRowKeys: [],
    totalCallNo: 0,
  };

  componentWillReceiveProps(nextProps) {
    // clean state
    if (nextProps.selectedRows.length === 0) {
      this.setState({
        selectedRowKeys: [],
        totalCallNo: 0,
      });
    }
  }

  handleRowSelectChange = (selectedRowKeys, selectedRows) => {
    const totalCallNo = selectedRows.reduce((sum, val) => {
      return sum + parseFloat(val.callNo, 10);
    }, 0);

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

afc163's avatar
afc163 committed
32
    this.setState({ selectedRowKeys, totalCallNo });
33 34 35 36 37 38 39 40 41 42 43 44 45 46
  }

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

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

  render() {
    const { selectedRowKeys, totalCallNo } = this.state;
    const { data: { list, pagination }, loading } = this.props;

nikogu's avatar
nikogu committed
47
    const status = ['关闭', '运行中', '已上线', '异常'];
48 49 50 51 52 53 54 55 56 57 58 59 60 61

    const columns = [
      {
        title: '规则编号',
        dataIndex: 'no',
      },
      {
        title: '描述',
        dataIndex: 'description',
      },
      {
        title: '服务调用次数',
        dataIndex: 'callNo',
        sorter: true,
afc163's avatar
afc163 committed
62 63
        align: 'right',
        render: val => `${val} 万`,
64 65 66 67 68 69 70 71 72 73 74 75 76
      },
      {
        title: '状态',
        dataIndex: 'status',
        filters: [
          {
            text: status[0],
            value: 0,
          },
          {
            text: status[1],
            value: 1,
          },
nikogu's avatar
nikogu committed
77 78 79 80 81 82 83 84
          {
            text: status[2],
            value: 2,
          },
          {
            text: status[3],
            value: 3,
          },
85 86
        ],
        render(val) {
nikogu's avatar
nikogu committed
87
          return <Badge status={statusMap[val]} text={status[val]} />;
88 89 90 91 92 93 94 95 96 97 98
        },
      },
      {
        title: '更新时间',
        dataIndex: 'updatedAt',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD HH:mm:ss')}</span>,
      },
      {
        title: '操作',
        render: () => (
陈帅's avatar
陈帅 committed
99
          <Fragment>
100
            <a href="">配置</a>
afc163's avatar
afc163 committed
101
            <Divider type="vertical" />
102
            <a href="">订阅警报</a>
陈帅's avatar
陈帅 committed
103
          </Fragment>
104 105 106 107 108 109 110 111 112 113 114 115 116
        ),
      },
    ];

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

    const rowSelection = {
      selectedRowKeys,
      onChange: this.handleRowSelectChange,
nikogu's avatar
nikogu committed
117 118 119
      getCheckboxProps: record => ({
        disabled: record.disabled,
      }),
120 121 122 123 124 125 126
    };

    return (
      <div className={styles.standardTable}>
        <div className={styles.tableAlert}>
          <Alert
            message={(
afc163's avatar
afc163 committed
127
              <div>
ddcat1115's avatar
ddcat1115 committed
128
                已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> 项&nbsp;&nbsp;
129
                服务调用总计 <span style={{ fontWeight: 600 }}>{totalCallNo}</span> 
niko's avatar
niko committed
130
                <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>清空</a>
afc163's avatar
afc163 committed
131
              </div>
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            )}
            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;