index.jsx 19.7 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
import classNames from 'classnames';
A
afc163 已提交
12

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

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

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

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

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

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

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

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

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

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

A
afc163 已提交
104
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
105
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
106 107 108
      this.setState({
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
      });
A
afc163 已提交
109
    }
110 111
    // 外界只有 dataSource 的变化会触发新请求
    if ('dataSource' in nextProps &&
A
afc163 已提交
112 113 114 115
        nextProps.dataSource !== this.props.dataSource) {
      let selectedRowKeys = this.state.selectedRowKeys;
      // 把不在当前页的选中项去掉
      if (this.isLocalDataSource()) {
A
afc163 已提交
116 117 118 119
        let currentPageRowKeys =
          this.getLocalDataPaging(nextProps.dataSource).map(
            (record, i) => this.getRecordKey(record, i)
          );
A
afc163 已提交
120 121 122 123
        selectedRowKeys = selectedRowKeys.filter((key) => {
          return currentPageRowKeys.indexOf(key) >= 0;
        });
      }
124
      this.setState({
A
afc163 已提交
125
        selectionDirty: false,
A
afc163 已提交
126
        selectedRowKeys,
127 128
        dataSource: nextProps.dataSource,
        loading: true
129
      }, this.fetch);
Y
yiminghe 已提交
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
    });
193 194 195 196 197 198 199
    // Remove filters not in current columns
    const currentColumnKeys = this.props.columns.map(c => this.getColumnKey(c));
    Object.keys(filters).forEach((columnKey) => {
      if (currentColumnKeys.indexOf(columnKey) < 0) {
        delete filters[columnKey];
      }
    });
A
afc163 已提交
200
    const newState = {
Y
yiminghe 已提交
201
      selectedRowKeys: [],
A
afc163 已提交
202
      selectionDirty: false,
A
afc163 已提交
203 204 205 206
      filters
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
207
  },
A
afc163 已提交
208

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

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

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

R
RaoHai 已提交
300 301 302
  onRadioChange: function (ev) {
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
303
    });
304
  },
A
afc163 已提交
305

R
RaoHai 已提交
306 307 308 309 310 311
  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 已提交
312 313 314 315 316 317 318
    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 已提交
319 320
    return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
                  value={record.key} checked={checked}/>;
R
RaoHai 已提交
321 322
  },

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

  getRecordKey(record, index) {
341 342 343
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
344
    return record.key || index;
345
  },
A
afc163 已提交
346

347
  renderRowSelection() {
Y
yiminghe 已提交
348
    let columns = this.props.columns.concat();
349
    if (this.props.rowSelection) {
Y
yiminghe 已提交
350 351 352 353 354
      let data = this.getCurrentPageData();
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
355
        data = data.filter((item) => {
R
RaoHai 已提交
356 357 358 359
          if (this.props.rowSelection.getCheckboxProps) {
            return !this.props.rowSelection.getCheckboxProps(item).disabled;
          }
          return true;
Y
yiminghe 已提交
360
        });
A
afc163 已提交
361 362 363 364 365 366
        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 已提交
367
      }
R
RaoHai 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
        let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow}/>;
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
384
      if (columns[0] &&
Y
yiminghe 已提交
385
        columns[0].key === 'selection-column') {
386 387 388 389 390 391 392
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
393

A
afc163 已提交
394
  getCurrentPageData() {
Y
yiminghe 已提交
395 396 397
    return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
  },

A
afc163 已提交
398 399 400 401 402 403 404 405 406 407 408
  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 已提交
409 410 411
  },

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

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

457 458 459 460
  handleShowSizeChange(current, pageSize) {
    let pagination = objectAssign(this.state.pagination, {
      pageSize: pageSize
    });
Y
yiminghe 已提交
461
    this.fetch({pagination});
462 463
  },

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

Y
yiminghe 已提交
485
  prepareParamsArguments(state) {
486
    // 准备筛选、排序、分页的参数
487 488 489
    const pagination = state.pagination;
    const filters = state.filters;
    const sorter = {};
Y
yiminghe 已提交
490 491 492 493 494
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
A
afc163 已提交
495 496
    }
    return [pagination, filters, sorter];
497
  },
Y
yiminghe 已提交
498 499 500 501 502 503 504

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

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

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

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

Y
yiminghe 已提交
614 615 616
  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
Z
zhujun24 已提交
617
    let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
618 619 620 621 622 623 624

    let classString = classNames({
      [`ant-table-${this.props.size}`]: true,
      'ant-table-bordered': this.props.bordered,
      [this.props.className]: !!this.props.className,
    });

Y
yiminghe 已提交
625
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
626
    columns = columns.map((column, i) => {
A
afc163 已提交
627
      column.key = column.key || column.dataIndex || i;
A
afc163 已提交
628 629
      return column;
    });
A
afc163 已提交
630
    let emptyText;
631
    let emptyClass = '';
A
afc163 已提交
632
    if (!data || data.length === 0) {
633
      emptyText = <div className="ant-table-placeholder">
Y
yiminghe 已提交
634
        <Icon type="frown"/>暂无数据
A
afc163 已提交
635
      </div>;
636
      emptyClass = ' ant-table-empty';
A
afc163 已提交
637
    }
A
afc163 已提交
638

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

AntTable.DataSource = DataSource;

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