index.jsx 6.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
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
      selectedRowKeys: [],
      loading: false,
A
afc163 已提交
14
      filters: [],
A
afc163 已提交
15
      data: []
A
afc163 已提交
16 17
    };
  },
A
afc163 已提交
18 19
  getDefaultProps() {
    return {
A
afc163 已提交
20
      prefixCls: 'ant-table',
A
afc163 已提交
21
      useFixedHeader: false,
A
afc163 已提交
22 23
      rowSelection: null,
      size: 'normal'
A
afc163 已提交
24 25
    };
  },
A
afc163 已提交
26 27
  renderMenus(items) {
    let menuItems = items.map((item) => {
A
afc163 已提交
28
      return <Menu.Item key={item.value}>{item.text}</Menu.Item>;
A
afc163 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
    });
    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 已提交
42 43 44 45 46 47 48 49
  handleFilter(column) {
    if (column.onFilter) {
      column.onFilter.call(this, column.filters);
    }
  },
  handleSelectFilter(column, selected) {
    column.filters = column.filters || [];
    column.filters.push(selected);
A
afc163 已提交
50
  },
A
afc163 已提交
51 52 53 54 55 56
  handleDeselectFilter(column, key) {
    column.filters = column.filters || [];
    var index = column.filters.indexOf(key);
    if (index !== -1) {
      column.filters.splice(index, 1);
    }
A
afc163 已提交
57
  },
A
afc163 已提交
58
  renderColumnsDropdown() {
A
afc163 已提交
59
    this.props.columns = this.props.columns.map((column) => {
A
afc163 已提交
60 61 62 63 64
      if (!column.originTitle) {
        column.originTitle = column.title;
      }
      let filterDropdown, menus, sortButton;
      if (column.filter) {
A
afc163 已提交
65 66 67 68
        menus = <Menu multiple={true}
          className="ant-table-filter-dropdown"
          onSelect={this.handleSelectFilter.bind(this, column)}
          onDeselect={this.handleDeselectFilter.bind(this, column)}>
A
afc163 已提交
69
          {this.renderMenus(column.filter())}
A
afc163 已提交
70 71 72 73 74 75 76 77 78 79
          <Menu.Item disabled>
            <button style={{
                cursor: 'pointer',
                pointerEvents: 'visible'
              }}
              className="ant-btn ant-btn-primary ant-btn-sm"
              onClick={this.handleFilter.bind(this, column)}>
              确 定
            </button>
          </Menu.Item>
A
afc163 已提交
80
        </Menu>;
A
afc163 已提交
81 82 83 84 85 86 87 88
        let dropdownSelectedClass = '';
        if (column.filters && column.filters.length > 0) {
          dropdownSelectedClass = 'ant-table-filter-selected';
        }
        filterDropdown = <Dropdown trigger="click"
          closeOnSelect={false}
          overlay={menus}>
          <i title="筛选" className={'anticon anticon-bars ' + dropdownSelectedClass}></i>
A
afc163 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        </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 已提交
112
      return column;
A
afc163 已提交
113 114
    });
  },
A
afc163 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  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 已提交
149
  fetch: function(url) {
A
afc163 已提交
150 151 152
    this.props.resolve = this.props.resolve || function(data) {
      return data || [];
    };
A
afc163 已提交
153 154
    let dataSource = url || this.props.dataSource;
    if (dataSource) {
A
afc163 已提交
155 156 157 158
      this.setState({
        loading: true
      });
      jQuery.ajax({
A
afc163 已提交
159
        url: dataSource,
A
afc163 已提交
160
        success: (result) => {
A
afc163 已提交
161
          result = this.props.resolve.call(this, result);
A
afc163 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
          if (this.isMounted()) {
            this.setState({
              data: result
            });
          }
        },
        complete: () => {
          this.setState({
            loading: false
          });
        }
      });
    }
  },
  componentDidMount() {
A
afc163 已提交
177
    this.fetch();
A
afc163 已提交
178
  },
A
afc163 已提交
179
  render() {
A
afc163 已提交
180 181 182 183 184 185 186 187
    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 已提交
188
        width: 60,
A
afc163 已提交
189 190 191 192 193 194 195 196 197
        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 已提交
198 199 200 201 202 203 204
    var classString = '';
    if (this.props.loading) {
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
205
    this.renderColumnsDropdown();
A
afc163 已提交
206 207 208
    return <Table data={this.state.data}
      className={classString}
      {...this.props} />;
A
afc163 已提交
209 210 211 212
  }
});

export default AntTable;