index.jsx 18.2 KB
Newer Older
A
afc163 已提交
1
import React from 'react';
A
afc163 已提交
2
import reqwest from 'reqwest';
A
afc163 已提交
3
import Table from 'rc-table';
A
afc163 已提交
4
import Checkbox from '../checkbox';
R
RaoHai 已提交
5
import Radio from '../radio';
A
afc163 已提交
6
import FilterDropdown from './filterDropdown';
7
import Pagination from '../pagination';
E
elrrrrrrr 已提交
8
import Icon from '../iconfont';
A
afc163 已提交
9
import objectAssign from 'object-assign';
K
KgTong 已提交
10
import Spin from '../spin';
A
afc163 已提交
11

Y
yiminghe 已提交
12 13
function noop() {
}
14

Y
yiminghe 已提交
15 16 17
function defaultResolve(data) {
  return data || [];
}
18

19
class DataSource {
Y
yiminghe 已提交
20 21
  init(config) {
    this.config = config;
dqaria's avatar
dqaria 已提交
22
    this.url = config.url || '';
23 24 25
    this.resolve = config.resolve || defaultResolve;
    this.getParams = config.getParams || noop;
    this.getPagination = config.getPagination || noop;
A
afc163 已提交
26
    this.headers = config.headers || {};
A
afc163 已提交
27
    this.data = config.data || {};
Y
yiminghe 已提交
28 29 30 31 32 33 34 35
  }

  constructor(config) {
    if (config) {
      this.init(config);
    }
  }

36
  clone(config = {}) {
37
    return new DataSource(objectAssign({}, this.config, config));
38 39 40
  }
}

A
afc163 已提交
41
let AntTable = React.createClass({
Y
yiminghe 已提交
42
  getInitialState() {
A
afc163 已提交
43
    return {
Y
yiminghe 已提交
44
      // 减少状态
A
afc163 已提交
45
      selectedRowKeys: [],
Y
yiminghe 已提交
46 47
      // only for remote
      data: [],
48
      dataSource: this.props.dataSource,
Y
yiminghe 已提交
49
      filters: {},
R
RaoHai 已提交
50
      dirty: false,
51
      loading: false,
Y
yiminghe 已提交
52 53 54
      sortColumn: '',
      sortOrder: '',
      sorter: null,
R
RaoHai 已提交
55
      radioIndex: null,
Y
yiminghe 已提交
56
      pagination: this.hasPagination() ? objectAssign({
A
afc163 已提交
57 58
        pageSize: 10,
        current: 1
Y
yiminghe 已提交
59
      }, this.props.pagination) : {}
A
afc163 已提交
60 61
    };
  },
Y
yiminghe 已提交
62

A
afc163 已提交
63 64
  getDefaultProps() {
    return {
A
afc163 已提交
65
      prefixCls: 'ant-table',
A
afc163 已提交
66
      useFixedHeader: false,
A
afc163 已提交
67
      rowSelection: null,
Y
yiminghe 已提交
68
      className: '',
69
      size: 'default',
A
afc163 已提交
70
      bordered: false,
Y
yiminghe 已提交
71 72
      onChange: function () {
      }
A
afc163 已提交
73 74
    };
  },
Y
yiminghe 已提交
75

A
afc163 已提交
76
  propTypes: {
A
afc163 已提交
77
    dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
A
afc163 已提交
78 79
  },

R
RaoHai 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  getDefaultSelection() {
    let selectedRowKeys = [];
    if (this.props.rowSelection && this.props.rowSelection.getCheckboxProps) {
      let data = this.getCurrentPageData();
      data.filter((item) => {
        if (this.props.rowSelection.getCheckboxProps) {
          return this.props.rowSelection.getCheckboxProps(item).defaultValue;
        }
        return true;
      }).map((record, rowIndex) => {
        selectedRowKeys.push(this.getRecordKey(record, rowIndex));
      });
    }
    return selectedRowKeys;
  },

A
afc163 已提交
96
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
97
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
98 99 100
      this.setState({
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
      });
A
afc163 已提交
101
    }
102 103
    // 外界只有 dataSource 的变化会触发新请求
    if ('dataSource' in nextProps &&
Y
yiminghe 已提交
104
      nextProps.dataSource !== this.props.dataSource) {
105
      this.setState({
106 107 108
        selectedRowKeys: [],
        dataSource: nextProps.dataSource,
        loading: true
109
      }, this.fetch);
Y
yiminghe 已提交
110 111
    }
    if (nextProps.columns !== this.props.columns) {
112 113 114
      this.setState({
        filters: {}
      });
A
afc163 已提交
115 116
    }
  },
A
afc163 已提交
117 118

  hasPagination(pagination) {
Y
yiminghe 已提交
119 120
    if (pagination === undefined) {
      pagination = this.props.pagination;
A
afc163 已提交
121
    }
Y
yiminghe 已提交
122 123
    return pagination !== false;
  },
A
afc163 已提交
124 125

  isLocalDataSource() {
126
    return Array.isArray(this.state.dataSource);
A
afc163 已提交
127
  },
A
afc163 已提交
128 129

  getRemoteDataSource() {
130
    return this.state.dataSource;
A
afc163 已提交
131
  },
A
afc163 已提交
132

A
afc163 已提交
133
  toggleSortOrder(order, column) {
134 135
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
J
jljsj 已提交
136
    let sorter;
A
afc163 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
    // 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    let isSortColumn = this.isSortColumn(column);
    if (!isSortColumn) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
      }
    }
Y
yiminghe 已提交
150 151
    if (this.isLocalDataSource()) {
      sorter = function () {
152
        let result = column.sorter.apply(this, arguments);
153
        if (sortOrder === 'ascend') {
154
          return result;
155
        } else if (sortOrder === 'descend') {
156 157 158
          return -result;
        }
      };
A
afc163 已提交
159
    }
A
afc163 已提交
160 161 162 163 164 165 166
    const newState = {
      sortOrder,
      sortColumn,
      sorter
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
167
  },
A
afc163 已提交
168

Y
yiminghe 已提交
169 170 171 172
  handleFilter(column, filters) {
    filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: filters
    });
A
afc163 已提交
173
    const newState = {
Y
yiminghe 已提交
174
      selectedRowKeys: [],
A
afc163 已提交
175 176 177 178
      filters
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
A
afc163 已提交
179
  },
A
afc163 已提交
180

Y
yiminghe 已提交
181
  handleSelect(record, rowIndex, e) {
A
afc163 已提交
182
    let checked = e.target.checked;
R
RaoHai 已提交
183 184 185 186 187
    let defaultSelection = [];
    if (!this.state.dirty) {
      defaultSelection = this.getDefaultSelection();
    }
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
Y
yiminghe 已提交
188
    let key = this.getRecordKey(record, rowIndex);
189
    if (checked) {
Y
yiminghe 已提交
190
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
191
    } else {
Y
yiminghe 已提交
192 193
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
194 195 196
      });
    }
    this.setState({
R
RaoHai 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      selectedRowKeys: selectedRowKeys,
      dirty: true
    });
    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) {
    let checked = e.target.checked;
    let defaultSelection = [];
    if (!this.state.dirty) {
      defaultSelection = this.getDefaultSelection();
    }
    let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
    let key = this.getRecordKey(record, rowIndex);
    selectedRowKeys = [key];
    this.setState({
      selectedRowKeys: selectedRowKeys,
      radioIndex: record.key,
      dirty: true
222 223
    });
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
224 225 226
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
227
      });
Y
yiminghe 已提交
228
      this.props.rowSelection.onSelect(record, checked, selectedRows);
229 230
    }
  },
A
afc163 已提交
231

A
afc163 已提交
232 233
  handleSelectAllRow(e) {
    let checked = e.target.checked;
Y
yiminghe 已提交
234
    let data = this.getCurrentPageData();
R
RaoHai 已提交
235 236 237 238 239 240
    let selectedRowKeys = checked ? data.filter((item) => {
      if (this.props.rowSelection.getCheckboxProps) {
        return !this.props.rowSelection.getCheckboxProps(item).disabled;
      }
      return true;
    }).map((item, i) => {
Y
yiminghe 已提交
241 242
      return this.getRecordKey(item, i);
    }) : [];
A
afc163 已提交
243
    this.setState({
R
RaoHai 已提交
244 245
      selectedRowKeys: selectedRowKeys,
      dirty: true
246 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;
    }
A
afc163 已提交
262
    const newState = {
Y
yiminghe 已提交
263 264
      // 防止内存泄漏,只维持当页
      selectedRowKeys: [],
A
afc163 已提交
265 266 267 268
      pagination
    };
    this.fetch(newState);
    this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
269
  },
A
afc163 已提交
270

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

R
RaoHai 已提交
277 278 279 280 281 282 283
  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);
    }
    const checked = this.state.dirty ? this.state.radioIndex === record.key : this.getDefaultSelection().indexOf(rowIndex) >= 0;
Y
yiminghe 已提交
284 285
    return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
                  value={record.key} checked={checked}/>;
R
RaoHai 已提交
286 287
  },

288
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
289
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
R
RaoHai 已提交
290 291 292 293 294
    let checked = this.state.dirty ? this.state.selectedRowKeys.indexOf(rowIndex) >= 0 : this.getDefaultSelection().indexOf(rowIndex) >= 0;
    let props = {};
    if (this.props.rowSelection.getCheckboxProps) {
      props = this.props.rowSelection.getCheckboxProps.call(this, record);
    }
Y
yiminghe 已提交
295 296
    return <Checkbox checked={checked} disabled={props.disabled}
                     onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
Y
yiminghe 已提交
297
  },
A
afc163 已提交
298 299

  getRecordKey(record, index) {
Y
yiminghe 已提交
300
    return record.key || index;
301
  },
A
afc163 已提交
302

303
  renderRowSelection() {
Y
yiminghe 已提交
304
    let columns = this.props.columns.concat();
305
    if (this.props.rowSelection) {
Y
yiminghe 已提交
306 307 308 309 310
      let data = this.getCurrentPageData();
      let checked;
      if (!data.length) {
        checked = false;
      } else {
R
RaoHai 已提交
311 312 313 314 315 316
        checked = data.filter((item) => {
          if (this.props.rowSelection.getCheckboxProps) {
            return !this.props.rowSelection.getCheckboxProps(item).disabled;
          }
          return true;
        }).every((item, i) => {
Y
yiminghe 已提交
317 318 319 320
          let key = this.getRecordKey(item, i);
          return this.state.selectedRowKeys.indexOf(key) >= 0;
        });
      }
R
RaoHai 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
      let selectionColumn;
      if (this.props.rowSelection.type === 'radio') {
        selectionColumn = {
          key: 'selection-column',
          width: 60,
          render: this.renderSelectionRadio,
          className: 'ant-table-selection-column'
        };
      } else {
        let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow}/>;
        selectionColumn = {
          key: 'selection-column',
          title: checkboxAll,
          width: 60,
          render: this.renderSelectionCheckBox,
          className: 'ant-table-selection-column'
        };
      }
339
      if (columns[0] &&
Y
yiminghe 已提交
340
        columns[0].key === 'selection-column') {
341 342 343 344 345 346 347
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
348

A
afc163 已提交
349
  getCurrentPageData() {
Y
yiminghe 已提交
350 351 352
    return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
  },

A
afc163 已提交
353 354 355 356 357 358 359 360 361 362 363
  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 已提交
364 365 366
  },

  renderColumnsDropdown(columns) {
Y
yiminghe 已提交
367
    return columns.map((column, i) => {
Y
yiminghe 已提交
368
      column = objectAssign({}, column);
A
afc163 已提交
369
      let key = this.getColumnKey(column, i);
A
afc163 已提交
370
      let filterDropdown, sortButton;
371
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
372
        let colFilters = this.state.filters[key] || [];
A
afc163 已提交
373 374 375
        filterDropdown =
          <FilterDropdown column={column}
                          selectedKeys={colFilters}
Y
yiminghe 已提交
376
                          confirmFilter={this.handleFilter}/>;
A
afc163 已提交
377 378
      }
      if (column.sorter) {
A
afc163 已提交
379
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
380 381
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
382 383 384
          if (this.state.sortOrder) {
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
385
        }
A
afc163 已提交
386 387
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
388
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
Y
yiminghe 已提交
389 390
                title="升序排序"
                onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
Y
yiminghe 已提交
391
            <Icon type="caret-up"/>
A
afc163 已提交
392 393
          </span>
          <span className={'ant-table-column-sorter-down ' +
394
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
Y
yiminghe 已提交
395 396
                title="降序排序"
                onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
Y
yiminghe 已提交
397
            <Icon type="caret-down"/>
A
afc163 已提交
398 399 400
          </span>
        </div>;
      }
A
afc163 已提交
401 402 403 404 405
      column.title = <div>
        {column.title}
        {sortButton}
        {filterDropdown}
      </div>;
A
afc163 已提交
406
      return column;
A
afc163 已提交
407 408
    });
  },
A
afc163 已提交
409

410 411 412 413
  handleShowSizeChange(current, pageSize) {
    let pagination = objectAssign(this.state.pagination, {
      pageSize: pageSize
    });
Y
yiminghe 已提交
414
    this.fetch({pagination});
415 416
  },

417 418
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
419 420
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
421
    }
A
afc163 已提交
422 423 424 425
    let classString = 'ant-table-pagination';
    if (this.props.size === 'small') {
      classString += ' mini';
    }
A
afc163 已提交
426 427
    let total = this.state.pagination.total;
    if (!total && this.isLocalDataSource()) {
Y
yiminghe 已提交
428 429
      total = this.getLocalData().length;
    }
A
afc163 已提交
430
    return (total > 0) ? <Pagination className={classString}
Y
yiminghe 已提交
431 432 433 434
                                     onChange={this.handlePageChange}
                                     total={total}
                                     pageSize={10}
                                     onShowSizeChange={this.handleShowSizeChange}
A
afc163 已提交
435
      {...this.state.pagination} /> : null;
A
afc163 已提交
436
  },
A
afc163 已提交
437

Y
yiminghe 已提交
438
  prepareParamsArguments(state) {
439 440 441
    // 准备筛选、排序、分页的参数
    let pagination;
    let filters = {};
A
afc163 已提交
442
    let sorter = {};
Y
yiminghe 已提交
443 444 445 446 447
    pagination = state.pagination;
    this.props.columns.forEach((column) => {
      let colFilters = state.filters[this.getColumnKey(column)] || [];
      if (colFilters.length > 0) {
        filters[this.getColumnKey(column)] = colFilters;
448 449
      }
    });
Y
yiminghe 已提交
450 451 452 453 454
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
A
afc163 已提交
455 456
    }
    return [pagination, filters, sorter];
457
  },
Y
yiminghe 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470

  fetch(newState) {
    if (this.isLocalDataSource()) {
      if (newState) {
        this.setState(newState);
      }
    } else {
      let state = objectAssign({}, this.state, newState);
      if (newState || !this.state.loading) {
        this.setState(objectAssign({
          loading: true
        }, newState));
      }
A
afc163 已提交
471
      // remote 模式使用 this.dataSource
Y
yiminghe 已提交
472
      let dataSource = this.getRemoteDataSource();
A
afc163 已提交
473
      let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
474
      return reqwest({
475
        url: dataSource.url,
476
        method: 'get',
A
afc163 已提交
477
        data: objectAssign(buildInParams, dataSource.data),
A
afc163 已提交
478
        headers: dataSource.headers,
479
        type: 'json',
A
afc163 已提交
480 481
        success: (result) => {
          if (this.isMounted()) {
A
afc163 已提交
482
            let pagination = objectAssign(
Y
yiminghe 已提交
483
              state.pagination,
A
afc163 已提交
484 485
              dataSource.getPagination.call(this, result)
            );
A
afc163 已提交
486
            this.setState({
R
RaoHai 已提交
487
              dirty: false,
Y
yiminghe 已提交
488
              loading: false,
489
              data: dataSource.resolve.call(this, result),
Y
yiminghe 已提交
490
              pagination: pagination
A
afc163 已提交
491 492 493
            });
          }
        },
A
afc163 已提交
494
        error: () => {
A
afc163 已提交
495
          this.setState({
Y
yiminghe 已提交
496 497
            loading: false,
            data: []
A
afc163 已提交
498 499 500
          });
        }
      });
Y
yiminghe 已提交
501 502 503
    }
  },

A
afc163 已提交
504
  findColumn(myKey) {
Y
yiminghe 已提交
505 506 507 508 509
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
510
  getLocalDataPaging() {
Y
yiminghe 已提交
511 512 513 514 515 516 517
    let data = this.getLocalData();
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
518
    } else {
Y
yiminghe 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
      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 已提交
537
  getLocalData() {
Y
yiminghe 已提交
538
    let state = this.state;
539
    let data = this.state.dataSource;
Y
yiminghe 已提交
540 541 542 543 544 545 546 547 548
    // 排序
    if (state.sortOrder && state.sorter) {
      data = data.sort(state.sorter);
    }
    // 筛选
    if (state.filters) {
      Object.keys(state.filters).forEach((columnKey) => {
        let col = this.findColumn(columnKey);
        let values = state.filters[columnKey] || [];
A
afc163 已提交
549 550 551
        if (values.length === 0) {
          return;
        }
Y
yiminghe 已提交
552 553 554 555
        data = data.filter((record) => {
          return values.some((v)=> {
            return col.onFilter(v, record);
          });
556
        });
A
afc163 已提交
557
      });
A
afc163 已提交
558
    }
Y
yiminghe 已提交
559
    return data;
A
afc163 已提交
560
  },
Y
yiminghe 已提交
561

A
afc163 已提交
562
  componentDidMount() {
Y
yiminghe 已提交
563 564 565
    if (!this.isLocalDataSource()) {
      this.fetch();
    }
A
afc163 已提交
566
  },
567

Y
yiminghe 已提交
568 569 570
  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
Y
yiminghe 已提交
571
    let classString = this.props.className;
Z
zhujun24 已提交
572
    let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
573 574 575
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
576 577 578
    if (this.props.bordered) {
      classString += ' ant-table-bordered';
    }
Y
yiminghe 已提交
579
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
580 581 582 583
    columns = columns.map((column, i) => {
      column.key = column.dataIndex || i;
      return column;
    });
A
afc163 已提交
584
    let emptyText;
585
    let emptyClass = '';
A
afc163 已提交
586
    if (!data || data.length === 0) {
587
      emptyText = <div className="ant-table-placeholder">
Y
yiminghe 已提交
588
        <Icon type="frown"/>暂无数据
A
afc163 已提交
589
      </div>;
590
      emptyClass = ' ant-table-empty';
A
afc163 已提交
591
    }
A
afc163 已提交
592

593 594 595 596 597 598
    let table = (
      <Table {...this.props}
        data={data}
        columns={columns}
        className={classString}
        expandIconAsCell={expandIconAsCell} />
A
afc163 已提交
599
    );
K
KgTong 已提交
600 601 602 603 604
    if (this.state.loading && !this.isLocalDataSource()) {
      // 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 已提交
605
      let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
606
      table = <Spin className={spinClassName}>{table}</Spin>;
A
afc163 已提交
607
    }
608 609 610 611 612 613 614
    return (
      <div className={'clearfix' + emptyClass}>
        {table}
        {emptyText}
        {this.renderPagination()}
      </div>
    );
A
afc163 已提交
615 616
  }
});
617 618 619

AntTable.DataSource = DataSource;

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