index.jsx 5.1 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
import Table from 'rc-table';
A
afc163 已提交
6 7
import Menu from 'rc-menu';
import Dropdown from '../dropdown';
A
afc163 已提交
8 9

let AntTable = React.createClass({
A
afc163 已提交
10 11
  getInitialState() {
    return {
A
afc163 已提交
12 13 14
      selectedRowKeys: [],
      loading: false,
      data: []
A
afc163 已提交
15 16
    };
  },
A
afc163 已提交
17 18
  getDefaultProps() {
    return {
A
afc163 已提交
19
      prefixCls: 'ant-table',
A
afc163 已提交
20
      useFixedHeader: false,
A
afc163 已提交
21 22
      rowSelection: null,
      size: 'normal'
A
afc163 已提交
23 24
    };
  },
A
afc163 已提交
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  renderMenus(items) {
    let menuItems = items.map((item) => {
      return <Menu.Item key={item.value}>
        {item.text}
      </Menu.Item>;
    });
    return menuItems;
  },
  toggleSortOrder(order, column) {
    if (column.sortOrder === order) {
      column.sortOrder = '';
    } else {
      column.sortOrder = order;
    }
    if (column.sorter) {
      column.sorter.call(this, column.sortOrder);
    }
  },
  renderColumnsDropdown() {
    this.props.columns.forEach((column) => {
      if (!column.originTitle) {
        column.originTitle = column.title;
      }
      let filterDropdown, menus, sortButton;
      if (column.filter) {
        menus = <Menu multiple={true} onSelect={column.onFilter.bind(this)}>
          {this.renderMenus(column.filter())}
        </Menu>;
        filterDropdown = <Dropdown trigger="click" closeOnSelect={false} overlay={menus}>
          <i className="anticon anticon-bars"></i>
        </Dropdown>;
      }
      if (column.sorter) {
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
                           (column.sortOrder === 'ascend' ? 'on' : 'off')}
            title="升序排序"
            onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
            <i className="anticon anticon-caret-up"></i>
          </span>
          <span className={'ant-table-column-sorter-down ' +
                           (column.sortOrder === 'descend' ? 'on' : 'off')}
            title="降序排序"
            onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
            <i className="anticon anticon-caret-down"></i>
          </span>
        </div>;
      }
      column.title = [
        column.originTitle,
        sortButton,
        filterDropdown
      ];
    });
  },
A
afc163 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  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 已提交
114
  fetch: function(url) {
A
afc163 已提交
115 116 117
    this.props.resolve = this.props.resolve || function(data) {
      return data || [];
    };
A
afc163 已提交
118 119
    let dataSource = url || this.props.dataSource;
    if (dataSource) {
A
afc163 已提交
120 121 122 123
      this.setState({
        loading: true
      });
      jQuery.ajax({
A
afc163 已提交
124
        url: dataSource,
A
afc163 已提交
125
        success: (result) => {
A
afc163 已提交
126
          result = this.props.resolve.call(this, result);
A
afc163 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
          if (this.isMounted()) {
            this.setState({
              data: result
            });
          }
        },
        complete: () => {
          this.setState({
            loading: false
          });
        }
      });
    }
  },
  componentDidMount() {
A
afc163 已提交
142
    this.fetch();
A
afc163 已提交
143
  },
A
afc163 已提交
144
  render() {
A
afc163 已提交
145 146 147 148 149 150 151 152
    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 已提交
153
        width: 60,
A
afc163 已提交
154 155 156 157 158 159 160 161 162
        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 已提交
163 164 165 166 167 168 169
    var classString = '';
    if (this.props.loading) {
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
170
    this.renderColumnsDropdown();
A
afc163 已提交
171 172 173
    return <Table data={this.state.data}
      className={classString}
      {...this.props} />;
A
afc163 已提交
174 175 176 177
  }
});

export default AntTable;