index.jsx 9.8 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
import Dropdown from '../dropdown';
A
afc163 已提交
7
import FilterMenu from './filterMenu';
8
import Pagination from '../pagination';
A
afc163 已提交
9
import objectAssign from 'object-assign';
A
afc163 已提交
10 11

let AntTable = React.createClass({
A
afc163 已提交
12
  getInitialState() {
13 14 15 16 17 18 19
    // 支持两种模式
    if (Array.isArray(this.props.dataSource)) {
      this.mode = 'local';
      // 保留原来的数据
      this.originDataSource = this.props.dataSource.slice(0);
    } else {
      this.mode = 'remote';
A
afc163 已提交
20 21
      this.props.dataSource = objectAssign({
        resolve: function(data) {
22
          return data || [];
A
afc163 已提交
23 24 25 26
        },
        getParams: function() {},
        getPagination: function() {}
      }, this.props.dataSource);
27 28 29 30 31
    }
    var pagination;
    if (this.props.pagination === false) {
      pagination = false;
    } else {
A
afc163 已提交
32 33 34 35
      pagination = objectAssign({
        pageSize: 10,
        total: this.props.dataSource.length
      }, this.props.pagination);
36
    }
A
afc163 已提交
37
    return {
A
afc163 已提交
38 39
      selectedRowKeys: [],
      loading: false,
40
      pagination: pagination,
A
afc163 已提交
41
      data: []
A
afc163 已提交
42 43
    };
  },
A
afc163 已提交
44 45
  getDefaultProps() {
    return {
A
afc163 已提交
46
      prefixCls: 'ant-table',
A
afc163 已提交
47
      useFixedHeader: false,
A
afc163 已提交
48 49
      rowSelection: null,
      size: 'normal'
A
afc163 已提交
50 51
    };
  },
A
afc163 已提交
52
  toggleSortOrder(order, column) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
    // 同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    if (sortColumn) {
      sortColumn.className = '';
    }
    if (sortColumn !== column) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
      sortColumn.className = 'ant-table-column-sort';
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
        sortColumn.className = 'ant-table-column-sort';
      }
A
afc163 已提交
71
    }
72 73 74
    if (this.mode === 'local') {
      let sorter = function() {
        let result = column.sorter.apply(this, arguments);
75
        if (sortOrder === 'ascend') {
76
          return result;
77
        } else if (sortOrder === 'descend') {
78 79 80
          return -result;
        }
      };
81
      if (sortOrder) {
82 83 84 85
        this.props.dataSource = this.props.dataSource.sort(sorter);
      } else {
        this.props.dataSource = this.originDataSource.slice();
      }
A
afc163 已提交
86
    }
A
afc163 已提交
87 88 89 90
    this.setState({
      sortOrder: sortOrder,
      sortColumn: sortColumn
    }, this.fetch);
A
afc163 已提交
91
  },
A
afc163 已提交
92
  handleFilter(column) {
93 94 95 96 97 98 99 100
    if (this.mode === 'local') {
      this.props.dataSource = this.originDataSource.slice().filter(function(record) {
        if (column.selectedFilters.length === 0) {
          return true;
        }
        return column.selectedFilters.some(function(value) {
          return column.onFilter.call(this, value, record);
        });
101
      });
102
    }
103
    this.fetch();
A
afc163 已提交
104
  },
105 106 107 108 109 110 111
  handleSelect(e) {
    let checked = e.currentTarget.checked;
    let currentRowIndex = e.currentTarget.parentElement.parentElement.rowIndex;
    let selectedRow = this.state.data[currentRowIndex - 1];
    if (checked) {
      this.state.selectedRowKeys.push(currentRowIndex);
    } else {
A
afc163 已提交
112
      this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i) {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        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.state.data.map(function(item, i) {
        return i + 1;
      }) : []
    });
    if (this.props.rowSelection.onSelectAll) {
      this.props.rowSelection.onSelectAll(checked);
A
afc163 已提交
132
    }
A
afc163 已提交
133
  },
134
  handlePageChange: function(current) {
A
afc163 已提交
135 136 137 138 139
    let pagination = this.state.pagination;
    pagination.current = current || 1;
    this.setState({
      pagination: pagination
    }, this.fetch);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  },
  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;
  },
  renderRowSelection() {
    var columns = this.props.columns;
    if (this.props.rowSelection) {
      let checked = this.state.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,
        width: 60,
A
afc163 已提交
157 158
        render: this.renderSelectionCheckBox,
        className: 'ant-table-selection-column'
159 160 161 162 163 164 165
      };
      if (columns[0] &&
          columns[0].key === 'selection-column') {
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
A
afc163 已提交
166 167 168 169 170
      // 加上选中行的样式
      this.props.rowClassName = (record, i) => {
        return this.state.selectedRowKeys.indexOf(i + 1) >= 0 ?
          'ant-table-row-selected' : '';
      };
171 172 173
    }
    return columns;
  },
A
afc163 已提交
174
  renderColumnsDropdown() {
175
    return this.props.columns.map((column) => {
A
afc163 已提交
176 177 178 179
      if (!column.originTitle) {
        column.originTitle = column.title;
      }
      let filterDropdown, menus, sortButton;
180
      if (column.filters && column.filters.length > 0) {
181
        column.selectedFilters = column.selectedFilters || [];
A
afc163 已提交
182
        menus = <FilterMenu column={column} confirmFilter={this.handleFilter.bind(this, column)} />;
A
afc163 已提交
183
        let dropdownSelectedClass = '';
184
        if (column.selectedFilters && column.selectedFilters.length > 0) {
A
afc163 已提交
185 186 187 188 189 190
          dropdownSelectedClass = 'ant-table-filter-selected';
        }
        filterDropdown = <Dropdown trigger="click"
          closeOnSelect={false}
          overlay={menus}>
          <i title="筛选" className={'anticon anticon-bars ' + dropdownSelectedClass}></i>
A
afc163 已提交
191 192 193
        </Dropdown>;
      }
      if (column.sorter) {
194
        let isSortColumn = (this.state.sortColumn === column);
A
afc163 已提交
195 196
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
197
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
A
afc163 已提交
198 199 200 201 202
            title="升序排序"
            onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
            <i className="anticon anticon-caret-up"></i>
          </span>
          <span className={'ant-table-column-sorter-down ' +
203
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
A
afc163 已提交
204 205 206 207 208 209 210 211 212 213 214
            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 已提交
215
      return column;
A
afc163 已提交
216 217
    });
  },
218 219 220 221
  renderPagination() {
    // 强制不需要分页
    if (this.state.pagination === false) {
      return '';
A
afc163 已提交
222
    }
A
afc163 已提交
223 224 225 226 227
    let classString = 'ant-table-pagination';
    if (this.props.size === 'small') {
      classString += ' mini';
    }
    return <Pagination className={classString}
228 229
      onChange={this.handlePageChange}
      {...this.state.pagination} />;
A
afc163 已提交
230
  },
231 232 233 234
  prepareParamsArguments() {
    // 准备筛选、排序、分页的参数
    let pagination;
    let filters = {};
A
afc163 已提交
235
    let sorter = {};
236 237 238 239 240 241 242
    pagination = this.state.pagination;
    this.props.columns.forEach(function(column) {
      if (column.dataIndex && column.selectedFilters &&
          column.selectedFilters.length > 0) {
        filters[column.dataIndex] = column.selectedFilters;
      }
    });
A
afc163 已提交
243 244 245 246 247 248
    if (this.state.sortColumn && this.state.sortOrder &&
        this.state.sortColumn.dataIndex) {
      sorter.field = this.state.sortColumn.dataIndex;
      sorter.order = this.state.sortOrder;
    }
    return [pagination, filters, sorter];
249
  },
250 251 252
  fetch: function() {
    let dataSource = this.props.dataSource;
    if (this.mode === 'remote') {
A
afc163 已提交
253 254 255 256
      this.setState({
        loading: true
      });
      jQuery.ajax({
257
        url: dataSource.url,
A
afc163 已提交
258 259 260
        data: dataSource.getParams.apply(this, this.prepareParamsArguments()) || {},
        headers: dataSource.headers,
        dataType: 'json',
A
afc163 已提交
261 262
        success: (result) => {
          if (this.isMounted()) {
A
afc163 已提交
263
            let pagination = objectAssign(
A
afc163 已提交
264 265 266
              this.state.pagination,
              dataSource.getPagination.call(this, result)
            );
A
afc163 已提交
267
            this.setState({
268
              data: dataSource.resolve.call(this, result),
A
afc163 已提交
269 270
              pagination: pagination,
              loading: false
A
afc163 已提交
271 272 273
            });
          }
        },
A
afc163 已提交
274
        error: () => {
A
afc163 已提交
275 276 277 278 279
          this.setState({
            loading: false
          });
        }
      });
280
    } else {
A
afc163 已提交
281 282 283 284 285 286 287 288
      let pageSize = this.state.pagination.pageSize;
      let current = this.state.pagination.current;
      this.setState({
        data: this.props.dataSource.filter(function(item, i) {
          if (i >= (current - 1) * pageSize &&
              i < current * pageSize) {
            return item;
          }
A
afc163 已提交
289
        })
A
afc163 已提交
290
      });
A
afc163 已提交
291 292 293
    }
  },
  componentDidMount() {
A
afc163 已提交
294
    this.handlePageChange();
A
afc163 已提交
295
  },
A
afc163 已提交
296
  render() {
297 298
    this.props.columns = this.renderRowSelection();

A
afc163 已提交
299
    var classString = '';
A
afc163 已提交
300
    if (this.state.loading) {
A
afc163 已提交
301 302 303 304 305
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
306 307 308 309

    return <div className="clearfix">
      <Table data={this.state.data}
      columns={this.renderColumnsDropdown()}
A
afc163 已提交
310
      className={classString}
311 312 313
      {...this.props} />
      {this.renderPagination()}
    </div>;
A
afc163 已提交
314 315 316 317
  }
});

export default AntTable;