index.jsx 19.3 KB
Newer Older
A
afc163 已提交
1 2
import React from 'react';
import Table 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';
A
afc163 已提交
8
import objectAssign from 'object-assign';
K
KgTong 已提交
9
import Spin from '../spin';
A
afc163 已提交
10
import classNames from 'classnames';
A
afc163 已提交
11
import { flatArray } from './util';
A
afc163 已提交
12

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

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

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

A
afc163 已提交
30
let AntTable = React.createClass({
Y
yiminghe 已提交
31
  getInitialState() {
A
afc163 已提交
32
    return {
Y
yiminghe 已提交
33
      // 减少状态
34
      selectedRowKeys: this.props.selectedRowKeys || [],
Y
yiminghe 已提交
35
      filters: {},
A
afc163 已提交
36
      selectionDirty: false,
Y
yiminghe 已提交
37 38 39
      sortColumn: '',
      sortOrder: '',
      sorter: null,
R
RaoHai 已提交
40
      radioIndex: null,
41
      pagination: this.hasPagination() ?
A
afc163 已提交
42 43 44
        objectAssign({
          size: this.props.size,
        }, defaultPagination, this.props.pagination) :
45
        {},
A
afc163 已提交
46 47
    };
  },
Y
yiminghe 已提交
48

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

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

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

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

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

A
afc163 已提交
99
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
100
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
101
      this.setState({
Y
yiminghe 已提交
102
        pagination: objectAssign({}, defaultPagination, this.state.pagination, nextProps.pagination)
103
      });
A
afc163 已提交
104
    }
105
    // dataSource 的变化会清空选中项
106
    if ('dataSource' in nextProps &&
A
afc163 已提交
107
        nextProps.dataSource !== this.props.dataSource) {
108
      this.setState({
A
afc163 已提交
109
        selectionDirty: false,
A
afc163 已提交
110
      });
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }
    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 已提交
126
      const data = this.getFlatCurrentPageData();
127 128 129
      const selectedRows = data.filter(
        (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0
      );
A
afc163 已提交
130
      this.props.rowSelection.onChange(selectedRowKeys, selectedRows);
A
afc163 已提交
131
    }
A
afc163 已提交
132
  },
A
afc163 已提交
133

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

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

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

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

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

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

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

278
  handlePageChange(current) {
Y
yiminghe 已提交
279
    let pagination = objectAssign({}, this.state.pagination);
280 281 282 283 284
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
285 286
    pagination.onChange(pagination.current);

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

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

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

321
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
322
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
323 324 325 326 327 328 329
    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 已提交
330 331 332 333
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
334 335
    return (
      <Checkbox checked={checked} disabled={props.disabled}
336
        onChange={this.handleSelect.bind(this, record, rowIndex)} />
337
    );
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) {
A
afc163 已提交
350
      let data = this.getFlatCurrentPageData().filter((item) => {
351 352 353 354 355
        if (this.props.rowSelection.getCheckboxProps) {
          return !this.props.rowSelection.getCheckboxProps(item).disabled;
        }
        return true;
      });
Y
yiminghe 已提交
356 357 358 359
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
360 361 362
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
A
afc163 已提交
363 364 365 366
          : (
            data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
            data.every((item) =>
A
afc163 已提交
367
              this.props.rowSelection.getCheckboxProps &&
A
afc163 已提交
368 369
              this.props.rowSelection.getCheckboxProps(item).defaultChecked)
          );
Y
yiminghe 已提交
370
      }
R
RaoHai 已提交
371 372 373 374 375 376 377 378
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
379 380 381 382 383
        const checkboxAllDisabled = data.every(item =>
          this.props.rowSelection.getCheckboxProps &&
          this.props.rowSelection.getCheckboxProps(item).disabled);
        const checkboxAll = (
            <Checkbox checked={checked}
384 385
              disabled={checkboxAllDisabled}
              onChange={this.handleSelectAllRow} />
386
        );
R
RaoHai 已提交
387 388 389 390 391 392 393
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
A
afc163 已提交
394
      if (columns[0] && columns[0].key === 'selection-column') {
395 396 397 398 399 400 401
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
402

A
afc163 已提交
403 404 405 406 407 408 409 410 411 412 413
  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 已提交
414 415 416
  },

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

440 441
        const isAscend = isSortColumn && this.state.sortOrder === 'ascend';
        const isDescend = isSortColumn && this.state.sortOrder === 'descend';
442 443
        sortButton = (
          <div className="ant-table-column-sorter">
444
            <span className={`ant-table-column-sorter-up ${isAscend ? 'on' : 'off'}`}
445 446
              title="↑"
              onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
447
              <Icon type="caret-up" />
448
            </span>
449
            <span className={`ant-table-column-sorter-down ${isDescend ? 'on' : 'off'}`}
450 451
              title="↓"
              onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
452
              <Icon type="caret-down" />
453 454 455
            </span>
          </div>
        );
A
afc163 已提交
456
      }
457
      column.title = (
458
        <span>
459 460 461
          {column.title}
          {sortButton}
          {filterDropdown}
462
        </span>
463
      );
A
afc163 已提交
464
      return column;
A
afc163 已提交
465 466
    });
  },
A
afc163 已提交
467

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

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

Y
yiminghe 已提交
499
  prepareParamsArguments(state) {
500
    // 准备筛选、排序、分页的参数
501 502 503
    const pagination = state.pagination;
    const filters = state.filters;
    const sorter = {};
Y
yiminghe 已提交
504 505 506 507 508
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
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() {
Y
yiminghe 已提交
547
    let state = this.state;
A
afc163 已提交
548
    let data = this.props.dataSource || [];
Y
yiminghe 已提交
549 550
    // 排序
    if (state.sortOrder && state.sorter) {
A
afc163 已提交
551 552 553 554
      data = data.slice(0);
      for (let i = 0; i < data.length; i++) {
        data[i].index = i;
      }
Y
yiminghe 已提交
555 556 557 558 559 560
      data = data.sort(state.sorter);
    }
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
561 562 563
        if (!col) {
          return;
        }
Y
yiminghe 已提交
564
        let values = state.filters[columnKey] || [];
A
afc163 已提交
565 566 567
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
568 569 570
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
571
      });
A
afc163 已提交
572
    }
Y
yiminghe 已提交
573
    return data;
A
afc163 已提交
574
  },
Y
yiminghe 已提交
575 576

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

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

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

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

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