index.jsx 19.2 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,
Y
yiminghe 已提交
36 37
      sortColumn: '',
      sortOrder: '',
R
RaoHai 已提交
38
      radioIndex: null,
39
      pagination: this.hasPagination() ?
B
Benjy Cui 已提交
40 41 42 43 44
      {
        size: this.props.size,
        ...defaultPagination,
        ...this.props.pagination,
      } : {},
A
afc163 已提交
45 46
    };
  },
Y
yiminghe 已提交
47

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

A
afc163 已提交
64
  propTypes: {
A
afc163 已提交
65
    dataSource: React.PropTypes.array,
A
afc163 已提交
66 67 68 69 70 71 72 73 74
    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 已提交
75 76
  },

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

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

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

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

  setSelectedRowKeys(selectedRowKeys) {
    if (this.props.rowSelection &&
        !('selectedRowKeys' in this.props.rowSelection)) {
      this.setState({ selectedRowKeys });
    }
    if (this.props.rowSelection && this.props.rowSelection.onChange) {
A
afc163 已提交
125
      const data = this.getFlatCurrentPageData();
126 127 128
      const selectedRows = data.filter(
        (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0
      );
A
afc163 已提交
129
      this.props.rowSelection.onChange(selectedRowKeys, selectedRows);
A
afc163 已提交
130
    }
A
afc163 已提交
131
  },
A
afc163 已提交
132

A
afc163 已提交
133
  hasPagination() {
A
afc163 已提交
134
    return this.props.pagination !== false;
A
afc163 已提交
135
  },
A
afc163 已提交
136

A
afc163 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150
  getSorterFn() {
    const { sortOrder, sortColumn } = this.state;
    if (!sortOrder || !sortColumn) {
      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 已提交
151
  toggleSortOrder(order, column) {
A
afc163 已提交
152
    let { sortColumn, sortOrder } = this.state;
A
afc163 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
A
afc163 已提交
166 167 168 169
    const newState = {
      sortOrder,
      sortColumn,
    };
A
afc163 已提交
170
    this.setState(newState);
A
afc163 已提交
171
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
172
  },
A
afc163 已提交
173

174
  handleFilter(column, nextFilters) {
B
Benjy Cui 已提交
175 176
    const filters = {
      ...this.state.filters,
177
      [this.getColumnKey(column)]: nextFilters
B
Benjy Cui 已提交
178
    };
179 180 181 182 183 184 185
    // 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 已提交
186
    const newState = {
A
afc163 已提交
187
      selectionDirty: false,
A
afc163 已提交
188
      filters,
A
afc163 已提交
189
    };
A
afc163 已提交
190
    this.setState(newState);
191
    this.setSelectedRowKeys([]);
A
afc163 已提交
192
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
193
  },
A
afc163 已提交
194

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

220
  handleRadioSelect(record, rowIndex, e) {
221 222
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
223 224 225 226
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
227
      radioIndex: key,
228
      selectionDirty: true,
229
    });
230
    this.setSelectedRowKeys(selectedRowKeys);
231
    if (this.props.rowSelection.onSelect) {
A
afc163 已提交
232
      let data = this.getFlatCurrentPageData();
Y
yiminghe 已提交
233 234
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
235
      });
Y
yiminghe 已提交
236
      this.props.rowSelection.onSelect(record, checked, selectedRows);
237 238
    }
  },
A
afc163 已提交
239

A
afc163 已提交
240
  handleSelectAllRow(e) {
241
    const checked = e.target.checked;
A
afc163 已提交
242
    const data = this.getFlatCurrentPageData();
243 244 245 246 247 248
    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));
249 250 251

    // 记录变化的列
    const changeRowKeys = [];
252 253 254 255
    if (checked) {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) < 0) {
          selectedRowKeys.push(key);
256
          changeRowKeys.push(key);
257 258 259 260 261 262
        }
      });
    } else {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) >= 0) {
          selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
263
          changeRowKeys.push(key);
264 265 266
        }
      });
    }
A
afc163 已提交
267
    this.setState({
268
      selectionDirty: true,
269
    });
270
    this.setSelectedRowKeys(selectedRowKeys);
271
    if (this.props.rowSelection.onSelectAll) {
272 273 274 275 276
      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 已提交
277
    }
A
afc163 已提交
278
  },
A
afc163 已提交
279

280
  handlePageChange(current) {
B
Benjy Cui 已提交
281
    let pagination = { ...this.state.pagination };
282 283 284 285 286
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
287 288
    pagination.onChange(pagination.current);

A
afc163 已提交
289
    const newState = {
A
afc163 已提交
290
      selectionDirty: false,
A
afc163 已提交
291 292
      pagination
    };
A
afc163 已提交
293
    this.setState(newState);
A
afc163 已提交
294
    this.props.onChange(...this.prepareParamsArguments({ ...this.state, ...newState }));
295
  },
A
afc163 已提交
296

297
  onRadioChange(ev) {
R
RaoHai 已提交
298 299
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
300
    });
301
  },
A
afc163 已提交
302

R
RaoHai 已提交
303 304 305 306 307 308
  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 已提交
309 310
    let checked;
    if (this.state.selectionDirty) {
311
      checked = this.state.radioIndex === rowIndex;
A
afc163 已提交
312
    } else {
313
      checked = (this.state.radioIndex === rowIndex ||
A
afc163 已提交
314 315
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
316
    return (
317 318
      <Radio disabled={props.disabled}
        onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
319
        value={rowIndex} checked={checked} />
320
    );
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);
    }
336 337
    return (
      <Checkbox checked={checked} disabled={props.disabled}
338
        onChange={this.handleSelect.bind(this, record, rowIndex)} />
339
    );
Y
yiminghe 已提交
340
  },
A
afc163 已提交
341 342

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

349
  renderRowSelection() {
Y
yiminghe 已提交
350
    let columns = this.props.columns.concat();
351
    if (this.props.rowSelection) {
A
afc163 已提交
352
      let data = this.getFlatCurrentPageData().filter((item) => {
353 354 355 356 357
        if (this.props.rowSelection.getCheckboxProps) {
          return !this.props.rowSelection.getCheckboxProps(item).disabled;
        }
        return true;
      });
Y
yiminghe 已提交
358 359 360 361
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
362 363 364
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
A
afc163 已提交
365 366 367 368
          : (
            data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
            data.every((item) =>
A
afc163 已提交
369
              this.props.rowSelection.getCheckboxProps &&
A
afc163 已提交
370 371
              this.props.rowSelection.getCheckboxProps(item).defaultChecked)
          );
Y
yiminghe 已提交
372
      }
R
RaoHai 已提交
373 374 375 376 377 378 379 380
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
381 382 383 384 385
        const checkboxAllDisabled = data.every(item =>
          this.props.rowSelection.getCheckboxProps &&
          this.props.rowSelection.getCheckboxProps(item).disabled);
        const checkboxAll = (
            <Checkbox checked={checked}
386 387
              disabled={checkboxAllDisabled}
              onChange={this.handleSelectAllRow} />
388
        );
R
RaoHai 已提交
389 390 391 392 393 394 395
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
A
afc163 已提交
396
      if (columns[0] && columns[0].key === 'selection-column') {
397 398 399 400 401 402 403
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
404

A
afc163 已提交
405 406 407 408 409
  getColumnKey(column, index) {
    return column.key || column.dataIndex || index;
  },

  isSortColumn(column) {
A
afc163 已提交
410 411
    const { sortColumn } = this.state;
    if (!column || !sortColumn) {
A
afc163 已提交
412 413
      return false;
    }
A
afc163 已提交
414
    return this.getColumnKey(sortColumn) === this.getColumnKey(column);
Y
yiminghe 已提交
415 416 417
  },

  renderColumnsDropdown(columns) {
A
afc163 已提交
418
    const { sortOrder } = this.state;
A
afc163 已提交
419
    const locale = this.getLocale();
420
    return columns.map((originColumn, i) => {
B
Benjy Cui 已提交
421
      let column = { ...originColumn };
A
afc163 已提交
422
      let key = this.getColumnKey(column, i);
423 424
      let filterDropdown;
      let sortButton;
425
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
426
        let colFilters = this.state.filters[key] || [];
427
        filterDropdown = (
B
Benjy Cui 已提交
428
          <FilterDropdown locale={locale} column={column}
429
            selectedKeys={colFilters}
430
            confirmFilter={this.handleFilter} />
431
        );
A
afc163 已提交
432 433
      }
      if (column.sorter) {
A
afc163 已提交
434
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
435 436
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
437
          if (sortOrder) {
A
afc163 已提交
438 439
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
440
        }
A
afc163 已提交
441 442
        const isAscend = isSortColumn && sortOrder === 'ascend';
        const isDescend = isSortColumn && sortOrder === 'descend';
443 444
        sortButton = (
          <div className="ant-table-column-sorter">
445
            <span className={`ant-table-column-sorter-up ${isAscend ? 'on' : 'off'}`}
446 447
              title="↑"
              onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
448
              <Icon type="caret-up" />
449
            </span>
450
            <span className={`ant-table-column-sorter-down ${isDescend ? 'on' : 'off'}`}
451 452
              title="↓"
              onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
453
              <Icon type="caret-down" />
454 455 456
            </span>
          </div>
        );
A
afc163 已提交
457
      }
458
      column.title = (
459
        <span>
460 461 462
          {column.title}
          {sortButton}
          {filterDropdown}
463
        </span>
464
      );
A
afc163 已提交
465
      return column;
A
afc163 已提交
466 467
    });
  },
A
afc163 已提交
468

469
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
470
    const pagination = this.state.pagination;
471
    pagination.onShowSizeChange(current, pageSize);
472
    const nextPagination = { ...pagination, pageSize, current };
B
Benjy Cui 已提交
473
    this.setState({ pagination: nextPagination });
A
afc163 已提交
474
    this.props.onChange(...this.prepareParamsArguments({
475 476 477
      ...this.state,
      pagination: nextPagination,
    }));
478 479
  },

480 481
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
482 483
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
484
    }
A
afc163 已提交
485 486
    let classString = classNames({
      'ant-table-pagination': true,
487
      mini: this.props.size === 'middle' || this.props.size === 'small',
A
afc163 已提交
488
    });
A
afc163 已提交
489
    let total = this.state.pagination.total || this.getLocalData().length;
Y
yiminghe 已提交
490
    const pageSize = this.state.pagination.pageSize;
A
afc163 已提交
491
    return (total > 0) ?
B
Benjy Cui 已提交
492
      <Pagination {...this.state.pagination}
493 494 495 496 497
        className={classString}
        onChange={this.handlePageChange}
        total={total}
        pageSize={pageSize}
        onShowSizeChange={this.handleShowSizeChange} /> : null;
A
afc163 已提交
498
  },
A
afc163 已提交
499

Y
yiminghe 已提交
500
  prepareParamsArguments(state) {
501
    // 准备筛选、排序、分页的参数
502 503 504
    const pagination = state.pagination;
    const filters = state.filters;
    const sorter = {};
A
afc163 已提交
505 506 507 508
    if (this.state.sortColumn && this.state.sortOrder) {
      sorter.column = this.state.sortColumn;
      sorter.order = this.state.sortOrder;
      sorter.field = this.state.sortColumn.dataIndex;
A
afc163 已提交
509 510
    }
    return [pagination, filters, sorter];
511
  },
Y
yiminghe 已提交
512

A
afc163 已提交
513
  findColumn(myKey) {
A
afc163 已提交
514
    return this.props.columns.filter(c => this.getColumnKey(c) === myKey)[0];
Y
yiminghe 已提交
515 516
  },

A
afc163 已提交
517 518
  getCurrentPageData() {
    let data = this.getLocalData();
519 520
    let current;
    let pageSize;
Y
yiminghe 已提交
521 522 523 524 525
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
526
    } else {
Y
yiminghe 已提交
527 528 529 530 531
      pageSize = state.pagination.pageSize;
      current = state.pagination.current;
    }
    // 分页
    // ---
A
afc163 已提交
532
    // 当数据量少于等于每页数量时,直接设置数据
Y
yiminghe 已提交
533 534 535
    // 否则进行读取分页数据
    if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
      data = data.filter((item, i) => {
536
        return i >= (current - 1) * pageSize && i < current * pageSize;
Y
yiminghe 已提交
537 538 539 540 541
      });
    }
    return data;
  },

A
afc163 已提交
542 543 544 545 546
  getFlatCurrentPageData() {
    return flatArray(this.getCurrentPageData());
  },

  getLocalData() {
A
afc163 已提交
547
    const state = this.state;
A
afc163 已提交
548
    let data = this.props.dataSource || [];
Y
yiminghe 已提交
549
    // 排序
A
afc163 已提交
550 551 552
    data = data.slice(0);
    for (let i = 0; i < data.length; i++) {
      data[i].index = i;
Y
yiminghe 已提交
553
    }
A
afc163 已提交
554
    data = data.sort(this.getSorterFn());
Y
yiminghe 已提交
555 556 557 558
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
559 560 561
        if (!col) {
          return;
        }
Y
yiminghe 已提交
562
        let values = state.filters[columnKey] || [];
A
afc163 已提交
563 564 565
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
566 567 568
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
569
      });
A
afc163 已提交
570
    }
Y
yiminghe 已提交
571
    return data;
A
afc163 已提交
572
  },
Y
yiminghe 已提交
573 574

  render() {
A
afc163 已提交
575
    const data = this.getCurrentPageData();
Y
yiminghe 已提交
576
    let columns = this.renderRowSelection();
A
afc163 已提交
577
    const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
578
    const locale = this.getLocale();
A
afc163 已提交
579

A
afc163 已提交
580
    const classString = classNames({
A
afc163 已提交
581 582 583 584 585
      [`ant-table-${this.props.size}`]: true,
      'ant-table-bordered': this.props.bordered,
      [this.props.className]: !!this.props.className,
    });

Y
yiminghe 已提交
586
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
587
    columns = columns.map((column, i) => {
B
Benjy Cui 已提交
588
      const newColumn = { ...column };
589 590
      newColumn.key = newColumn.key || newColumn.dataIndex || i;
      return newColumn;
A
afc163 已提交
591
    });
A
afc163 已提交
592
    let emptyText;
593
    let emptyClass = '';
A
afc163 已提交
594
    if (!data || data.length === 0) {
595 596
      emptyText = (
        <div className="ant-table-placeholder">
597
          <Icon type="frown" />{locale.emptyText}
598 599
        </div>
      );
600
      emptyClass = ' ant-table-empty';
A
afc163 已提交
601
    }
A
afc163 已提交
602

603 604
    let table = (
      <div>
605
        <RcTable {...this.props}
606 607 608
          data={data}
          columns={columns}
          className={classString}
A
afc163 已提交
609
          expandIconColumnIndex={(columns[0] && columns[0].key === 'selection-column') ? 1 : 0}
610
          expandIconAsCell={expandIconAsCell} />
611 612 613
          {emptyText}
      </div>
    );
A
afc163 已提交
614
    if (this.props.loading) {
615 616
      // if there is no pagination or no data,
      // the height of spin should decrease by half of pagination
A
afc163 已提交
617
      const paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
K
KgTong 已提交
618 619
              ? 'ant-table-with-pagination'
              : 'ant-table-without-pagination';
A
afc163 已提交
620
      const spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
621
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
622
    }
623
    return (
624
      <div className={`clearfix${emptyClass}`}>
625 626 627 628
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
629 630
  }
});
631

632
export default Table;