index.jsx 18.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
};

A
afc163 已提交
22
let AntTable = React.createClass({
Y
yiminghe 已提交
23
  getInitialState() {
A
afc163 已提交
24
    return {
Y
yiminghe 已提交
25
      // 减少状态
A
afc163 已提交
26
      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 85
    // 外界只有 dataSource 的变化会触发新请求
    if ('dataSource' in nextProps &&
A
afc163 已提交
86
        nextProps.dataSource !== this.props.dataSource) {
87
      this.setState({
A
afc163 已提交
88
        selectionDirty: false,
89
        selectedRowKeys: [],
A
afc163 已提交
90
      });
A
afc163 已提交
91 92 93
      if (this.props.rowSelection && this.props.rowSelection.onChange) {
        this.props.rowSelection.onChange([]);
      }
A
afc163 已提交
94
    }
A
afc163 已提交
95
  },
A
afc163 已提交
96

A
afc163 已提交
97
  hasPagination() {
A
afc163 已提交
98
    return this.props.pagination !== false;
A
afc163 已提交
99
  },
A
afc163 已提交
100

A
afc163 已提交
101
  toggleSortOrder(order, column) {
102 103
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
J
jljsj 已提交
104
    let sorter;
A
afc163 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
A
afc163 已提交
118
    if (typeof column.sorter === 'function') {
Y
yiminghe 已提交
119
      sorter = function () {
120
        let result = column.sorter.apply(this, arguments);
121
        if (sortOrder === 'ascend') {
122
          return result;
123
        } else if (sortOrder === 'descend') {
124 125 126
          return -result;
        }
      };
A
afc163 已提交
127
    }
A
afc163 已提交
128 129 130 131 132
    const newState = {
      sortOrder,
      sortColumn,
      sorter
    };
A
afc163 已提交
133
    this.setState(newState);
A
afc163 已提交
134
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
135
  },
A
afc163 已提交
136

Y
yiminghe 已提交
137 138 139 140
  handleFilter(column, filters) {
    filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: filters
    });
141 142 143 144 145 146 147
    // 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 已提交
148
    const newState = {
Y
yiminghe 已提交
149
      selectedRowKeys: [],
A
afc163 已提交
150
      selectionDirty: false,
A
afc163 已提交
151 152
      filters
    };
A
afc163 已提交
153
    this.setState(newState);
A
afc163 已提交
154
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
155
  },
A
afc163 已提交
156

Y
yiminghe 已提交
157
  handleSelect(record, rowIndex, e) {
158 159
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
160
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
161
    let key = this.getRecordKey(record, rowIndex);
162
    if (checked) {
Y
yiminghe 已提交
163
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
164
    } else {
Y
yiminghe 已提交
165 166
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
167 168 169
      });
    }
    this.setState({
A
afc163 已提交
170
      selectedRowKeys,
A
afc163 已提交
171
      selectionDirty: true
R
RaoHai 已提交
172 173 174 175 176 177 178 179
    });
    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);
    }
A
afc163 已提交
180 181 182
    if (this.props.rowSelection.onChange) {
      this.props.rowSelection.onChange(selectedRowKeys);
    }
R
RaoHai 已提交
183 184 185
  },

  handleRadioSelect: function (record, rowIndex, e) {
186 187
    const checked = e.target.checked;
    const defaultSelection = this.state.selectionDirty ? [] : this.getDefaultSelection();
R
RaoHai 已提交
188 189 190 191
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
A
afc163 已提交
192
      selectedRowKeys,
193
      radioIndex: key,
A
afc163 已提交
194
      selectionDirty: true
195 196
    });
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
197 198 199
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
200
      });
Y
yiminghe 已提交
201
      this.props.rowSelection.onSelect(record, checked, selectedRows);
202
    }
A
afc163 已提交
203 204 205
    if (this.props.rowSelection.onChange) {
      this.props.rowSelection.onChange(selectedRowKeys);
    }
206
  },
A
afc163 已提交
207

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

245
  handlePageChange(current) {
Y
yiminghe 已提交
246
    let pagination = objectAssign({}, this.state.pagination);
247 248 249 250 251
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
A
afc163 已提交
252
    const newState = {
Y
yiminghe 已提交
253 254
      // 防止内存泄漏,只维持当页
      selectedRowKeys: [],
A
afc163 已提交
255
      selectionDirty: false,
A
afc163 已提交
256 257
      pagination
    };
A
afc163 已提交
258
    this.setState(newState);
A
afc163 已提交
259 260 261
    if (this.props.rowSelection && this.props.rowSelection.onChange) {
      this.props.rowSelection.onChange([]);
    }
A
afc163 已提交
262
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
263
  },
A
afc163 已提交
264

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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