index.jsx 3.2 KB
Newer Older
A
afc163 已提交
1 2
'use strict';

A
afc163 已提交
3
import React from 'react';
A
afc163 已提交
4
import jQuery from 'jquery';
A
afc163 已提交
5 6 7
import Table from 'rc-table';

let AntTable = React.createClass({
A
afc163 已提交
8 9
  getInitialState() {
    return {
A
afc163 已提交
10 11 12
      selectedRowKeys: [],
      loading: false,
      data: []
A
afc163 已提交
13 14
    };
  },
A
afc163 已提交
15 16
  getDefaultProps() {
    return {
A
afc163 已提交
17
      prefixCls: 'ant-table',
A
afc163 已提交
18
      useFixedHeader: false,
A
afc163 已提交
19 20
      rowSelection: null,
      size: 'normal'
A
afc163 已提交
21 22
    };
  },
A
afc163 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  handleSelect(e) {
    let checked = e.currentTarget.checked;
    let currentRowIndex = e.currentTarget.parentElement.parentElement.rowIndex;
    let selectedRow = this.props.data[currentRowIndex - 1];
    if (checked) {
      this.state.selectedRowKeys.push(currentRowIndex);
    } else {
      this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i){
        return currentRowIndex !== i;
      });
    }
    this.setState({
      selectedRowKeys: this.state.selectedRowKeys
    });
    if (this.props.rowSelection.onSelect) {
      this.props.rowSelection.onSelect(selectedRow, checked);
    }
  },
  handleSelectAllRow(e) {
    let checked = e.currentTarget.checked;
    this.setState({
      selectedRowKeys: checked ? this.props.data.map(function(item, i) {
        return i + 1;
      }) : []
    });
    if (this.props.rowSelection.onSelectAll) {
      this.props.rowSelection.onSelectAll(checked);
    }
  },
  renderSelectionCheckBox(value, record, index) {
    let checked = this.state.selectedRowKeys.indexOf(index + 1) >= 0;
    let checkbox = <input type="checkbox" checked={checked} onChange={this.handleSelect} />;
    return checkbox;
  },
A
afc163 已提交
57
  loadData: function() {
A
afc163 已提交
58 59 60
    this.props.resolve = this.props.resolve || function(data) {
      return data || [];
    };
A
afc163 已提交
61 62 63 64 65 66 67
    if (this.props.dataSource) {
      this.setState({
        loading: true
      });
      jQuery.ajax({
        url: this.props.dataSource,
        success: (result) => {
A
afc163 已提交
68
          result = this.props.resolve.call(this, result);
A
afc163 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
          if (this.isMounted()) {
            this.setState({
              data: result
            });
          }
        },
        complete: () => {
          this.setState({
            loading: false
          });
        }
      });
    }
  },
  componentDidMount() {
    this.loadData();
  },
A
afc163 已提交
86
  render() {
A
afc163 已提交
87 88 89 90 91 92 93 94
    if (this.props.rowSelection) {
      let checked = this.props.data.every(function(item, i) {
        return this.state.selectedRowKeys.indexOf(i + 1) >= 0;
      }, this);
      let checkboxAll = <input type="checkbox" checked={checked} onChange={this.handleSelectAllRow} />;
      let selectionColumn = {
        key: 'selection-column',
        title: checkboxAll,
A
afc163 已提交
95
        width: 60,
A
afc163 已提交
96 97 98 99 100 101 102 103 104
        render: this.renderSelectionCheckBox
      };
      if (this.props.columns[0] &&
          this.props.columns[0].key === 'selection-column') {
        this.props.columns[0] = selectionColumn;
      } else {
        this.props.columns.unshift(selectionColumn);
      }
    }
A
afc163 已提交
105 106 107 108 109 110 111 112 113 114 115
    var classString = '';
    if (this.props.loading) {
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
    // 'message message-important message-read'
    return <Table data={this.state.data}
      className={classString}
      {...this.props} />;
A
afc163 已提交
116 117 118 119
  }
});

export default AntTable;