index.jsx 19.0 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

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
let AntTable = 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 38
      sortColumn: '',
      sortOrder: '',
      sorter: null,
R
RaoHai 已提交
39
      radioIndex: null,
40
      pagination: this.hasPagination() ?
A
afc163 已提交
41 42 43
        objectAssign({
          size: this.props.size,
        }, defaultPagination, this.props.pagination) :
44
        {},
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
  },

R
RaoHai 已提交
77
  getDefaultSelection() {
78 79
    if (!this.props.rowSelection || !this.props.rowSelection.getCheckboxProps) {
      return [];
R
RaoHai 已提交
80
    }
81 82 83
    return this.getCurrentPageData()
      .filter(item => this.props.rowSelection.getCheckboxProps(item).defaultChecked)
      .map((record, rowIndex) => this.getRecordKey(record, rowIndex));
R
RaoHai 已提交
84 85
  },

A
afc163 已提交
86
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
87
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
88
      this.setState({
Y
yiminghe 已提交
89
        pagination: objectAssign({}, defaultPagination, this.state.pagination, nextProps.pagination)
90
      });
A
afc163 已提交
91
    }
92
    // dataSource 的变化会清空选中项
93
    if ('dataSource' in nextProps &&
A
afc163 已提交
94
        nextProps.dataSource !== this.props.dataSource) {
95
      this.setState({
A
afc163 已提交
96
        selectionDirty: false,
A
afc163 已提交
97
      });
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }
    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 已提交
113
      const data = this.getCurrentPageData();
114 115 116
      const selectedRows = data.filter(
        (row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0
      );
A
afc163 已提交
117
      this.props.rowSelection.onChange(selectedRowKeys, selectedRows);
A
afc163 已提交
118
    }
A
afc163 已提交
119
  },
A
afc163 已提交
120

A
afc163 已提交
121
  hasPagination() {
A
afc163 已提交
122
    return this.props.pagination !== false;
A
afc163 已提交
123
  },
A
afc163 已提交
124

A
afc163 已提交
125
  toggleSortOrder(order, column) {
126 127
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
J
jljsj 已提交
128
    let sorter;
A
afc163 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
A
afc163 已提交
142
    if (typeof column.sorter === 'function') {
A
afc163 已提交
143 144 145 146
      sorter = (a, b) => {
        let result = column.sorter(a, b);
        if (result !== 0) {
          return (sortOrder === 'descend') ? -result : result;
147
        }
A
afc163 已提交
148
        return a.index - b.index;
149
      };
A
afc163 已提交
150
    }
A
afc163 已提交
151 152 153
    const newState = {
      sortOrder,
      sortColumn,
A
afc163 已提交
154
      sorter,
A
afc163 已提交
155
    };
A
afc163 已提交
156
    this.setState(newState);
157
    this.props.onChange(this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
158
  },
A
afc163 已提交
159

160 161 162
  handleFilter(column, nextFilters) {
    const filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: nextFilters
Y
yiminghe 已提交
163
    });
164 165 166 167 168 169 170
    // 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 已提交
171
    const newState = {
A
afc163 已提交
172
      selectionDirty: false,
A
afc163 已提交
173 174
      filters
    };
A
afc163 已提交
175
    this.setState(newState);
176
    this.setSelectedRowKeys([]);
177
    this.props.onChange(this.prepareParamsArguments({ ...this.state, ...newState }));
A
afc163 已提交
178
  },
A
afc163 已提交
179

Y
yiminghe 已提交
180
  handleSelect(record, rowIndex, e) {
181 182
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
183
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
184
    let key = this.getRecordKey(record, rowIndex);
185
    if (checked) {
Y
yiminghe 已提交
186
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
187
    } else {
Y
yiminghe 已提交
188 189
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
190 191 192
      });
    }
    this.setState({
193
      selectionDirty: true,
R
RaoHai 已提交
194
    });
195
    this.setSelectedRowKeys(selectedRowKeys);
R
RaoHai 已提交
196 197 198 199 200 201 202 203 204
    if (this.props.rowSelection.onSelect) {
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
      });
      this.props.rowSelection.onSelect(record, checked, selectedRows);
    }
  },

205
  handleRadioSelect(record, rowIndex, e) {
206 207
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
208 209 210 211
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
212
      radioIndex: key,
213
      selectionDirty: true,
214
    });
215
    this.setSelectedRowKeys(selectedRowKeys);
216
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
217 218 219
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
220
      });
Y
yiminghe 已提交
221
      this.props.rowSelection.onSelect(record, checked, selectedRows);
222 223
    }
  },
A
afc163 已提交
224

A
afc163 已提交
225
  handleSelectAllRow(e) {
226 227 228 229 230 231 232 233
    const checked = e.target.checked;
    const data = this.getCurrentPageData();
    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));
234 235 236

    // 记录变化的列
    const changeRowKeys = [];
237 238 239 240
    if (checked) {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) < 0) {
          selectedRowKeys.push(key);
241
          changeRowKeys.push(key);
242 243 244 245 246 247
        }
      });
    } else {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) >= 0) {
          selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
248
          changeRowKeys.push(key);
249 250 251
        }
      });
    }
A
afc163 已提交
252
    this.setState({
253
      selectionDirty: true,
254
    });
255
    this.setSelectedRowKeys(selectedRowKeys);
256
    if (this.props.rowSelection.onSelectAll) {
257 258 259 260 261
      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 已提交
262
    }
A
afc163 已提交
263
  },
A
afc163 已提交
264

265
  handlePageChange(current) {
Y
yiminghe 已提交
266
    let pagination = objectAssign({}, this.state.pagination);
267 268 269 270 271
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
272 273
    pagination.onChange(pagination.current);

A
afc163 已提交
274
    const newState = {
A
afc163 已提交
275
      selectionDirty: false,
A
afc163 已提交
276 277
      pagination
    };
A
afc163 已提交
278
    this.setState(newState);
279
    this.props.onChange(this.prepareParamsArguments({ ...this.state, ...newState }));
280
  },
A
afc163 已提交
281

282
  onRadioChange(ev) {
R
RaoHai 已提交
283 284
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
285
    });
286
  },
A
afc163 已提交
287

R
RaoHai 已提交
288 289 290 291 292 293
  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 已提交
294 295
    let checked;
    if (this.state.selectionDirty) {
296
      checked = this.state.radioIndex === rowIndex;
A
afc163 已提交
297
    } else {
298
      checked = (this.state.radioIndex === rowIndex ||
A
afc163 已提交
299 300
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
301
    return (
302 303
      <Radio disabled={props.disabled}
        onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
304
        value={rowIndex} checked={checked} />
305
    );
R
RaoHai 已提交
306 307
  },

308
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
309
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
310 311 312 313 314 315 316
    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 已提交
317 318 319 320
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
321 322
    return (
      <Checkbox checked={checked} disabled={props.disabled}
323
        onChange={this.handleSelect.bind(this, record, rowIndex)} />
324
    );
Y
yiminghe 已提交
325
  },
A
afc163 已提交
326 327

  getRecordKey(record, index) {
328 329 330
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
331
    return record.key || index;
332
  },
A
afc163 已提交
333

334
  renderRowSelection() {
Y
yiminghe 已提交
335
    let columns = this.props.columns.concat();
336
    if (this.props.rowSelection) {
337 338 339 340 341 342
      let data = this.getCurrentPageData().filter((item) => {
        if (this.props.rowSelection.getCheckboxProps) {
          return !this.props.rowSelection.getCheckboxProps(item).disabled;
        }
        return true;
      });
Y
yiminghe 已提交
343 344 345 346
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
347 348 349
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
A
afc163 已提交
350 351 352 353
          : (
            data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
            data.every((item) =>
A
afc163 已提交
354
              this.props.rowSelection.getCheckboxProps &&
A
afc163 已提交
355 356
              this.props.rowSelection.getCheckboxProps(item).defaultChecked)
          );
Y
yiminghe 已提交
357
      }
R
RaoHai 已提交
358 359 360 361 362 363 364 365
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
366 367 368 369 370
        const checkboxAllDisabled = data.every(item =>
          this.props.rowSelection.getCheckboxProps &&
          this.props.rowSelection.getCheckboxProps(item).disabled);
        const checkboxAll = (
            <Checkbox checked={checked}
371 372
              disabled={checkboxAllDisabled}
              onChange={this.handleSelectAllRow} />
373
        );
R
RaoHai 已提交
374 375 376 377 378 379 380
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
A
afc163 已提交
381
      if (columns[0] && columns[0].key === 'selection-column') {
382 383 384 385 386 387 388
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
389

A
afc163 已提交
390 391 392 393 394 395 396 397 398 399 400
  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 已提交
401 402 403
  },

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

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

455
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
456
    const pagination = this.state.pagination;
457
    pagination.onShowSizeChange(current, pageSize);
458
    const nextPagination = { ...pagination, pageSize };
B
Benjy Cui 已提交
459
    this.setState({ pagination: nextPagination });
460 461 462 463
    this.props.onChange(this.prepareParamsArguments({
      ...this.state,
      pagination: nextPagination,
    }));
464 465
  },

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

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

A
afc163 已提交
500
  findColumn(myKey) {
A
afc163 已提交
501
    return this.props.columns.filter(c => this.getColumnKey(c) === myKey)[0];
Y
yiminghe 已提交
502 503
  },

A
afc163 已提交
504
  getCurrentPageData(dataSource) {
A
afc163 已提交
505
    let data = this.getLocalData(dataSource);
506 507
    let current;
    let pageSize;
Y
yiminghe 已提交
508 509 510 511 512
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
513
    } else {
Y
yiminghe 已提交
514 515 516 517 518
      pageSize = state.pagination.pageSize;
      current = state.pagination.current;
    }
    // 分页
    // ---
A
afc163 已提交
519
    // 当数据量少于等于每页数量时,直接设置数据
Y
yiminghe 已提交
520 521 522
    // 否则进行读取分页数据
    if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
      data = data.filter((item, i) => {
523
        return i >= (current - 1) * pageSize && i < current * pageSize;
Y
yiminghe 已提交
524 525 526 527 528
      });
    }
    return data;
  },

A
afc163 已提交
529
  getLocalData(dataSource) {
Y
yiminghe 已提交
530
    let state = this.state;
A
afc163 已提交
531
    let data = dataSource || this.props.dataSource || [];
Y
yiminghe 已提交
532 533
    // 排序
    if (state.sortOrder && state.sorter) {
A
afc163 已提交
534 535 536 537
      data = data.slice(0);
      for (let i = 0; i < data.length; i++) {
        data[i].index = i;
      }
Y
yiminghe 已提交
538 539 540 541 542 543
      data = data.sort(state.sorter);
    }
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
544 545 546
        if (!col) {
          return;
        }
Y
yiminghe 已提交
547
        let values = state.filters[columnKey] || [];
A
afc163 已提交
548 549 550
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
551 552 553
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
554
      });
A
afc163 已提交
555
    }
Y
yiminghe 已提交
556
    return data;
A
afc163 已提交
557
  },
Y
yiminghe 已提交
558 559

  render() {
A
afc163 已提交
560
    const data = this.getCurrentPageData();
Y
yiminghe 已提交
561
    let columns = this.renderRowSelection();
A
afc163 已提交
562 563
    const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
    const locale = objectAssign({}, defaultLocale, this.props.locale);
A
afc163 已提交
564

A
afc163 已提交
565
    const classString = classNames({
A
afc163 已提交
566 567 568 569 570
      [`ant-table-${this.props.size}`]: true,
      'ant-table-bordered': this.props.bordered,
      [this.props.className]: !!this.props.className,
    });

Y
yiminghe 已提交
571
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
572
    columns = columns.map((column, i) => {
573 574 575
      const newColumn = objectAssign({}, column);
      newColumn.key = newColumn.key || newColumn.dataIndex || i;
      return newColumn;
A
afc163 已提交
576
    });
A
afc163 已提交
577
    let emptyText;
578
    let emptyClass = '';
A
afc163 已提交
579
    if (!data || data.length === 0) {
580 581
      emptyText = (
        <div className="ant-table-placeholder">
582
          <Icon type="frown" />{locale.emptyText}
583 584
        </div>
      );
585
      emptyClass = ' ant-table-empty';
A
afc163 已提交
586
    }
A
afc163 已提交
587

588 589 590
    let table = (
      <div>
        <Table {...this.props}
591 592 593
          data={data}
          columns={columns}
          className={classString}
A
afc163 已提交
594
          expandIconColumnIndex={(columns[0] && columns[0].key === 'selection-column') ? 1 : 0}
595
          expandIconAsCell={expandIconAsCell} />
596 597 598
          {emptyText}
      </div>
    );
A
afc163 已提交
599
    if (this.props.loading) {
600 601
      // if there is no pagination or no data,
      // the height of spin should decrease by half of pagination
A
afc163 已提交
602
      const paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
K
KgTong 已提交
603 604
              ? 'ant-table-with-pagination'
              : 'ant-table-without-pagination';
A
afc163 已提交
605
      const spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
606
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
607
    }
608
    return (
609
      <div className={`clearfix${emptyClass}`}>
610 611 612 613
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
614 615
  }
});
616

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