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
};

A
afc163 已提交
22
let AntTable = React.createClass({
Y
yiminghe 已提交
23
  getInitialState() {
A
afc163 已提交
24
    return {
Y
yiminghe 已提交
25
      // 减少状态
26
      selectedRowKeys: this.props.selectedRowKeys || [],
Y
yiminghe 已提交
27
      filters: {},
A
afc163 已提交
28
      selectionDirty: false,
Y
yiminghe 已提交
29 30 31
      sortColumn: '',
      sortOrder: '',
      sorter: null,
R
RaoHai 已提交
32
      radioIndex: null,
Y
yiminghe 已提交
33
      pagination: this.hasPagination() ? objectAssign({
A
afc163 已提交
34 35
        pageSize: 10,
        current: 1
Y
yiminghe 已提交
36
      }, this.props.pagination) : {}
A
afc163 已提交
37 38
    };
  },
Y
yiminghe 已提交
39

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

A
afc163 已提交
56
  propTypes: {
A
afc163 已提交
57
    dataSource: React.PropTypes.array,
A
afc163 已提交
58 59 60 61 62 63 64 65 66
    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 已提交
67 68
  },

R
RaoHai 已提交
69
  getDefaultSelection() {
70 71
    if (!this.props.rowSelection || !this.props.rowSelection.getCheckboxProps) {
      return [];
R
RaoHai 已提交
72
    }
73 74 75
    return this.getCurrentPageData()
      .filter(item => this.props.rowSelection.getCheckboxProps(item).defaultChecked)
      .map((record, rowIndex) => this.getRecordKey(record, rowIndex));
R
RaoHai 已提交
76 77
  },

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

A
afc163 已提交
110
  hasPagination() {
A
afc163 已提交
111
    return this.props.pagination !== false;
A
afc163 已提交
112
  },
A
afc163 已提交
113

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

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

Y
yiminghe 已提交
170
  handleSelect(record, rowIndex, e) {
171 172
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
173
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
174
    let key = this.getRecordKey(record, rowIndex);
175
    if (checked) {
Y
yiminghe 已提交
176
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
177
    } else {
Y
yiminghe 已提交
178 179
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
180 181 182
      });
    }
    this.setState({
183
      selectionDirty: true,
R
RaoHai 已提交
184
    });
185
    this.setSelectedRowKeys(selectedRowKeys);
R
RaoHai 已提交
186 187 188 189 190 191 192 193 194 195
    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) {
196 197
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
198 199 200 201
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
202
      radioIndex: key,
203
      selectionDirty: true,
204
    });
205
    this.setSelectedRowKeys(selectedRowKeys);
206
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
207 208 209
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
210
      });
Y
yiminghe 已提交
211
      this.props.rowSelection.onSelect(record, checked, selectedRows);
212 213
    }
  },
A
afc163 已提交
214

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

249
  handlePageChange(current) {
Y
yiminghe 已提交
250
    let pagination = objectAssign({}, this.state.pagination);
251 252 253 254 255
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
256 257
    pagination.onChange(pagination.current);

A
afc163 已提交
258
    const newState = {
A
afc163 已提交
259
      selectionDirty: false,
A
afc163 已提交
260 261
      pagination
    };
A
afc163 已提交
262
    this.setState(newState);
263
    this.setSelectedRowKeys([]);
A
afc163 已提交
264
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
265
  },
A
afc163 已提交
266

R
RaoHai 已提交
267 268 269
  onRadioChange: function (ev) {
    this.setState({
      radioIndex: ev.target.value
Y
yiminghe 已提交
270
    });
271
  },
A
afc163 已提交
272

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

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

  getRecordKey(record, index) {
308 309 310
    if (this.props.rowKey) {
      return this.props.rowKey(record, index);
    }
Y
yiminghe 已提交
311
    return record.key || index;
312
  },
A
afc163 已提交
313

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

A
afc163 已提交
367 368 369 370 371 372 373 374 375 376 377
  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 已提交
378 379 380
  },

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

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

426
  handleShowSizeChange(current, pageSize) {
B
Benjy Cui 已提交
427 428 429 430 431 432
    const pagination = this.state.pagination;
    if (pagination.onShowSizeChange) {
      pagination.onShowSizeChange(current, pageSize);
    }

    let nextPagination = objectAssign(pagination, {
433 434
      pageSize: pageSize
    });
B
Benjy Cui 已提交
435
    this.setState({ pagination: nextPagination });
436 437
  },

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

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

A
afc163 已提交
472
  findColumn(myKey) {
Y
yiminghe 已提交
473 474 475 476 477
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
478
  getCurrentPageData(dataSource) {
A
afc163 已提交
479
    let data = this.getLocalData(dataSource);
Y
yiminghe 已提交
480 481 482 483 484 485
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
486
    } else {
Y
yiminghe 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
      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 已提交
505
  getLocalData(dataSource) {
Y
yiminghe 已提交
506
    let state = this.state;
A
afc163 已提交
507
    let data = dataSource || this.props.dataSource;
Y
yiminghe 已提交
508 509 510 511 512 513 514 515
    // 排序
    if (state.sortOrder && state.sorter) {
      data = data.sort(state.sorter);
    }
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
516 517 518
        if (!col) {
          return;
        }
Y
yiminghe 已提交
519
        let values = state.filters[columnKey] || [];
A
afc163 已提交
520 521 522
        if (values.length === 0) {
          return;
        }
A
afc163 已提交
523 524 525
        data = col.onFilter ? data.filter(record => {
          return values.some(v => col.onFilter(v, record));
        }) : data;
A
afc163 已提交
526
      });
A
afc163 已提交
527
    }
Y
yiminghe 已提交
528
    return data;
A
afc163 已提交
529
  },
Y
yiminghe 已提交
530 531 532 533

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

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

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

A
afc163 已提交
557
    let table = <div>
558 559 560 561 562
      <Table {...this.props}
        data={data}
        columns={columns}
        className={classString}
        expandIconAsCell={expandIconAsCell} />
A
afc163 已提交
563 564
      {emptyText}
    </div>;
A
afc163 已提交
565
    if (this.props.loading) {
K
KgTong 已提交
566 567 568 569
      // 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 已提交
570
      let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
571
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
572
    }
573 574 575 576 577 578
    return (
      <div className={'clearfix' + emptyClass}>
        {table}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
579 580
  }
});
581

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