index.jsx 18.4 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
    }
    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 已提交
112
    }
A
afc163 已提交
113
  },
A
afc163 已提交
114

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

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

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

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

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

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

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

A
afc163 已提交
267
    const newState = {
A
afc163 已提交
268
      selectionDirty: false,
A
afc163 已提交
269 270
      pagination
    };
A
afc163 已提交
271
    this.setState(newState);
272 273 274
    this.props.onChange.apply(this, this.prepareParamsArguments(
      objectAssign({}, this.state, newState)
    ));
275
  },
A
afc163 已提交
276

277
  onRadioChange(ev) {
R
RaoHai 已提交
278 279
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
280
    });
281
  },
A
afc163 已提交
282

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

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

  getRecordKey(record, index) {
323 324 325
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
326
    return record.key || index;
327
  },
A
afc163 已提交
328

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

A
afc163 已提交
386 387 388 389 390 391 392 393 394 395 396
  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 已提交
397 398 399
  },

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

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

451
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
452
    const pagination = this.state.pagination;
453
    pagination.onShowSizeChange(current, pageSize);
B
Benjy Cui 已提交
454 455

    let nextPagination = objectAssign(pagination, {
456
      pageSize,
457
    });
B
Benjy Cui 已提交
458
    this.setState({ pagination: nextPagination });
459 460
  },

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

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

A
afc163 已提交
495
  findColumn(myKey) {
Y
yiminghe 已提交
496 497 498 499 500
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

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

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

  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
Z
zhujun24 已提交
555
    let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
556
    let locale = objectAssign({}, defaultLocale, this.props.locale);
A
afc163 已提交
557 558 559 560 561 562 563

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

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

581 582 583
    let table = (
      <div>
        <Table {...this.props}
584 585 586 587
          data={data}
          columns={columns}
          className={classString}
          expandIconAsCell={expandIconAsCell} />
588 589 590
          {emptyText}
      </div>
    );
A
afc163 已提交
591
    if (this.props.loading) {
592 593
      // if there is no pagination or no data,
      // the height of spin should decrease by half of pagination
K
KgTong 已提交
594 595 596
      let paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
              ? 'ant-table-with-pagination'
              : 'ant-table-without-pagination';
A
afc163 已提交
597
      let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
598
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
599
    }
600
    return (
601
      <div className={`clearfix${emptyClass}`}>
602 603 604 605
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
606 607
  }
});
608

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