index.jsx 20.0 KB
Newer Older
A
afc163 已提交
1
import React from 'react';
2
import RcTable from 'rc-table';
A
afc163 已提交
3
import Checkbox from '../checkbox';
R
RaoHai 已提交
4
import Radio from '../radio';
A
afc163 已提交
5
import FilterDropdown from './filterDropdown';
6
import Pagination from '../pagination';
A
afc163 已提交
7
import Icon from '../icon';
K
KgTong 已提交
8
import Spin from '../spin';
A
afc163 已提交
9
import classNames from 'classnames';
A
afc163 已提交
10
import { flatArray } from './util';
A
afc163 已提交
11

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

B
Benjy Cui 已提交
15 16 17
const defaultLocale = {
  filterTitle: '筛选',
  filterConfirm: '确定',
A
afc163 已提交
18 19
  filterReset: '重置',
  emptyText: '暂无数据',
B
Benjy Cui 已提交
20 21
};

22 23 24 25 26 27 28
const defaultPagination = {
  pageSize: 10,
  current: 1,
  onChange: noop,
  onShowSizeChange: noop,
};

A
afc163 已提交
29
const Table = React.createClass({
Y
yiminghe 已提交
30
  getInitialState() {
A
afc163 已提交
31
    return {
Y
yiminghe 已提交
32
      // 减少状态
33
      selectedRowKeys: this.props.selectedRowKeys || [],
Y
yiminghe 已提交
34
      filters: {},
A
afc163 已提交
35
      selectionDirty: false,
A
afc163 已提交
36
      ...this.getSortStateFromColumns(),
R
RaoHai 已提交
37
      radioIndex: null,
38
      pagination: this.hasPagination() ?
B
Benjy Cui 已提交
39 40 41 42 43
      {
        size: this.props.size,
        ...defaultPagination,
        ...this.props.pagination,
      } : {},
A
afc163 已提交
44 45
    };
  },
Y
yiminghe 已提交
46

A
afc163 已提交
47 48
  getDefaultProps() {
    return {
A
afc163 已提交
49
      dataSource: [],
A
afc163 已提交
50
      prefixCls: 'ant-table',
A
afc163 已提交
51
      useFixedHeader: false,
A
afc163 已提交
52
      rowSelection: null,
Y
yiminghe 已提交
53
      className: '',
A
afc163 已提交
54
      size: 'large',
A
afc163 已提交
55
      loading: false,
A
afc163 已提交
56
      bordered: false,
A
afc163 已提交
57
      indentSize: 20,
B
Benjy Cui 已提交
58 59
      onChange: noop,
      locale: {}
A
afc163 已提交
60 61
    };
  },
Y
yiminghe 已提交
62

A
afc163 已提交
63
  propTypes: {
A
afc163 已提交
64
    dataSource: React.PropTypes.array,
A
afc163 已提交
65 66 67 68 69 70 71 72 73
    prefixCls: React.PropTypes.string,
    useFixedHeader: React.PropTypes.bool,
    rowSelection: React.PropTypes.object,
    className: React.PropTypes.string,
    size: React.PropTypes.string,
    loading: React.PropTypes.bool,
    bordered: React.PropTypes.bool,
    onChange: React.PropTypes.func,
    locale: React.PropTypes.object,
A
afc163 已提交
74 75
  },

A
afc163 已提交
76
  contextTypes: {
A
afc163 已提交
77
    antLocale: React.PropTypes.object,
A
afc163 已提交
78 79
  },

R
RaoHai 已提交
80
  getDefaultSelection() {
81 82
    if (!this.props.rowSelection || !this.props.rowSelection.getCheckboxProps) {
      return [];
R
RaoHai 已提交
83
    }
A
afc163 已提交
84
    return this.getFlatCurrentPageData()
85 86
      .filter(item => this.props.rowSelection.getCheckboxProps(item).defaultChecked)
      .map((record, rowIndex) => this.getRecordKey(record, rowIndex));
R
RaoHai 已提交
87 88
  },

A
afc163 已提交
89 90
  getLocale() {
    let locale = {};
A
afc163 已提交
91 92
    if (this.context.antLocale && this.context.antLocale.Table) {
      locale = this.context.antLocale.Table;
A
afc163 已提交
93
    }
B
Benjy Cui 已提交
94
    return { ...defaultLocale, ...locale, ...this.props.locale };
A
afc163 已提交
95 96
  },

A
afc163 已提交
97
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
98
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
99
      this.setState({
B
Benjy Cui 已提交
100
        pagination: { ...defaultPagination, ...this.state.pagination, ...nextProps.pagination },
101
      });
A
afc163 已提交
102
    }
103
    // dataSource 的变化会清空选中项
104
    if ('dataSource' in nextProps &&
A
afc163 已提交
105
        nextProps.dataSource !== this.props.dataSource) {
106
      this.setState({
A
afc163 已提交
107
        selectionDirty: false,
A
afc163 已提交
108
      });
109 110 111 112 113 114 115
    }
    if (nextProps.rowSelection &&
        'selectedRowKeys' in nextProps.rowSelection) {
      this.setState({
        selectedRowKeys: nextProps.rowSelection.selectedRowKeys || [],
      });
    }
A
afc163 已提交
116
    if ('columns' in nextProps) {
A
afc163 已提交
117 118 119 120
      const sortState = this.getSortStateFromColumns(nextProps.columns);
      if (sortState && (
        sortState.sortColumn !== this.state.sortColumn ||
        sortState.sortOrder !== this.state.sortOrder)) {
A
afc163 已提交
121 122 123
        this.setState(sortState);
      }
    }
124 125 126 127 128 129 130 131
  },

  setSelectedRowKeys(selectedRowKeys) {
    if (this.props.rowSelection &&
        !('selectedRowKeys' in this.props.rowSelection)) {
      this.setState({ selectedRowKeys });
    }
    if (this.props.rowSelection && this.props.rowSelection.onChange) {
A
afc163 已提交
132
      const data = this.getFlatCurrentPageData();
133 134 135
      const selectedRows = data.filter(
        (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0
      );
A
afc163 已提交
136
      this.props.rowSelection.onChange(selectedRowKeys, selectedRows);
A
afc163 已提交
137
    }
A
afc163 已提交
138
  },
A
afc163 已提交
139

A
afc163 已提交
140
  hasPagination() {
A
afc163 已提交
141
    return this.props.pagination !== false;
A
afc163 已提交
142
  },
A
afc163 已提交
143

A
afc163 已提交
144 145 146 147
  getSortedColumn(columns) {
    return (columns || this.props.columns).filter(col => col.sorted)[0];
  },

A
afc163 已提交
148
  getSortStateFromColumns(columns) {
A
afc163 已提交
149 150 151 152 153 154 155
    const sortedColumn = this.getSortedColumn(columns);
    if (sortedColumn) {
      return {
        sortColumn: sortedColumn,
        sortOrder: sortedColumn.sorted,
      };
    }
A
afc163 已提交
156 157 158 159
    return {
      sortColumn: null,
      sortOrder: null,
    };
A
afc163 已提交
160 161
  },

A
afc163 已提交
162 163
  getSorterFn() {
    const { sortOrder, sortColumn } = this.state;
A
afc163 已提交
164 165
    if (!sortOrder || !sortColumn ||
        typeof sortColumn.sorter !== 'function') {
A
afc163 已提交
166 167 168 169 170 171 172 173 174 175 176
      return () => {};
    }
    return (a, b) => {
      let result = sortColumn.sorter(a, b);
      if (result !== 0) {
        return (sortOrder === 'descend') ? -result : result;
      }
      return a.index - b.index;
    };
  },

A
afc163 已提交
177
  toggleSortOrder(order, column) {
A
afc163 已提交
178
    let { sortColumn, sortOrder } = this.state;
A
afc163 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
A
afc163 已提交
192 193 194 195
    const newState = {
      sortOrder,
      sortColumn,
    };
A
afc163 已提交
196 197 198 199
    // Controlled
    if (!this.getSortStateFromColumns()) {
      this.setState(newState);
    }
A
afc163 已提交
200
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
201
  },
A
afc163 已提交
202

203
  handleFilter(column, nextFilters) {
B
Benjy Cui 已提交
204 205
    const filters = {
      ...this.state.filters,
206
      [this.getColumnKey(column)]: nextFilters
B
Benjy Cui 已提交
207
    };
208 209 210 211 212 213 214
    // 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 已提交
215
    const newState = {
A
afc163 已提交
216
      selectionDirty: false,
A
afc163 已提交
217
      filters,
A
afc163 已提交
218
    };
A
afc163 已提交
219
    this.setState(newState);
220
    this.setSelectedRowKeys([]);
A
afc163 已提交
221
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
222
  },
A
afc163 已提交
223

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

249
  handleRadioSelect(record, rowIndex, e) {
250 251
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
252 253 254 255
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
256
      radioIndex: key,
257
      selectionDirty: true,
258
    });
259
    this.setSelectedRowKeys(selectedRowKeys);
260
    if (this.props.rowSelection.onSelect) {
A
afc163 已提交
261
      let data = this.getFlatCurrentPageData();
Y
yiminghe 已提交
262 263
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
264
      });
Y
yiminghe 已提交
265
      this.props.rowSelection.onSelect(record, checked, selectedRows);
266 267
    }
  },
A
afc163 已提交
268

A
afc163 已提交
269
  handleSelectAllRow(e) {
270
    const checked = e.target.checked;
A
afc163 已提交
271
    const data = this.getFlatCurrentPageData();
272 273 274 275 276 277
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
    const selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    const changableRowKeys = data.filter(item =>
      !this.props.rowSelection.getCheckboxProps ||
      !this.props.rowSelection.getCheckboxProps(item).disabled
    ).map((item, i) => this.getRecordKey(item, i));
278 279 280

    // 记录变化的列
    const changeRowKeys = [];
281 282 283 284
    if (checked) {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) < 0) {
          selectedRowKeys.push(key);
285
          changeRowKeys.push(key);
286 287 288 289 290 291
        }
      });
    } else {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) >= 0) {
          selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
292
          changeRowKeys.push(key);
293 294 295
        }
      });
    }
A
afc163 已提交
296
    this.setState({
297
      selectionDirty: true,
298
    });
299
    this.setSelectedRowKeys(selectedRowKeys);
300
    if (this.props.rowSelection.onSelectAll) {
301 302 303 304 305
      const selectedRows = data.filter((row, i) =>
        selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0);
      const changeRows = data.filter((row, i) =>
        changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0);
      this.props.rowSelection.onSelectAll(checked, selectedRows, changeRows);
A
afc163 已提交
306
    }
A
afc163 已提交
307
  },
A
afc163 已提交
308

309
  handlePageChange(current) {
B
Benjy Cui 已提交
310
    let pagination = { ...this.state.pagination };
311 312 313 314 315
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
316 317
    pagination.onChange(pagination.current);

A
afc163 已提交
318
    const newState = {
A
afc163 已提交
319
      selectionDirty: false,
A
afc163 已提交
320 321
      pagination
    };
A
afc163 已提交
322
    this.setState(newState);
A
afc163 已提交
323
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
324
  },
A
afc163 已提交
325

326
  onRadioChange(ev) {
R
RaoHai 已提交
327 328
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
329
    });
330
  },
A
afc163 已提交
331

R
RaoHai 已提交
332 333 334 335 336 337
  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 已提交
338 339
    let checked;
    if (this.state.selectionDirty) {
340
      checked = this.state.radioIndex === rowIndex;
A
afc163 已提交
341
    } else {
342
      checked = (this.state.radioIndex === rowIndex ||
A
afc163 已提交
343 344
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
345
    return (
346 347
      <Radio disabled={props.disabled}
        onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
348
        value={rowIndex} checked={checked} />
349
    );
R
RaoHai 已提交
350 351
  },

352
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
353
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
354 355 356 357 358 359 360
    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 已提交
361 362 363 364
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
365 366
    return (
      <Checkbox checked={checked} disabled={props.disabled}
367
        onChange={this.handleSelect.bind(this, record, rowIndex)} />
368
    );
Y
yiminghe 已提交
369
  },
A
afc163 已提交
370 371

  getRecordKey(record, index) {
372 373 374
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
375
    return record.key || index;
376
  },
A
afc163 已提交
377

378
  renderRowSelection() {
Y
yiminghe 已提交
379
    let columns = this.props.columns.concat();
380
    if (this.props.rowSelection) {
A
afc163 已提交
381
      let data = this.getFlatCurrentPageData().filter((item) => {
382 383 384 385 386
        if (this.props.rowSelection.getCheckboxProps) {
          return !this.props.rowSelection.getCheckboxProps(item).disabled;
        }
        return true;
      });
Y
yiminghe 已提交
387 388 389 390
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
391 392 393
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
A
afc163 已提交
394 395 396 397
          : (
            data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
            data.every((item) =>
A
afc163 已提交
398
              this.props.rowSelection.getCheckboxProps &&
A
afc163 已提交
399 400
              this.props.rowSelection.getCheckboxProps(item).defaultChecked)
          );
Y
yiminghe 已提交
401
      }
R
RaoHai 已提交
402 403 404 405 406 407 408 409
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
410 411 412 413 414
        const checkboxAllDisabled = data.every(item =>
          this.props.rowSelection.getCheckboxProps &&
          this.props.rowSelection.getCheckboxProps(item).disabled);
        const checkboxAll = (
            <Checkbox checked={checked}
415 416
              disabled={checkboxAllDisabled}
              onChange={this.handleSelectAllRow} />
417
        );
R
RaoHai 已提交
418 419 420 421 422 423 424
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
A
afc163 已提交
425
      if (columns[0] && columns[0].key === 'selection-column') {
426 427 428 429 430 431 432
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
433

A
afc163 已提交
434 435 436 437 438
  getColumnKey(column, index) {
    return column.key || column.dataIndex || index;
  },

  isSortColumn(column) {
A
afc163 已提交
439 440
    const { sortColumn } = this.state;
    if (!column || !sortColumn) {
A
afc163 已提交
441 442
      return false;
    }
A
afc163 已提交
443
    return this.getColumnKey(sortColumn) === this.getColumnKey(column);
Y
yiminghe 已提交
444 445 446
  },

  renderColumnsDropdown(columns) {
A
afc163 已提交
447
    const { sortOrder } = this.state;
A
afc163 已提交
448
    const locale = this.getLocale();
449
    return columns.map((originColumn, i) => {
B
Benjy Cui 已提交
450
      let column = { ...originColumn };
A
afc163 已提交
451
      let key = this.getColumnKey(column, i);
452 453
      let filterDropdown;
      let sortButton;
454
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
455
        let colFilters = this.state.filters[key] || [];
456
        filterDropdown = (
B
Benjy Cui 已提交
457
          <FilterDropdown locale={locale} column={column}
458
            selectedKeys={colFilters}
459
            confirmFilter={this.handleFilter} />
460
        );
A
afc163 已提交
461 462
      }
      if (column.sorter) {
A
afc163 已提交
463
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
464 465
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
466
          if (sortOrder) {
A
afc163 已提交
467 468
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
469
        }
A
afc163 已提交
470 471
        const isAscend = isSortColumn && sortOrder === 'ascend';
        const isDescend = isSortColumn && sortOrder === 'descend';
472 473
        sortButton = (
          <div className="ant-table-column-sorter">
474
            <span className={`ant-table-column-sorter-up ${isAscend ? 'on' : 'off'}`}
475 476
              title="↑"
              onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
477
              <Icon type="caret-up" />
478
            </span>
479
            <span className={`ant-table-column-sorter-down ${isDescend ? 'on' : 'off'}`}
480 481
              title="↓"
              onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
482
              <Icon type="caret-down" />
483 484 485
            </span>
          </div>
        );
A
afc163 已提交
486
      }
487
      column.title = (
488
        <span>
489 490 491
          {column.title}
          {sortButton}
          {filterDropdown}
492
        </span>
493
      );
A
afc163 已提交
494
      return column;
A
afc163 已提交
495 496
    });
  },
A
afc163 已提交
497

498
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
499
    const pagination = this.state.pagination;
500
    pagination.onShowSizeChange(current, pageSize);
501
    const nextPagination = { ...pagination, pageSize, current };
B
Benjy Cui 已提交
502
    this.setState({ pagination: nextPagination });
A
afc163 已提交
503
    this.props.onChange(...this.prepareParamsArguments({
504 505 506
      ...this.state,
      pagination: nextPagination,
    }));
507 508
  },

509 510
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
511 512
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
513
    }
A
afc163 已提交
514 515
    let classString = classNames({
      'ant-table-pagination': true,
516
      mini: this.props.size === 'middle' || this.props.size === 'small',
A
afc163 已提交
517
    });
A
afc163 已提交
518
    let total = this.state.pagination.total || this.getLocalData().length;
Y
yiminghe 已提交
519
    const pageSize = this.state.pagination.pageSize;
A
afc163 已提交
520
    return (total > 0) ?
B
Benjy Cui 已提交
521
      <Pagination {...this.state.pagination}
522 523 524 525 526
        className={classString}
        onChange={this.handlePageChange}
        total={total}
        pageSize={pageSize}
        onShowSizeChange={this.handleShowSizeChange} /> : null;
A
afc163 已提交
527
  },
A
afc163 已提交
528

Y
yiminghe 已提交
529
  prepareParamsArguments(state) {
530
    // 准备筛选、排序、分页的参数
531 532 533
    const pagination = state.pagination;
    const filters = state.filters;
    const sorter = {};
A
afc163 已提交
534 535 536 537
    if (state.sortColumn && state.sortOrder) {
      sorter.column = state.sortColumn;
      sorter.order = state.sortOrder;
      sorter.field = state.sortColumn.dataIndex;
A
afc163 已提交
538
      sorter.columnKey = this.getColumnKey(state.sortColumn);
A
afc163 已提交
539 540
    }
    return [pagination, filters, sorter];
541
  },
Y
yiminghe 已提交
542

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

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

A
afc163 已提交
572 573 574 575 576
  getFlatCurrentPageData() {
    return flatArray(this.getCurrentPageData());
  },

  getLocalData() {
A
afc163 已提交
577
    const state = this.state;
A
afc163 已提交
578
    let data = this.props.dataSource || [];
Y
yiminghe 已提交
579
    // 排序
A
afc163 已提交
580 581 582
    data = data.slice(0);
    for (let i = 0; i < data.length; i++) {
      data[i].index = i;
Y
yiminghe 已提交
583
    }
A
afc163 已提交
584
    data = data.sort(this.getSorterFn());
Y
yiminghe 已提交
585 586 587 588
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
589 590 591
        if (!col) {
          return;
        }
Y
yiminghe 已提交
592
        let values = state.filters[columnKey] || [];
A
afc163 已提交
593 594 595
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
596 597 598
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
599
      });
A
afc163 已提交
600
    }
Y
yiminghe 已提交
601
    return data;
A
afc163 已提交
602
  },
Y
yiminghe 已提交
603 604

  render() {
A
afc163 已提交
605
    const data = this.getCurrentPageData();
Y
yiminghe 已提交
606
    let columns = this.renderRowSelection();
A
afc163 已提交
607
    const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
608
    const locale = this.getLocale();
A
afc163 已提交
609

A
afc163 已提交
610
    const classString = classNames({
A
afc163 已提交
611 612 613 614 615
      [`ant-table-${this.props.size}`]: true,
      'ant-table-bordered': this.props.bordered,
      [this.props.className]: !!this.props.className,
    });

Y
yiminghe 已提交
616
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
617
    columns = columns.map((column, i) => {
B
Benjy Cui 已提交
618
      const newColumn = { ...column };
619 620
      newColumn.key = newColumn.key || newColumn.dataIndex || i;
      return newColumn;
A
afc163 已提交
621
    });
A
afc163 已提交
622
    let emptyText;
623
    let emptyClass = '';
A
afc163 已提交
624
    if (!data || data.length === 0) {
625 626
      emptyText = (
        <div className="ant-table-placeholder">
627
          <Icon type="frown" />{locale.emptyText}
628 629
        </div>
      );
630
      emptyClass = ' ant-table-empty';
A
afc163 已提交
631
    }
A
afc163 已提交
632

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

662
export default Table;