index.jsx 21.7 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 || [],
A
afc163 已提交
34
      filters: this.getFiltersFromColumns(),
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 117

    if (this.getSortOrderColumns(nextProps.columns).length > 0) {
A
afc163 已提交
118
      const sortState = this.getSortStateFromColumns(nextProps.columns);
A
afc163 已提交
119 120
      if (sortState.sortColumn !== this.state.sortColumn ||
          sortState.sortOrder !== this.state.sortOrder) {
A
afc163 已提交
121 122 123
        this.setState(sortState);
      }
    }
A
afc163 已提交
124 125 126 127 128 129 130 131 132

    const filteredValueColumns = this.getFilteredValueColumns(nextProps.columns);
    if (filteredValueColumns.length > 0) {
      const filtersFromColumns = this.getFiltersFromColumns(nextProps.columns);
      const newFilters = { ...this.state.filters };
      Object.keys(filtersFromColumns).forEach(key => {
        newFilters[key] = filtersFromColumns[key];
      });
      this.setState({ filters: newFilters });
A
afc163 已提交
133
    }
134 135 136 137 138 139 140 141
  },

  setSelectedRowKeys(selectedRowKeys) {
    if (this.props.rowSelection &&
        !('selectedRowKeys' in this.props.rowSelection)) {
      this.setState({ selectedRowKeys });
    }
    if (this.props.rowSelection && this.props.rowSelection.onChange) {
A
afc163 已提交
142
      const data = this.getFlatCurrentPageData();
143 144 145
      const selectedRows = data.filter(
        (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0
      );
A
afc163 已提交
146
      this.props.rowSelection.onChange(selectedRowKeys, selectedRows);
A
afc163 已提交
147
    }
A
afc163 已提交
148
  },
A
afc163 已提交
149

A
afc163 已提交
150
  hasPagination() {
A
afc163 已提交
151
    return this.props.pagination !== false;
A
afc163 已提交
152
  },
A
afc163 已提交
153

A
afc163 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  isFiltersChanged(filters) {
    let filtersChanged = false;
    if (Object.keys(filters).length !== Object.keys(this.state.filters).length) {
      filtersChanged = true;
    } else {
      Object.keys(filters).forEach(columnKey => {
        if (filters[columnKey] !== this.state.filters[columnKey]) {
          filtersChanged = true;
        }
      });
    }
    return filtersChanged;
  },

A
afc163 已提交
168 169
  getSortOrderColumns(columns) {
    return (columns || this.props.columns || []).filter(column => 'sortOrder' in column);
A
afc163 已提交
170 171
  },

A
afc163 已提交
172 173
  getFilteredValueColumns(columns) {
    return (columns || this.props.columns || []).filter(column => 'filteredValue' in column);
A
afc163 已提交
174 175 176 177
  },

  getFiltersFromColumns(columns) {
    let filters = {};
A
afc163 已提交
178 179
    this.getFilteredValueColumns(columns).forEach(col => {
      filters[this.getColumnKey(col)] = col.filteredValue;
A
afc163 已提交
180 181 182 183
    });
    return filters;
  },

A
afc163 已提交
184
  getSortStateFromColumns(columns) {
A
afc163 已提交
185 186 187
    // return fisrt column which sortOrder is not falsy
    const sortedColumn =
      this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0];
A
afc163 已提交
188 189 190
    if (sortedColumn) {
      return {
        sortColumn: sortedColumn,
A
afc163 已提交
191
        sortOrder: sortedColumn.sortOrder,
A
afc163 已提交
192 193
      };
    }
A
afc163 已提交
194 195 196 197
    return {
      sortColumn: null,
      sortOrder: null,
    };
A
afc163 已提交
198 199
  },

A
afc163 已提交
200 201
  getSorterFn() {
    const { sortOrder, sortColumn } = this.state;
A
afc163 已提交
202 203
    if (!sortOrder || !sortColumn ||
        typeof sortColumn.sorter !== 'function') {
A
afc163 已提交
204 205 206 207 208 209 210 211 212 213 214
      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 已提交
215
  toggleSortOrder(order, column) {
A
afc163 已提交
216
    let { sortColumn, sortOrder } = this.state;
A
afc163 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
A
afc163 已提交
230 231 232 233
    const newState = {
      sortOrder,
      sortColumn,
    };
A
afc163 已提交
234

A
afc163 已提交
235
    // Controlled
A
afc163 已提交
236
    if (this.getSortOrderColumns() === 0) {
A
afc163 已提交
237 238
      this.setState(newState);
    }
A
afc163 已提交
239

A
afc163 已提交
240
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
241
  },
A
afc163 已提交
242

243
  handleFilter(column, nextFilters) {
B
Benjy Cui 已提交
244 245
    const filters = {
      ...this.state.filters,
A
afc163 已提交
246
      [this.getColumnKey(column)]: nextFilters,
B
Benjy Cui 已提交
247
    };
248 249 250 251 252 253 254
    // 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 已提交
255

A
afc163 已提交
256
    const newState = {
A
afc163 已提交
257
      selectionDirty: false,
A
afc163 已提交
258
    };
A
afc163 已提交
259 260 261 262 263 264 265 266 267 268
    const filtersToSetState = { ...filters };
    // Remove filters which is controlled
    this.getFilteredValueColumns().forEach(col => {
      const columnKey = this.getColumnKey(col);
      if (columnKey) {
        delete filtersToSetState[columnKey];
      }
    });
    if (Object.keys(filtersToSetState).length > 0) {
      newState.filters = filtersToSetState;
A
afc163 已提交
269
    }
A
afc163 已提交
270 271 272 273 274
    this.setState(newState, () => {
      this.props.onChange(...this.prepareParamsArguments({
        ...this.state, selectionDirty: false, filters,
      }));
    });
A
afc163 已提交
275
  },
A
afc163 已提交
276

Y
yiminghe 已提交
277
  handleSelect(record, rowIndex, e) {
278 279
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
280
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
281
    let key = this.getRecordKey(record, rowIndex);
282
    if (checked) {
Y
yiminghe 已提交
283
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
284
    } else {
Y
yiminghe 已提交
285 286
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
287 288 289
      });
    }
    this.setState({
290
      selectionDirty: true,
R
RaoHai 已提交
291
    });
292
    this.setSelectedRowKeys(selectedRowKeys);
R
RaoHai 已提交
293
    if (this.props.rowSelection.onSelect) {
A
afc163 已提交
294
      let data = this.getFlatCurrentPageData();
R
RaoHai 已提交
295 296 297 298 299 300 301
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
      });
      this.props.rowSelection.onSelect(record, checked, selectedRows);
    }
  },

302
  handleRadioSelect(record, rowIndex, e) {
303 304
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
305 306 307 308
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
309
      radioIndex: key,
310
      selectionDirty: true,
311
    });
312
    this.setSelectedRowKeys(selectedRowKeys);
313
    if (this.props.rowSelection.onSelect) {
A
afc163 已提交
314
      let data = this.getFlatCurrentPageData();
Y
yiminghe 已提交
315 316
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
317
      });
Y
yiminghe 已提交
318
      this.props.rowSelection.onSelect(record, checked, selectedRows);
319 320
    }
  },
A
afc163 已提交
321

A
afc163 已提交
322
  handleSelectAllRow(e) {
323
    const checked = e.target.checked;
A
afc163 已提交
324
    const data = this.getFlatCurrentPageData();
325 326 327 328 329 330
    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));
331 332 333

    // 记录变化的列
    const changeRowKeys = [];
334 335 336 337
    if (checked) {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) < 0) {
          selectedRowKeys.push(key);
338
          changeRowKeys.push(key);
339 340 341 342 343 344
        }
      });
    } else {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) >= 0) {
          selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
345
          changeRowKeys.push(key);
346 347 348
        }
      });
    }
A
afc163 已提交
349
    this.setState({
350
      selectionDirty: true,
351
    });
352
    this.setSelectedRowKeys(selectedRowKeys);
353
    if (this.props.rowSelection.onSelectAll) {
354 355 356 357 358
      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 已提交
359
    }
A
afc163 已提交
360
  },
A
afc163 已提交
361

362
  handlePageChange(current) {
B
Benjy Cui 已提交
363
    let pagination = { ...this.state.pagination };
364 365 366 367 368
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
369 370
    pagination.onChange(pagination.current);

A
afc163 已提交
371
    const newState = {
A
afc163 已提交
372
      selectionDirty: false,
A
afc163 已提交
373 374
      pagination
    };
A
afc163 已提交
375
    this.setState(newState);
A
afc163 已提交
376
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
377
  },
A
afc163 已提交
378

379
  onRadioChange(ev) {
R
RaoHai 已提交
380 381
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
382
    });
383
  },
A
afc163 已提交
384

R
RaoHai 已提交
385 386 387 388 389 390
  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 已提交
391 392
    let checked;
    if (this.state.selectionDirty) {
393
      checked = this.state.radioIndex === rowIndex;
A
afc163 已提交
394
    } else {
395
      checked = (this.state.radioIndex === rowIndex ||
A
afc163 已提交
396 397
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
398
    return (
399 400
      <Radio disabled={props.disabled}
        onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
401
        value={rowIndex} checked={checked} />
402
    );
R
RaoHai 已提交
403 404
  },

405
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
406
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
407 408 409 410 411 412 413
    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 已提交
414 415 416 417
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
418 419
    return (
      <Checkbox checked={checked} disabled={props.disabled}
420
        onChange={this.handleSelect.bind(this, record, rowIndex)} />
421
    );
Y
yiminghe 已提交
422
  },
A
afc163 已提交
423 424

  getRecordKey(record, index) {
425 426 427
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
428
    return record.key || index;
429
  },
A
afc163 已提交
430

431
  renderRowSelection() {
Y
yiminghe 已提交
432
    let columns = this.props.columns.concat();
433
    if (this.props.rowSelection) {
A
afc163 已提交
434
      let data = this.getFlatCurrentPageData().filter((item) => {
435 436 437 438 439
        if (this.props.rowSelection.getCheckboxProps) {
          return !this.props.rowSelection.getCheckboxProps(item).disabled;
        }
        return true;
      });
Y
yiminghe 已提交
440 441 442 443
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
444 445 446
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
A
afc163 已提交
447 448 449 450
          : (
            data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
            data.every((item) =>
A
afc163 已提交
451
              this.props.rowSelection.getCheckboxProps &&
A
afc163 已提交
452 453
              this.props.rowSelection.getCheckboxProps(item).defaultChecked)
          );
Y
yiminghe 已提交
454
      }
R
RaoHai 已提交
455 456 457 458 459 460 461 462
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
463 464 465 466 467
        const checkboxAllDisabled = data.every(item =>
          this.props.rowSelection.getCheckboxProps &&
          this.props.rowSelection.getCheckboxProps(item).disabled);
        const checkboxAll = (
            <Checkbox checked={checked}
468 469
              disabled={checkboxAllDisabled}
              onChange={this.handleSelectAllRow} />
470
        );
R
RaoHai 已提交
471 472 473 474 475 476 477
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
A
afc163 已提交
478
      if (columns[0] && columns[0].key === 'selection-column') {
479 480 481 482 483 484 485
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
486

A
afc163 已提交
487 488 489 490 491
  getColumnKey(column, index) {
    return column.key || column.dataIndex || index;
  },

  isSortColumn(column) {
A
afc163 已提交
492 493
    const { sortColumn } = this.state;
    if (!column || !sortColumn) {
A
afc163 已提交
494 495
      return false;
    }
A
afc163 已提交
496
    return this.getColumnKey(sortColumn) === this.getColumnKey(column);
Y
yiminghe 已提交
497 498 499
  },

  renderColumnsDropdown(columns) {
A
afc163 已提交
500
    const { sortOrder } = this.state;
A
afc163 已提交
501
    const locale = this.getLocale();
502
    return columns.map((originColumn, i) => {
B
Benjy Cui 已提交
503
      let column = { ...originColumn };
A
afc163 已提交
504
      let key = this.getColumnKey(column, i);
505 506
      let filterDropdown;
      let sortButton;
507
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
508
        let colFilters = this.state.filters[key] || [];
509
        filterDropdown = (
B
Benjy Cui 已提交
510
          <FilterDropdown locale={locale} column={column}
511
            selectedKeys={colFilters}
512
            confirmFilter={this.handleFilter} />
513
        );
A
afc163 已提交
514 515
      }
      if (column.sorter) {
A
afc163 已提交
516
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
517 518
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
519
          if (sortOrder) {
A
afc163 已提交
520 521
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
522
        }
A
afc163 已提交
523 524
        const isAscend = isSortColumn && sortOrder === 'ascend';
        const isDescend = isSortColumn && sortOrder === 'descend';
525 526
        sortButton = (
          <div className="ant-table-column-sorter">
527
            <span className={`ant-table-column-sorter-up ${isAscend ? 'on' : 'off'}`}
528 529
              title="↑"
              onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
530
              <Icon type="caret-up" />
531
            </span>
532
            <span className={`ant-table-column-sorter-down ${isDescend ? 'on' : 'off'}`}
533 534
              title="↓"
              onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
535
              <Icon type="caret-down" />
536 537 538
            </span>
          </div>
        );
A
afc163 已提交
539
      }
540
      column.title = (
541
        <span>
542 543 544
          {column.title}
          {sortButton}
          {filterDropdown}
545
        </span>
546
      );
A
afc163 已提交
547
      return column;
A
afc163 已提交
548 549
    });
  },
A
afc163 已提交
550

551
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
552
    const pagination = this.state.pagination;
553
    pagination.onShowSizeChange(current, pageSize);
554
    const nextPagination = { ...pagination, pageSize, current };
B
Benjy Cui 已提交
555
    this.setState({ pagination: nextPagination });
A
afc163 已提交
556
    this.props.onChange(...this.prepareParamsArguments({
557 558 559
      ...this.state,
      pagination: nextPagination,
    }));
560 561
  },

562 563
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
564 565
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
566
    }
A
afc163 已提交
567 568
    let classString = classNames({
      'ant-table-pagination': true,
569
      mini: this.props.size === 'middle' || this.props.size === 'small',
A
afc163 已提交
570
    });
A
afc163 已提交
571
    let total = this.state.pagination.total || this.getLocalData().length;
Y
yiminghe 已提交
572
    const pageSize = this.state.pagination.pageSize;
A
afc163 已提交
573
    return (total > 0) ?
B
Benjy Cui 已提交
574
      <Pagination {...this.state.pagination}
575 576 577 578 579
        className={classString}
        onChange={this.handlePageChange}
        total={total}
        pageSize={pageSize}
        onShowSizeChange={this.handleShowSizeChange} /> : null;
A
afc163 已提交
580
  },
A
afc163 已提交
581

Y
yiminghe 已提交
582
  prepareParamsArguments(state) {
583
    // 准备筛选、排序、分页的参数
584 585 586
    const pagination = state.pagination;
    const filters = state.filters;
    const sorter = {};
A
afc163 已提交
587 588 589 590
    if (state.sortColumn && state.sortOrder) {
      sorter.column = state.sortColumn;
      sorter.order = state.sortOrder;
      sorter.field = state.sortColumn.dataIndex;
A
afc163 已提交
591
      sorter.columnKey = this.getColumnKey(state.sortColumn);
A
afc163 已提交
592 593
    }
    return [pagination, filters, sorter];
594
  },
Y
yiminghe 已提交
595

A
afc163 已提交
596
  findColumn(myKey) {
A
afc163 已提交
597
    return this.props.columns.filter(c => this.getColumnKey(c) === myKey)[0];
Y
yiminghe 已提交
598 599
  },

A
afc163 已提交
600 601
  getCurrentPageData() {
    let data = this.getLocalData();
602 603
    let current;
    let pageSize;
Y
yiminghe 已提交
604 605 606 607 608
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
609
    } else {
Y
yiminghe 已提交
610 611 612 613 614
      pageSize = state.pagination.pageSize;
      current = state.pagination.current;
    }
    // 分页
    // ---
A
afc163 已提交
615
    // 当数据量少于等于每页数量时,直接设置数据
Y
yiminghe 已提交
616 617 618
    // 否则进行读取分页数据
    if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
      data = data.filter((item, i) => {
619
        return i >= (current - 1) * pageSize && i < current * pageSize;
Y
yiminghe 已提交
620 621 622 623 624
      });
    }
    return data;
  },

A
afc163 已提交
625 626 627 628 629
  getFlatCurrentPageData() {
    return flatArray(this.getCurrentPageData());
  },

  getLocalData() {
A
afc163 已提交
630
    const state = this.state;
A
afc163 已提交
631
    let data = this.props.dataSource || [];
Y
yiminghe 已提交
632
    // 排序
A
afc163 已提交
633 634 635
    data = data.slice(0);
    for (let i = 0; i < data.length; i++) {
      data[i].index = i;
Y
yiminghe 已提交
636
    }
A
afc163 已提交
637
    data = data.sort(this.getSorterFn());
Y
yiminghe 已提交
638 639 640 641
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
642 643 644
        if (!col) {
          return;
        }
Y
yiminghe 已提交
645
        let values = state.filters[columnKey] || [];
A
afc163 已提交
646 647 648
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
649 650 651
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
652
      });
A
afc163 已提交
653
    }
Y
yiminghe 已提交
654
    return data;
A
afc163 已提交
655
  },
Y
yiminghe 已提交
656 657

  render() {
A
afc163 已提交
658
    const data = this.getCurrentPageData();
Y
yiminghe 已提交
659
    let columns = this.renderRowSelection();
A
afc163 已提交
660
    const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
661
    const locale = this.getLocale();
A
afc163 已提交
662

A
afc163 已提交
663
    const classString = classNames({
A
afc163 已提交
664 665 666 667 668
      [`ant-table-${this.props.size}`]: true,
      'ant-table-bordered': this.props.bordered,
      [this.props.className]: !!this.props.className,
    });

Y
yiminghe 已提交
669
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
670
    columns = columns.map((column, i) => {
B
Benjy Cui 已提交
671
      const newColumn = { ...column };
672 673
      newColumn.key = newColumn.key || newColumn.dataIndex || i;
      return newColumn;
A
afc163 已提交
674
    });
A
afc163 已提交
675
    let emptyText;
676
    let emptyClass = '';
A
afc163 已提交
677
    if (!data || data.length === 0) {
678 679
      emptyText = (
        <div className="ant-table-placeholder">
680
          <Icon type="frown" />{locale.emptyText}
681 682
        </div>
      );
683
      emptyClass = ' ant-table-empty';
A
afc163 已提交
684
    }
A
afc163 已提交
685

686 687
    let table = (
      <div>
688
        <RcTable {...this.props}
689 690 691
          data={data}
          columns={columns}
          className={classString}
A
afc163 已提交
692
          expandIconColumnIndex={(columns[0] && columns[0].key === 'selection-column') ? 1 : 0}
693
          expandIconAsCell={expandIconAsCell} />
694 695 696
          {emptyText}
      </div>
    );
A
afc163 已提交
697
    if (this.props.loading) {
698 699
      // if there is no pagination or no data,
      // the height of spin should decrease by half of pagination
A
afc163 已提交
700
      const paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
K
KgTong 已提交
701 702
              ? 'ant-table-with-pagination'
              : 'ant-table-without-pagination';
A
afc163 已提交
703
      const spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
704
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
705
    }
706
    return (
707
      <div className={`clearfix${emptyClass}`}>
708 709 710 711
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
712 713
  }
});
714

715
export default Table;