index.jsx 19.4 KB
Newer Older
A
afc163 已提交
1
import React from 'react';
A
afc163 已提交
2
import reqwest from 'reqwest';
A
afc163 已提交
3
import Table from 'rc-table';
A
afc163 已提交
4
import Checkbox from '../checkbox';
R
RaoHai 已提交
5
import Radio from '../radio';
A
afc163 已提交
6
import FilterDropdown from './filterDropdown';
7
import Pagination from '../pagination';
E
elrrrrrrr 已提交
8
import Icon from '../iconfont';
A
afc163 已提交
9
import objectAssign from 'object-assign';
K
KgTong 已提交
10
import Spin from '../spin';
A
afc163 已提交
11

Y
yiminghe 已提交
12 13
function noop() {
}
14

Y
yiminghe 已提交
15 16 17
function defaultResolve(data) {
  return data || [];
}
18

19
class DataSource {
Y
yiminghe 已提交
20 21
  init(config) {
    this.config = config;
dqaria's avatar
dqaria 已提交
22
    this.url = config.url || '';
23 24 25
    this.resolve = config.resolve || defaultResolve;
    this.getParams = config.getParams || noop;
    this.getPagination = config.getPagination || noop;
A
afc163 已提交
26
    this.headers = config.headers || {};
A
afc163 已提交
27
    this.data = config.data || {};
Y
yiminghe 已提交
28 29 30 31 32 33 34 35
  }

  constructor(config) {
    if (config) {
      this.init(config);
    }
  }

36
  clone(config = {}) {
37
    return new DataSource(objectAssign({}, this.config, config));
38 39 40
  }
}

A
afc163 已提交
41
let AntTable = React.createClass({
Y
yiminghe 已提交
42
  getInitialState() {
A
afc163 已提交
43
    return {
Y
yiminghe 已提交
44
      // 减少状态
A
afc163 已提交
45
      selectedRowKeys: [],
Y
yiminghe 已提交
46 47
      // only for remote
      data: [],
48
      dataSource: this.props.dataSource,
Y
yiminghe 已提交
49
      filters: {},
A
afc163 已提交
50
      selectionDirty: false,
A
afc163 已提交
51
      loading: this.props.loading,
Y
yiminghe 已提交
52 53 54
      sortColumn: '',
      sortOrder: '',
      sorter: null,
R
RaoHai 已提交
55
      radioIndex: null,
Y
yiminghe 已提交
56
      pagination: this.hasPagination() ? objectAssign({
A
afc163 已提交
57 58
        pageSize: 10,
        current: 1
Y
yiminghe 已提交
59
      }, this.props.pagination) : {}
A
afc163 已提交
60 61
    };
  },
Y
yiminghe 已提交
62

A
afc163 已提交
63 64
  getDefaultProps() {
    return {
A
afc163 已提交
65
      prefixCls: 'ant-table',
A
afc163 已提交
66
      useFixedHeader: false,
A
afc163 已提交
67
      rowSelection: null,
Y
yiminghe 已提交
68
      className: '',
69
      size: 'default',
A
afc163 已提交
70
      loading: false,
A
afc163 已提交
71
      bordered: false,
Y
yiminghe 已提交
72 73
      onChange: function () {
      }
A
afc163 已提交
74 75
    };
  },
Y
yiminghe 已提交
76

A
afc163 已提交
77
  propTypes: {
A
afc163 已提交
78
    dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
A
afc163 已提交
79 80
  },

R
RaoHai 已提交
81 82 83 84 85 86
  getDefaultSelection() {
    let selectedRowKeys = [];
    if (this.props.rowSelection && this.props.rowSelection.getCheckboxProps) {
      let data = this.getCurrentPageData();
      data.filter((item) => {
        if (this.props.rowSelection.getCheckboxProps) {
87
          return this.props.rowSelection.getCheckboxProps(item).defaultChecked;
R
RaoHai 已提交
88 89 90 91 92 93 94 95 96
        }
        return true;
      }).map((record, rowIndex) => {
        selectedRowKeys.push(this.getRecordKey(record, rowIndex));
      });
    }
    return selectedRowKeys;
  },

A
afc163 已提交
97
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
98
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
99 100 101
      this.setState({
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
      });
A
afc163 已提交
102
    }
103 104
    // 外界只有 dataSource 的变化会触发新请求
    if ('dataSource' in nextProps &&
A
afc163 已提交
105 106 107 108 109 110 111 112 113
        nextProps.dataSource !== this.props.dataSource) {
      let selectedRowKeys = this.state.selectedRowKeys;
      // 把不在当前页的选中项去掉
      if (this.isLocalDataSource()) {
        let currentPageRowKeys = this.getLocalDataPaging(nextProps.dataSource);
        selectedRowKeys = selectedRowKeys.filter((key) => {
          return currentPageRowKeys.indexOf(key) >= 0;
        });
      }
114
      this.setState({
A
afc163 已提交
115
        selectionDirty: false,
116 117
        dataSource: nextProps.dataSource,
        loading: true
118
      }, this.fetch);
Y
yiminghe 已提交
119 120
    }
    if (nextProps.columns !== this.props.columns) {
121 122 123
      this.setState({
        filters: {}
      });
A
afc163 已提交
124
    }
A
afc163 已提交
125 126 127 128 129
    if ('loading' in nextProps) {
      this.setState({
        loading: nextProps.loading
      });
    }
A
afc163 已提交
130
  },
A
afc163 已提交
131 132

  hasPagination(pagination) {
Y
yiminghe 已提交
133 134
    if (pagination === undefined) {
      pagination = this.props.pagination;
A
afc163 已提交
135
    }
Y
yiminghe 已提交
136 137
    return pagination !== false;
  },
A
afc163 已提交
138 139

  isLocalDataSource() {
140
    return Array.isArray(this.state.dataSource);
A
afc163 已提交
141
  },
A
afc163 已提交
142 143

  getRemoteDataSource() {
144
    return this.state.dataSource;
A
afc163 已提交
145
  },
A
afc163 已提交
146

A
afc163 已提交
147
  toggleSortOrder(order, column) {
148 149
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
J
jljsj 已提交
150
    let sorter;
A
afc163 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
Y
yiminghe 已提交
164 165
    if (this.isLocalDataSource()) {
      sorter = function () {
166
        let result = column.sorter.apply(this, arguments);
167
        if (sortOrder === 'ascend') {
168
          return result;
169
        } else if (sortOrder === 'descend') {
170 171 172
          return -result;
        }
      };
A
afc163 已提交
173
    }
A
afc163 已提交
174 175 176 177 178 179 180
    const newState = {
      sortOrder,
      sortColumn,
      sorter
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
181
  },
A
afc163 已提交
182

Y
yiminghe 已提交
183 184 185 186
  handleFilter(column, filters) {
    filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: filters
    });
A
afc163 已提交
187
    const newState = {
Y
yiminghe 已提交
188
      selectedRowKeys: [],
A
afc163 已提交
189
      selectionDirty: false,
A
afc163 已提交
190 191 192 193
      filters
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
194
  },
A
afc163 已提交
195

Y
yiminghe 已提交
196
  handleSelect(record, rowIndex, e) {
A
afc163 已提交
197
    let checked = e.target.checked;
R
RaoHai 已提交
198
    let defaultSelection = [];
A
afc163 已提交
199
    if (!this.state.selectionDirty) {
R
RaoHai 已提交
200 201 202
      defaultSelection = this.getDefaultSelection();
    }
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
203
    let key = this.getRecordKey(record, rowIndex);
204
    if (checked) {
Y
yiminghe 已提交
205
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
206
    } else {
Y
yiminghe 已提交
207 208
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
209 210 211
      });
    }
    this.setState({
R
RaoHai 已提交
212
      selectedRowKeys: selectedRowKeys,
A
afc163 已提交
213
      selectionDirty: true
R
RaoHai 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
    });
    if (this.props.rowSelection.onSelect) {
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
      });
      this.props.rowSelection.onSelect(record, checked, selectedRows);
    }
  },

  handleRadioSelect: function (record, rowIndex, e) {
    let checked = e.target.checked;
    let defaultSelection = [];
A
afc163 已提交
227
    if (!this.state.selectionDirty) {
R
RaoHai 已提交
228 229 230 231 232 233 234 235
      defaultSelection = this.getDefaultSelection();
    }
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
      selectedRowKeys: selectedRowKeys,
      radioIndex: record.key,
A
afc163 已提交
236
      selectionDirty: true
237 238
    });
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
239 240 241
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
242
      });
Y
yiminghe 已提交
243
      this.props.rowSelection.onSelect(record, checked, selectedRows);
244 245
    }
  },
A
afc163 已提交
246

A
afc163 已提交
247 248
  handleSelectAllRow(e) {
    let checked = e.target.checked;
Y
yiminghe 已提交
249
    let data = this.getCurrentPageData();
R
RaoHai 已提交
250 251 252 253 254 255
    let selectedRowKeys = checked ? data.filter((item) => {
      if (this.props.rowSelection.getCheckboxProps) {
        return !this.props.rowSelection.getCheckboxProps(item).disabled;
      }
      return true;
    }).map((item, i) => {
Y
yiminghe 已提交
256 257
      return this.getRecordKey(item, i);
    }) : [];
A
afc163 已提交
258
    this.setState({
R
RaoHai 已提交
259
      selectedRowKeys: selectedRowKeys,
A
afc163 已提交
260
      selectionDirty: true
261 262
    });
    if (this.props.rowSelection.onSelectAll) {
Y
yiminghe 已提交
263 264
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
265 266
      });
      this.props.rowSelection.onSelectAll(checked, selectedRows);
A
afc163 已提交
267
    }
A
afc163 已提交
268
  },
A
afc163 已提交
269

270
  handlePageChange(current) {
Y
yiminghe 已提交
271
    let pagination = objectAssign({}, this.state.pagination);
272 273 274 275 276
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
A
afc163 已提交
277
    const newState = {
Y
yiminghe 已提交
278 279
      // 防止内存泄漏,只维持当页
      selectedRowKeys: [],
A
afc163 已提交
280
      selectionDirty: false,
A
afc163 已提交
281 282 283 284
      pagination
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
285
  },
A
afc163 已提交
286

R
RaoHai 已提交
287 288 289
  onRadioChange: function (ev) {
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
290
    });
291
  },
A
afc163 已提交
292

R
RaoHai 已提交
293 294 295 296 297 298
  renderSelectionRadio(value, record, index) {
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
A
afc163 已提交
299 300 301 302 303 304 305
    let checked;
    if (this.state.selectionDirty) {
      checked = this.state.radioIndex === record.key;
    } else {
      checked = (this.state.radioIndex === record.key ||
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
Y
yiminghe 已提交
306 307
    return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
                  value={record.key} checked={checked}/>;
R
RaoHai 已提交
308 309
  },

310
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
311
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
312 313 314 315 316 317 318
    let checked;
    if (this.state.selectionDirty) {
      checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
    } else {
      checked = (this.state.selectedRowKeys.indexOf(rowIndex) >= 0 ||
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
R
RaoHai 已提交
319 320 321 322
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
Y
yiminghe 已提交
323 324
    return <Checkbox checked={checked} disabled={props.disabled}
                     onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
Y
yiminghe 已提交
325
  },
A
afc163 已提交
326 327

  getRecordKey(record, index) {
328 329 330
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
331
    return record.key || index;
332
  },
A
afc163 已提交
333

334
  renderRowSelection() {
Y
yiminghe 已提交
335
    let columns = this.props.columns.concat();
336
    if (this.props.rowSelection) {
Y
yiminghe 已提交
337 338 339 340 341
      let data = this.getCurrentPageData();
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
342
        data = data.filter((item) => {
R
RaoHai 已提交
343 344 345 346
          if (this.props.rowSelection.getCheckboxProps) {
            return !this.props.rowSelection.getCheckboxProps(item).disabled;
          }
          return true;
Y
yiminghe 已提交
347
        });
A
afc163 已提交
348 349 350 351 352 353
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
          : data.every((item, i) =>
              this.props.rowSelection.getCheckboxProps &&
              this.props.rowSelection.getCheckboxProps(item).defaultChecked);
Y
yiminghe 已提交
354
      }
R
RaoHai 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          width: 60,
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
        let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow}/>;
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          width: 60,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
373
      if (columns[0] &&
Y
yiminghe 已提交
374
        columns[0].key === 'selection-column') {
375 376 377 378 379 380 381
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
382

A
afc163 已提交
383
  getCurrentPageData() {
Y
yiminghe 已提交
384 385 386
    return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
  },

A
afc163 已提交
387 388 389 390 391 392 393 394 395 396 397
  getColumnKey(column, index) {
    return column.key || column.dataIndex || index;
  },

  isSortColumn(column) {
    if (!column || !this.state.sortColumn) {
      return false;
    }
    let colKey = this.getColumnKey(column);
    let isSortColumn = (this.getColumnKey(this.state.sortColumn) === colKey);
    return isSortColumn;
Y
yiminghe 已提交
398 399 400
  },

  renderColumnsDropdown(columns) {
Y
yiminghe 已提交
401
    return columns.map((column, i) => {
Y
yiminghe 已提交
402
      column = objectAssign({}, column);
A
afc163 已提交
403
      let key = this.getColumnKey(column, i);
A
afc163 已提交
404
      let filterDropdown, sortButton;
405
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
406
        let colFilters = this.state.filters[key] || [];
A
afc163 已提交
407 408 409
        filterDropdown =
          <FilterDropdown column={column}
                          selectedKeys={colFilters}
Y
yiminghe 已提交
410
                          confirmFilter={this.handleFilter}/>;
A
afc163 已提交
411 412
      }
      if (column.sorter) {
A
afc163 已提交
413
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
414 415
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
416 417 418
          if (this.state.sortOrder) {
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
419
        }
A
afc163 已提交
420 421
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
422
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
Y
yiminghe 已提交
423 424
                title="升序排序"
                onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
Y
yiminghe 已提交
425
            <Icon type="caret-up"/>
A
afc163 已提交
426 427
          </span>
          <span className={'ant-table-column-sorter-down ' +
428
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
Y
yiminghe 已提交
429 430
                title="降序排序"
                onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
Y
yiminghe 已提交
431
            <Icon type="caret-down"/>
A
afc163 已提交
432 433 434
          </span>
        </div>;
      }
A
afc163 已提交
435 436 437 438 439
      column.title = <div>
        {column.title}
        {sortButton}
        {filterDropdown}
      </div>;
A
afc163 已提交
440
      return column;
A
afc163 已提交
441 442
    });
  },
A
afc163 已提交
443

444 445 446 447
  handleShowSizeChange(current, pageSize) {
    let pagination = objectAssign(this.state.pagination, {
      pageSize: pageSize
    });
Y
yiminghe 已提交
448
    this.fetch({pagination});
449 450
  },

451 452
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
453 454
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
455
    }
A
afc163 已提交
456 457 458 459
    let classString = 'ant-table-pagination';
    if (this.props.size === 'small') {
      classString += ' mini';
    }
A
afc163 已提交
460 461
    let total = this.state.pagination.total;
    if (!total && this.isLocalDataSource()) {
Y
yiminghe 已提交
462 463
      total = this.getLocalData().length;
    }
A
afc163 已提交
464
    return (total > 0) ? <Pagination className={classString}
Y
yiminghe 已提交
465 466 467 468
                                     onChange={this.handlePageChange}
                                     total={total}
                                     pageSize={10}
                                     onShowSizeChange={this.handleShowSizeChange}
A
afc163 已提交
469
      {...this.state.pagination} /> : null;
A
afc163 已提交
470
  },
A
afc163 已提交
471

Y
yiminghe 已提交
472
  prepareParamsArguments(state) {
473 474 475
    // 准备筛选、排序、分页的参数
    let pagination;
    let filters = {};
A
afc163 已提交
476
    let sorter = {};
Y
yiminghe 已提交
477 478 479 480 481
    pagination = state.pagination;
    this.props.columns.forEach((column) => {
      let colFilters = state.filters[this.getColumnKey(column)] || [];
      if (colFilters.length > 0) {
        filters[this.getColumnKey(column)] = colFilters;
482 483
      }
    });
Y
yiminghe 已提交
484 485 486 487 488
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
A
afc163 已提交
489 490
    }
    return [pagination, filters, sorter];
491
  },
Y
yiminghe 已提交
492 493 494 495 496 497 498

  fetch(newState) {
    if (this.isLocalDataSource()) {
      if (newState) {
        this.setState(newState);
      }
    } else {
A
afc163 已提交
499 500 501 502 503
      // remote 模式使用 this.dataSource
      let dataSource = this.getRemoteDataSource();
      if (!dataSource) {
        return null;
      }
Y
yiminghe 已提交
504 505 506 507 508 509
      let state = objectAssign({}, this.state, newState);
      if (newState || !this.state.loading) {
        this.setState(objectAssign({
          loading: true
        }, newState));
      }
A
afc163 已提交
510
      let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
511
      return reqwest({
512
        url: dataSource.url,
513
        method: 'get',
A
afc163 已提交
514
        data: objectAssign(buildInParams, dataSource.data),
A
afc163 已提交
515
        headers: dataSource.headers,
516
        type: 'json',
A
afc163 已提交
517 518
        success: (result) => {
          if (this.isMounted()) {
A
afc163 已提交
519
            let pagination = objectAssign(
Y
yiminghe 已提交
520
              state.pagination,
A
afc163 已提交
521 522
              dataSource.getPagination.call(this, result)
            );
A
afc163 已提交
523
            this.setState({
A
afc163 已提交
524
              selectionDirty: false,
Y
yiminghe 已提交
525
              loading: false,
526
              data: dataSource.resolve.call(this, result),
Y
yiminghe 已提交
527
              pagination: pagination
A
afc163 已提交
528 529 530
            });
          }
        },
A
afc163 已提交
531
        error: () => {
A
afc163 已提交
532
          this.setState({
Y
yiminghe 已提交
533 534
            loading: false,
            data: []
A
afc163 已提交
535 536 537
          });
        }
      });
Y
yiminghe 已提交
538 539 540
    }
  },

A
afc163 已提交
541
  findColumn(myKey) {
Y
yiminghe 已提交
542 543 544 545 546
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
547 548
  getLocalDataPaging(dataSource) {
    let data = this.getLocalData(dataSource);
Y
yiminghe 已提交
549 550 551 552 553 554
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
555
    } else {
Y
yiminghe 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
      pageSize = state.pagination.pageSize;
      current = state.pagination.current;
    }
    // 分页
    // ---
    // 当数据量少于每页数量时,直接设置数据
    // 否则进行读取分页数据
    if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
      data = data.filter((item, i) => {
        if (i >= (current - 1) * pageSize &&
          i < current * pageSize) {
          return item;
        }
      });
    }
    return data;
  },

A
afc163 已提交
574
  getLocalData(dataSource) {
Y
yiminghe 已提交
575
    let state = this.state;
A
afc163 已提交
576
    let data = dataSource || this.state.dataSource;
Y
yiminghe 已提交
577 578 579 580 581 582 583 584 585
    // 排序
    if (state.sortOrder && state.sorter) {
      data = data.sort(state.sorter);
    }
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
        let values = state.filters[columnKey] || [];
A
afc163 已提交
586 587 588
        if (values.length === 0) {
          return;
        }
Y
yiminghe 已提交
589 590 591 592
        data = data.filter((record) => {
          return values.some((v)=> {
            return col.onFilter(v, record);
          });
593
        });
A
afc163 已提交
594
      });
A
afc163 已提交
595
    }
Y
yiminghe 已提交
596
    return data;
A
afc163 已提交
597
  },
Y
yiminghe 已提交
598

A
afc163 已提交
599
  componentDidMount() {
Y
yiminghe 已提交
600 601 602
    if (!this.isLocalDataSource()) {
      this.fetch();
    }
A
afc163 已提交
603
  },
604

Y
yiminghe 已提交
605 606 607
  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
Y
yiminghe 已提交
608
    let classString = this.props.className;
Z
zhujun24 已提交
609
    let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
610 611 612
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
613 614 615
    if (this.props.bordered) {
      classString += ' ant-table-bordered';
    }
Y
yiminghe 已提交
616
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
617 618 619 620
    columns = columns.map((column, i) => {
      column.key = column.dataIndex || i;
      return column;
    });
A
afc163 已提交
621
    let emptyText;
622
    let emptyClass = '';
A
afc163 已提交
623
    if (!data || data.length === 0) {
624
      emptyText = <div className="ant-table-placeholder">
Y
yiminghe 已提交
625
        <Icon type="frown"/>暂无数据
A
afc163 已提交
626
      </div>;
627
      emptyClass = ' ant-table-empty';
A
afc163 已提交
628
    }
A
afc163 已提交
629

A
afc163 已提交
630
    let table = <div>
631 632 633 634 635
      <Table {...this.props}
        data={data}
        columns={columns}
        className={classString}
        expandIconAsCell={expandIconAsCell} />
A
afc163 已提交
636 637
      {emptyText}
    </div>;
A
afc163 已提交
638
    if (this.state.loading) {
K
KgTong 已提交
639 640 641 642
      // if there is no pagination or no data, the height of spin should decrease by half of pagination
      let paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
              ? 'ant-table-with-pagination'
              : 'ant-table-without-pagination';
A
afc163 已提交
643
      let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
644
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
645
    }
646 647 648 649 650 651
    return (
      <div className={'clearfix' + emptyClass}>
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
652 653
  }
});
654 655 656

AntTable.DataSource = DataSource;

dqaria's avatar
dqaria 已提交
657
export default AntTable;