index.jsx 18.1 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 41 42
      pagination: this.hasPagination() ?
        objectAssign({}, defaultPagination, this.props.pagination) :
        {},
A
afc163 已提交
43 44
    };
  },
Y
yiminghe 已提交
45

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

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

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

A
afc163 已提交
84
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
85
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
86 87 88
      this.setState({
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
      });
A
afc163 已提交
89
    }
90
    // dataSource 的变化会清空选中项
91
    if ('dataSource' in nextProps &&
A
afc163 已提交
92
        nextProps.dataSource !== this.props.dataSource) {
93
      this.setState({
A
afc163 已提交
94
        selectionDirty: false,
A
afc163 已提交
95
      });
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      this.setSelectedRowKeys([]);
    }
    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) {
      this.props.rowSelection.onChange(selectedRowKeys);
A
afc163 已提交
113
    }
A
afc163 已提交
114
  },
A
afc163 已提交
115

A
afc163 已提交
116
  hasPagination() {
A
afc163 已提交
117
    return this.props.pagination !== false;
A
afc163 已提交
118
  },
A
afc163 已提交
119

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

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

Y
yiminghe 已提交
176
  handleSelect(record, rowIndex, e) {
177 178
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
179
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
180
    let key = this.getRecordKey(record, rowIndex);
181
    if (checked) {
Y
yiminghe 已提交
182
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
183
    } else {
Y
yiminghe 已提交
184 185
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
186 187 188
      });
    }
    this.setState({
189
      selectionDirty: true,
R
RaoHai 已提交
190
    });
191
    this.setSelectedRowKeys(selectedRowKeys);
R
RaoHai 已提交
192 193 194 195 196 197 198 199 200 201
    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);
    }
  },

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

A
afc163 已提交
221
  handleSelectAllRow(e) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    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));
    if (checked) {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) < 0) {
          selectedRowKeys.push(key);
        }
      });
    } else {
      changableRowKeys.forEach(key => {
        if (selectedRowKeys.indexOf(key) >= 0) {
          selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
        }
      });
    }
A
afc163 已提交
243
    this.setState({
244
      selectionDirty: true,
245
    });
246
    this.setSelectedRowKeys(selectedRowKeys);
247
    if (this.props.rowSelection.onSelectAll) {
Y
yiminghe 已提交
248 249
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
250 251
      });
      this.props.rowSelection.onSelectAll(checked, selectedRows);
A
afc163 已提交
252
    }
A
afc163 已提交
253
  },
A
afc163 已提交
254

255
  handlePageChange(current) {
Y
yiminghe 已提交
256
    let pagination = objectAssign({}, this.state.pagination);
257 258 259 260 261
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
262 263
    pagination.onChange(pagination.current);

A
afc163 已提交
264
    const newState = {
A
afc163 已提交
265
      selectionDirty: false,
A
afc163 已提交
266 267
      pagination
    };
A
afc163 已提交
268
    this.setState(newState);
269
    this.setSelectedRowKeys([]);
A
afc163 已提交
270
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
271
  },
A
afc163 已提交
272

R
RaoHai 已提交
273 274 275
  onRadioChange: function (ev) {
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
276
    });
277
  },
A
afc163 已提交
278

R
RaoHai 已提交
279 280 281 282 283 284
  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 已提交
285 286
    let checked;
    if (this.state.selectionDirty) {
287
      checked = this.state.radioIndex === rowIndex;
A
afc163 已提交
288
    } else {
289
      checked = (this.state.radioIndex === rowIndex ||
A
afc163 已提交
290 291
                 this.getDefaultSelection().indexOf(rowIndex) >= 0);
    }
Y
yiminghe 已提交
292
    return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
293
                  value={rowIndex} checked={checked}/>;
R
RaoHai 已提交
294 295
  },

296
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
297
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
298 299 300 301 302 303 304
    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 已提交
305 306 307 308
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
Y
yiminghe 已提交
309 310
    return <Checkbox checked={checked} disabled={props.disabled}
                     onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
Y
yiminghe 已提交
311
  },
A
afc163 已提交
312 313

  getRecordKey(record, index) {
314 315 316
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
317
    return record.key || index;
318
  },
A
afc163 已提交
319

320
  renderRowSelection() {
Y
yiminghe 已提交
321
    let columns = this.props.columns.concat();
322
    if (this.props.rowSelection) {
323 324 325 326 327 328
      let data = this.getCurrentPageData().filter((item) => {
        if (this.props.rowSelection.getCheckboxProps) {
          return !this.props.rowSelection.getCheckboxProps(item).disabled;
        }
        return true;
      });
Y
yiminghe 已提交
329 330 331 332
      let checked;
      if (!data.length) {
        checked = false;
      } else {
A
afc163 已提交
333 334 335
        checked = this.state.selectionDirty
          ? data.every((item, i) =>
              this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
A
afc163 已提交
336
          : data.every((item) =>
A
afc163 已提交
337 338
              this.props.rowSelection.getCheckboxProps &&
              this.props.rowSelection.getCheckboxProps(item).defaultChecked);
Y
yiminghe 已提交
339
      }
R
RaoHai 已提交
340 341 342 343 344 345 346 347
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
348 349 350 351 352 353 354 355
        const checkboxAllDisabled = data.every(item =>
          this.props.rowSelection.getCheckboxProps &&
          this.props.rowSelection.getCheckboxProps(item).disabled);
        const checkboxAll = (
            <Checkbox checked={checked}
                      disabled={checkboxAllDisabled}
                      onChange={this.handleSelectAllRow} />
        );
R
RaoHai 已提交
356 357 358 359 360 361 362
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
363
      if (columns[0] &&
Y
yiminghe 已提交
364
        columns[0].key === 'selection-column') {
365 366 367 368 369 370 371
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
372

A
afc163 已提交
373 374 375 376 377 378 379 380 381 382 383
  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 已提交
384 385 386
  },

  renderColumnsDropdown(columns) {
B
Benjy Cui 已提交
387
    let locale = objectAssign({}, defaultLocale, this.props.locale);
Y
yiminghe 已提交
388
    return columns.map((column, i) => {
Y
yiminghe 已提交
389
      column = objectAssign({}, column);
A
afc163 已提交
390
      let key = this.getColumnKey(column, i);
A
afc163 已提交
391
      let filterDropdown, sortButton;
392
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
393
        let colFilters = this.state.filters[key] || [];
A
afc163 已提交
394
        filterDropdown =
B
Benjy Cui 已提交
395
          <FilterDropdown locale={locale} column={column}
A
afc163 已提交
396
                          selectedKeys={colFilters}
Y
yiminghe 已提交
397
                          confirmFilter={this.handleFilter}/>;
A
afc163 已提交
398 399
      }
      if (column.sorter) {
A
afc163 已提交
400
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
401 402
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
403 404 405
          if (this.state.sortOrder) {
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
406
        }
B
Benjy Cui 已提交
407

A
afc163 已提交
408 409
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
410
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
A
afc163 已提交
411
                title="↑"
Y
yiminghe 已提交
412
                onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
Y
yiminghe 已提交
413
            <Icon type="caret-up"/>
A
afc163 已提交
414 415
          </span>
          <span className={'ant-table-column-sorter-down ' +
416
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
A
afc163 已提交
417
                title="↓"
Y
yiminghe 已提交
418
                onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
Y
yiminghe 已提交
419
            <Icon type="caret-down"/>
A
afc163 已提交
420 421 422
          </span>
        </div>;
      }
A
afc163 已提交
423 424 425 426 427
      column.title = <div>
        {column.title}
        {sortButton}
        {filterDropdown}
      </div>;
A
afc163 已提交
428
      return column;
A
afc163 已提交
429 430
    });
  },
A
afc163 已提交
431

432
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
433
    const pagination = this.state.pagination;
434
    pagination.onShowSizeChange(current, pageSize);
B
Benjy Cui 已提交
435 436

    let nextPagination = objectAssign(pagination, {
437 438
      pageSize: pageSize
    });
B
Benjy Cui 已提交
439
    this.setState({ pagination: nextPagination });
440 441
  },

442 443
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
444 445
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
446
    }
A
afc163 已提交
447 448 449 450
    let classString = classNames({
      'ant-table-pagination': true,
      'mini': this.props.size === 'middle' || this.props.size === 'small',
    });
A
afc163 已提交
451
    let total = this.state.pagination.total || this.getLocalData().length;
Y
yiminghe 已提交
452
    const pageSize = this.state.pagination.pageSize;
A
afc163 已提交
453
    return (total > 0) ?
B
Benjy Cui 已提交
454 455
      <Pagination {...this.state.pagination}
                  className={classString}
A
afc163 已提交
456 457
                  onChange={this.handlePageChange}
                  total={total}
Y
yiminghe 已提交
458
                  pageSize={pageSize}
B
Benjy Cui 已提交
459
                  onShowSizeChange={this.handleShowSizeChange} /> : null;
A
afc163 已提交
460
  },
A
afc163 已提交
461

Y
yiminghe 已提交
462
  prepareParamsArguments(state) {
463
    // 准备筛选、排序、分页的参数
464 465 466
    const pagination = state.pagination;
    const filters = state.filters;
    const sorter = {};
Y
yiminghe 已提交
467 468 469 470 471
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
A
afc163 已提交
472 473
    }
    return [pagination, filters, sorter];
474
  },
Y
yiminghe 已提交
475

A
afc163 已提交
476
  findColumn(myKey) {
Y
yiminghe 已提交
477 478 479 480 481
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
482
  getCurrentPageData(dataSource) {
A
afc163 已提交
483
    let data = this.getLocalData(dataSource);
Y
yiminghe 已提交
484 485 486 487 488 489
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
490
    } else {
Y
yiminghe 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
      pageSize = state.pagination.pageSize;
      current = state.pagination.current;
    }
    // 分页
    // ---
    // 当数据量少于每页数量时,直接设置数据
    // 否则进行读取分页数据
    if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
      data = data.filter((item, i) => {
        if (i >= (current - 1) * pageSize &&
          i < current * pageSize) {
          return item;
        }
      });
    }
    return data;
  },

A
afc163 已提交
509
  getLocalData(dataSource) {
Y
yiminghe 已提交
510
    let state = this.state;
A
afc163 已提交
511
    let data = dataSource || this.props.dataSource;
Y
yiminghe 已提交
512 513 514 515 516 517 518 519
    // 排序
    if (state.sortOrder && state.sorter) {
      data = data.sort(state.sorter);
    }
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
520 521 522
        if (!col) {
          return;
        }
Y
yiminghe 已提交
523
        let values = state.filters[columnKey] || [];
A
afc163 已提交
524 525 526
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
527 528 529
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
530
      });
A
afc163 已提交
531
    }
Y
yiminghe 已提交
532
    return data;
A
afc163 已提交
533
  },
Y
yiminghe 已提交
534 535 536 537

  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
Z
zhujun24 已提交
538
    let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
539
    let locale = objectAssign({}, defaultLocale, this.props.locale);
A
afc163 已提交
540 541 542 543 544 545 546

    let classString = classNames({
      [`ant-table-${this.props.size}`]: true,
      'ant-table-bordered': this.props.bordered,
      [this.props.className]: !!this.props.className,
    });

Y
yiminghe 已提交
547
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
548
    columns = columns.map((column, i) => {
A
afc163 已提交
549
      column.key = column.key || column.dataIndex || i;
A
afc163 已提交
550 551
      return column;
    });
A
afc163 已提交
552
    let emptyText;
553
    let emptyClass = '';
A
afc163 已提交
554
    if (!data || data.length === 0) {
555
      emptyText = <div className="ant-table-placeholder">
A
afc163 已提交
556
        <Icon type="frown"/>{locale.emptyText}
A
afc163 已提交
557
      </div>;
558
      emptyClass = ' ant-table-empty';
A
afc163 已提交
559
    }
A
afc163 已提交
560

A
afc163 已提交
561
    let table = <div>
562 563 564 565 566
      <Table {...this.props}
        data={data}
        columns={columns}
        className={classString}
        expandIconAsCell={expandIconAsCell} />
A
afc163 已提交
567 568
      {emptyText}
    </div>;
A
afc163 已提交
569
    if (this.props.loading) {
K
KgTong 已提交
570 571 572 573
      // if there is no pagination or no data, the height of spin should decrease by half of pagination
      let paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
              ? 'ant-table-with-pagination'
              : 'ant-table-without-pagination';
A
afc163 已提交
574
      let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
575
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
576
    }
577 578 579 580 581 582
    return (
      <div className={'clearfix' + emptyClass}>
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
583 584
  }
});
585

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