index.jsx 5.3 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
  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);
    }
  },
A
afc163 已提交
43 44 45 46
  onSelectFilter() {
  },
  onDeselectFilter() {
  },
A
afc163 已提交
47
  renderColumnsDropdown() {
A
afc163 已提交
48
    this.props.columns = this.props.columns.map((column) => {
A
afc163 已提交
49 50 51 52 53
      if (!column.originTitle) {
        column.originTitle = column.title;
      }
      let filterDropdown, menus, sortButton;
      if (column.filter) {
A
afc163 已提交
54
        menus = <Menu multiple={true} onSelect={this.onSelectFilter} onDeselect={this.onDeselectFilter}>
A
afc163 已提交
55
          {this.renderMenus(column.filter())}
A
afc163 已提交
56 57 58
          <button className="ant-btn ant-btn-primary ant-btn-sm" onClick={column.onFilter.bind(this)}>
            确 定
          </button>
A
afc163 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        </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 已提交
85
      return column;
A
afc163 已提交
86 87
    });
  },
A
afc163 已提交
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 114 115 116 117 118 119 120 121
  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 已提交
122
  fetch: function(url) {
A
afc163 已提交
123 124 125
    this.props.resolve = this.props.resolve || function(data) {
      return data || [];
    };
A
afc163 已提交
126 127
    let dataSource = url || this.props.dataSource;
    if (dataSource) {
A
afc163 已提交
128 129 130 131
      this.setState({
        loading: true
      });
      jQuery.ajax({
A
afc163 已提交
132
        url: dataSource,
A
afc163 已提交
133
        success: (result) => {
A
afc163 已提交
134
          result = this.props.resolve.call(this, result);
A
afc163 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
          if (this.isMounted()) {
            this.setState({
              data: result
            });
          }
        },
        complete: () => {
          this.setState({
            loading: false
          });
        }
      });
    }
  },
  componentDidMount() {
A
afc163 已提交
150
    this.fetch();
A
afc163 已提交
151
  },
A
afc163 已提交
152
  render() {
A
afc163 已提交
153 154 155 156 157 158 159 160
    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 已提交
161
        width: 60,
A
afc163 已提交
162 163 164 165 166 167 168 169 170
        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 已提交
171 172 173 174 175 176 177
    var classString = '';
    if (this.props.loading) {
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
178
    this.renderColumnsDropdown();
A
afc163 已提交
179 180 181
    return <Table data={this.state.data}
      className={classString}
      {...this.props} />;
A
afc163 已提交
182 183 184 185
  }
});

export default AntTable;