index.jsx 19.6 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';
A
afc163 已提交
8
import Icon from '../icon';
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

B
Benjy Cui 已提交
19 20 21 22 23 24
const defaultLocale = {
  filterTitle: '筛选',
  filterConfirm: '确定',
  filterReset: '重置'
};

25
class DataSource {
Y
yiminghe 已提交
26 27
  init(config) {
    this.config = config;
dqaria's avatar
dqaria 已提交
28
    this.url = config.url || '';
29 30 31
    this.resolve = config.resolve || defaultResolve;
    this.getParams = config.getParams || noop;
    this.getPagination = config.getPagination || noop;
A
afc163 已提交
32
    this.headers = config.headers || {};
A
afc163 已提交
33
    this.data = config.data || {};
Y
yiminghe 已提交
34 35 36 37 38 39 40 41
  }

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

42
  clone(config = {}) {
43
    return new DataSource(objectAssign({}, this.config, config));
44 45 46
  }
}

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

A
afc163 已提交
69 70
  getDefaultProps() {
    return {
A
afc163 已提交
71
      prefixCls: 'ant-table',
A
afc163 已提交
72
      useFixedHeader: false,
A
afc163 已提交
73
      rowSelection: null,
Y
yiminghe 已提交
74
      className: '',
75
      size: 'default',
A
afc163 已提交
76
      loading: false,
A
afc163 已提交
77
      bordered: false,
B
Benjy Cui 已提交
78 79
      onChange: noop,
      locale: {}
A
afc163 已提交
80 81
    };
  },
Y
yiminghe 已提交
82

A
afc163 已提交
83
  propTypes: {
A
afc163 已提交
84
    dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
A
afc163 已提交
85 86
  },

R
RaoHai 已提交
87 88 89 90 91 92
  getDefaultSelection() {
    let selectedRowKeys = [];
    if (this.props.rowSelection && this.props.rowSelection.getCheckboxProps) {
      let data = this.getCurrentPageData();
      data.filter((item) => {
        if (this.props.rowSelection.getCheckboxProps) {
93
          return this.props.rowSelection.getCheckboxProps(item).defaultChecked;
R
RaoHai 已提交
94 95 96 97 98 99 100 101 102
        }
        return true;
      }).map((record, rowIndex) => {
        selectedRowKeys.push(this.getRecordKey(record, rowIndex));
      });
    }
    return selectedRowKeys;
  },

A
afc163 已提交
103
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
104
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
105 106 107
      this.setState({
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
      });
A
afc163 已提交
108
    }
109 110
    // 外界只有 dataSource 的变化会触发新请求
    if ('dataSource' in nextProps &&
A
afc163 已提交
111 112 113 114 115 116 117 118 119
        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;
        });
      }
120
      this.setState({
A
afc163 已提交
121
        selectionDirty: false,
122 123
        dataSource: nextProps.dataSource,
        loading: true
124
      }, this.fetch);
Y
yiminghe 已提交
125 126
    }
    if (nextProps.columns !== this.props.columns) {
127 128 129
      this.setState({
        filters: {}
      });
A
afc163 已提交
130
    }
A
afc163 已提交
131 132 133 134 135
    if ('loading' in nextProps) {
      this.setState({
        loading: nextProps.loading
      });
    }
A
afc163 已提交
136
  },
A
afc163 已提交
137 138

  hasPagination(pagination) {
Y
yiminghe 已提交
139 140
    if (pagination === undefined) {
      pagination = this.props.pagination;
A
afc163 已提交
141
    }
Y
yiminghe 已提交
142 143
    return pagination !== false;
  },
A
afc163 已提交
144 145

  isLocalDataSource() {
146
    return Array.isArray(this.state.dataSource);
A
afc163 已提交
147
  },
A
afc163 已提交
148 149

  getRemoteDataSource() {
150
    return this.state.dataSource;
A
afc163 已提交
151
  },
A
afc163 已提交
152

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

Y
yiminghe 已提交
189 190 191 192
  handleFilter(column, filters) {
    filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: filters
    });
A
afc163 已提交
193
    const newState = {
Y
yiminghe 已提交
194
      selectedRowKeys: [],
A
afc163 已提交
195
      selectionDirty: false,
A
afc163 已提交
196 197 198 199
      filters
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
200
  },
A
afc163 已提交
201

Y
yiminghe 已提交
202
  handleSelect(record, rowIndex, e) {
A
afc163 已提交
203
    let checked = e.target.checked;
R
RaoHai 已提交
204
    let defaultSelection = [];
A
afc163 已提交
205
    if (!this.state.selectionDirty) {
R
RaoHai 已提交
206 207 208
      defaultSelection = this.getDefaultSelection();
    }
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
209
    let key = this.getRecordKey(record, rowIndex);
210
    if (checked) {
Y
yiminghe 已提交
211
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
212
    } else {
Y
yiminghe 已提交
213 214
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
215 216 217
      });
    }
    this.setState({
R
RaoHai 已提交
218
      selectedRowKeys: selectedRowKeys,
A
afc163 已提交
219
      selectionDirty: true
R
RaoHai 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
    });
    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 已提交
233
    if (!this.state.selectionDirty) {
R
RaoHai 已提交
234 235 236 237 238 239 240 241
      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 已提交
242
      selectionDirty: true
243 244
    });
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
245 246 247
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
248
      });
Y
yiminghe 已提交
249
      this.props.rowSelection.onSelect(record, checked, selectedRows);
250 251
    }
  },
A
afc163 已提交
252

A
afc163 已提交
253 254
  handleSelectAllRow(e) {
    let checked = e.target.checked;
Y
yiminghe 已提交
255
    let data = this.getCurrentPageData();
R
RaoHai 已提交
256 257 258 259 260 261
    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 已提交
262 263
      return this.getRecordKey(item, i);
    }) : [];
A
afc163 已提交
264
    this.setState({
R
RaoHai 已提交
265
      selectedRowKeys: selectedRowKeys,
A
afc163 已提交
266
      selectionDirty: true
267 268
    });
    if (this.props.rowSelection.onSelectAll) {
Y
yiminghe 已提交
269 270
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
271 272
      });
      this.props.rowSelection.onSelectAll(checked, selectedRows);
A
afc163 已提交
273
    }
A
afc163 已提交
274
  },
A
afc163 已提交
275

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

R
RaoHai 已提交
293 294 295
  onRadioChange: function (ev) {
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
296
    });
297
  },
A
afc163 已提交
298

R
RaoHai 已提交
299 300 301 302 303 304
  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 已提交
305 306 307 308 309 310 311
    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 已提交
312 313
    return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
                  value={record.key} checked={checked}/>;
R
RaoHai 已提交
314 315
  },

316
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
317
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
318 319 320 321 322 323 324
    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 已提交
325 326 327 328
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
Y
yiminghe 已提交
329 330
    return <Checkbox checked={checked} disabled={props.disabled}
                     onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
Y
yiminghe 已提交
331
  },
A
afc163 已提交
332 333

  getRecordKey(record, index) {
334 335 336
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
337
    return record.key || index;
338
  },
A
afc163 已提交
339

340
  renderRowSelection() {
Y
yiminghe 已提交
341
    let columns = this.props.columns.concat();
342
    if (this.props.rowSelection) {
Y
yiminghe 已提交
343 344 345 346 347
      let data = this.getCurrentPageData();
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
348
        data = data.filter((item) => {
R
RaoHai 已提交
349 350 351 352
          if (this.props.rowSelection.getCheckboxProps) {
            return !this.props.rowSelection.getCheckboxProps(item).disabled;
          }
          return true;
Y
yiminghe 已提交
353
        });
A
afc163 已提交
354 355 356 357 358 359
        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 已提交
360
      }
R
RaoHai 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      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'
        };
      }
379
      if (columns[0] &&
Y
yiminghe 已提交
380
        columns[0].key === 'selection-column') {
381 382 383 384 385 386 387
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
388

A
afc163 已提交
389
  getCurrentPageData() {
Y
yiminghe 已提交
390 391 392
    return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
  },

A
afc163 已提交
393 394 395 396 397 398 399 400 401 402 403
  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 已提交
404 405 406
  },

  renderColumnsDropdown(columns) {
B
Benjy Cui 已提交
407
    let locale = objectAssign({}, defaultLocale, this.props.locale);
Y
yiminghe 已提交
408
    return columns.map((column, i) => {
Y
yiminghe 已提交
409
      column = objectAssign({}, column);
A
afc163 已提交
410
      let key = this.getColumnKey(column, i);
A
afc163 已提交
411
      let filterDropdown, sortButton;
412
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
413
        let colFilters = this.state.filters[key] || [];
A
afc163 已提交
414
        filterDropdown =
B
Benjy Cui 已提交
415
          <FilterDropdown locale={locale} column={column}
A
afc163 已提交
416
                          selectedKeys={colFilters}
Y
yiminghe 已提交
417
                          confirmFilter={this.handleFilter}/>;
A
afc163 已提交
418 419
      }
      if (column.sorter) {
A
afc163 已提交
420
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
421 422
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
423 424 425
          if (this.state.sortOrder) {
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
426
        }
B
Benjy Cui 已提交
427

A
afc163 已提交
428 429
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
430
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
A
afc163 已提交
431
                title="↑"
Y
yiminghe 已提交
432
                onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
Y
yiminghe 已提交
433
            <Icon type="caret-up"/>
A
afc163 已提交
434 435
          </span>
          <span className={'ant-table-column-sorter-down ' +
436
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
A
afc163 已提交
437
                title="↓"
Y
yiminghe 已提交
438
                onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
Y
yiminghe 已提交
439
            <Icon type="caret-down"/>
A
afc163 已提交
440 441 442
          </span>
        </div>;
      }
A
afc163 已提交
443 444 445 446 447
      column.title = <div>
        {column.title}
        {sortButton}
        {filterDropdown}
      </div>;
A
afc163 已提交
448
      return column;
A
afc163 已提交
449 450
    });
  },
A
afc163 已提交
451

452 453 454 455
  handleShowSizeChange(current, pageSize) {
    let pagination = objectAssign(this.state.pagination, {
      pageSize: pageSize
    });
Y
yiminghe 已提交
456
    this.fetch({pagination});
457 458
  },

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

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

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

A
afc163 已提交
549
  findColumn(myKey) {
Y
yiminghe 已提交
550 551 552 553 554
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
555 556
  getLocalDataPaging(dataSource) {
    let data = this.getLocalData(dataSource);
Y
yiminghe 已提交
557 558 559 560 561 562
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
563
    } else {
Y
yiminghe 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
      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 已提交
582
  getLocalData(dataSource) {
Y
yiminghe 已提交
583
    let state = this.state;
A
afc163 已提交
584
    let data = dataSource || this.state.dataSource;
Y
yiminghe 已提交
585 586 587 588 589 590 591 592 593
    // 排序
    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 已提交
594 595 596
        if (values.length === 0) {
          return;
        }
Y
yiminghe 已提交
597 598 599 600
        data = data.filter((record) => {
          return values.some((v)=> {
            return col.onFilter(v, record);
          });
601
        });
A
afc163 已提交
602
      });
A
afc163 已提交
603
    }
Y
yiminghe 已提交
604
    return data;
A
afc163 已提交
605
  },
Y
yiminghe 已提交
606

A
afc163 已提交
607
  componentDidMount() {
Y
yiminghe 已提交
608 609 610
    if (!this.isLocalDataSource()) {
      this.fetch();
    }
A
afc163 已提交
611
  },
612

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

A
afc163 已提交
638
    let table = <div>
639 640 641 642 643
      <Table {...this.props}
        data={data}
        columns={columns}
        className={classString}
        expandIconAsCell={expandIconAsCell} />
A
afc163 已提交
644 645
      {emptyText}
    </div>;
A
afc163 已提交
646
    if (this.state.loading) {
K
KgTong 已提交
647 648 649 650
      // 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 已提交
651
      let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
652
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
653
    }
654 655 656 657 658 659
    return (
      <div className={'clearfix' + emptyClass}>
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
660 661
  }
});
662 663 664

AntTable.DataSource = DataSource;

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