index.jsx 14.2 KB
Newer Older
A
afc163 已提交
1
import React from 'react';
A
afc163 已提交
2
import reqwest from 'reqwest-without-xhr2';
A
afc163 已提交
3
import Table from 'rc-table';
A
afc163 已提交
4
import Checkbox from '../checkbox';
A
afc163 已提交
5
import FilterDropdown from './filterDropdown';
6
import Pagination from '../pagination';
A
afc163 已提交
7
import objectAssign from 'object-assign';
A
afc163 已提交
8

Y
yiminghe 已提交
9 10
function noop() {
}
11

Y
yiminghe 已提交
12 13 14
function defaultResolve(data) {
  return data || [];
}
15

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

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

33
  clone(config = {}) {
34
    return new DataSource(objectAssign({}, this.config, config));
35 36 37
  }
}

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

A
afc163 已提交
58 59
  getDefaultProps() {
    return {
A
afc163 已提交
60
      prefixCls: 'ant-table',
A
afc163 已提交
61
      useFixedHeader: false,
A
afc163 已提交
62
      rowSelection: null,
A
afc163 已提交
63 64
      size: 'normal',
      bordered: false
A
afc163 已提交
65 66
    };
  },
Y
yiminghe 已提交
67

A
afc163 已提交
68
  propTypes: {
A
afc163 已提交
69
    dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
A
afc163 已提交
70 71
  },

A
afc163 已提交
72
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
73
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
74 75 76
      this.setState({
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
      });
A
afc163 已提交
77
    }
78 79 80
    // 外界只有 dataSource 的变化会触发新请求
    if ('dataSource' in nextProps &&
        nextProps.dataSource !== this.props.dataSource) {
81
      this.setState({
82 83 84
        selectedRowKeys: [],
        dataSource: nextProps.dataSource,
        loading: true
85
      }, this.fetch);
Y
yiminghe 已提交
86 87
    }
    if (nextProps.columns !== this.props.columns) {
88 89 90
      this.setState({
        filters: {}
      });
A
afc163 已提交
91 92
    }
  },
A
afc163 已提交
93 94

  hasPagination(pagination) {
Y
yiminghe 已提交
95 96
    if (pagination === undefined) {
      pagination = this.props.pagination;
A
afc163 已提交
97
    }
Y
yiminghe 已提交
98 99
    return pagination !== false;
  },
A
afc163 已提交
100 101

  isLocalDataSource() {
102
    return Array.isArray(this.state.dataSource);
A
afc163 已提交
103
  },
A
afc163 已提交
104 105

  getRemoteDataSource() {
106
    return this.state.dataSource;
A
afc163 已提交
107
  },
A
afc163 已提交
108

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

Y
yiminghe 已提交
143 144 145 146 147 148 149 150
  handleFilter(column, filters) {
    filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: filters
    });
    this.fetch({
      selectedRowKeys: [],
      filters: filters
    });
A
afc163 已提交
151
  },
A
afc163 已提交
152

Y
yiminghe 已提交
153
  handleSelect(record, rowIndex, e) {
A
afc163 已提交
154
    let checked = e.target.checked;
Y
yiminghe 已提交
155 156
    let selectedRowKeys = this.state.selectedRowKeys.concat();
    let key = this.getRecordKey(record, rowIndex);
157
    if (checked) {
Y
yiminghe 已提交
158
      selectedRowKeys.push(this.getRecordKey(record, rowIndex));
159
    } else {
Y
yiminghe 已提交
160 161
      selectedRowKeys = selectedRowKeys.filter((i) => {
        return key !== i;
162 163 164
      });
    }
    this.setState({
Y
yiminghe 已提交
165
      selectedRowKeys: selectedRowKeys
166 167
    });
    if (this.props.rowSelection.onSelect) {
Y
yiminghe 已提交
168 169 170
      let data = this.getCurrentPageData();
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
171
      });
Y
yiminghe 已提交
172
      this.props.rowSelection.onSelect(record, checked, selectedRows);
173 174
    }
  },
A
afc163 已提交
175

A
afc163 已提交
176 177
  handleSelectAllRow(e) {
    let checked = e.target.checked;
Y
yiminghe 已提交
178 179 180 181
    let data = this.getCurrentPageData();
    let selectedRowKeys = checked ? data.map((item, i) => {
      return this.getRecordKey(item, i);
    }) : [];
A
afc163 已提交
182 183
    this.setState({
      selectedRowKeys: selectedRowKeys
184 185
    });
    if (this.props.rowSelection.onSelectAll) {
Y
yiminghe 已提交
186 187
      let selectedRows = data.filter((row, i) => {
        return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
A
afc163 已提交
188 189
      });
      this.props.rowSelection.onSelectAll(checked, selectedRows);
A
afc163 已提交
190
    }
A
afc163 已提交
191
  },
A
afc163 已提交
192

193
  handlePageChange(current) {
Y
yiminghe 已提交
194
    let pagination = objectAssign({}, this.state.pagination);
195 196 197 198 199
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
Y
yiminghe 已提交
200 201 202
    this.fetch({
      // 防止内存泄漏,只维持当页
      selectedRowKeys: [],
A
afc163 已提交
203
      pagination: pagination
Y
yiminghe 已提交
204
    });
205
  },
A
afc163 已提交
206

207
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
208
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
209
    let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
Y
yiminghe 已提交
210 211
    return <Checkbox checked={checked} onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
  },
A
afc163 已提交
212 213

  getRecordKey(record, index) {
Y
yiminghe 已提交
214
    return record.key || index;
215
  },
A
afc163 已提交
216

217
  renderRowSelection() {
Y
yiminghe 已提交
218
    let columns = this.props.columns.concat();
219
    if (this.props.rowSelection) {
Y
yiminghe 已提交
220 221 222 223 224 225 226 227 228 229 230
      let data = this.getCurrentPageData();
      let checked;
      if (!data.length) {
        checked = false;
      } else {
        checked = data.every((item, i) => {
          let key = this.getRecordKey(item, i);
          return this.state.selectedRowKeys.indexOf(key) >= 0;
        });
      }
      let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow}/>;
231 232 233 234
      let selectionColumn = {
        key: 'selection-column',
        title: checkboxAll,
        width: 60,
A
afc163 已提交
235 236
        render: this.renderSelectionCheckBox,
        className: 'ant-table-selection-column'
237 238
      };
      if (columns[0] &&
Y
yiminghe 已提交
239
        columns[0].key === 'selection-column') {
240 241 242 243 244 245 246
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
247

A
afc163 已提交
248
  getCurrentPageData() {
Y
yiminghe 已提交
249 250 251
    return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
  },

A
afc163 已提交
252 253 254 255 256 257 258 259 260 261 262
  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 已提交
263 264 265
  },

  renderColumnsDropdown(columns) {
Y
yiminghe 已提交
266
    return columns.map((column, i) => {
Y
yiminghe 已提交
267
      column = objectAssign({}, column);
A
afc163 已提交
268
      let key = this.getColumnKey(column, i);
A
afc163 已提交
269
      let filterDropdown, sortButton;
270
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
271
        let colFilters = this.state.filters[key] || [];
A
afc163 已提交
272 273 274 275
        filterDropdown =
          <FilterDropdown column={column}
                          selectedKeys={colFilters}
                          confirmFilter={this.handleFilter} />;
A
afc163 已提交
276 277
      }
      if (column.sorter) {
A
afc163 已提交
278
        let isSortColumn = this.isSortColumn(column);
Y
yiminghe 已提交
279 280
        if (isSortColumn) {
          column.className = column.className || '';
A
afc163 已提交
281 282 283
          if (this.state.sortOrder) {
            column.className += ' ant-table-column-sort';
          }
Y
yiminghe 已提交
284
        }
A
afc163 已提交
285 286
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
287
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
Y
yiminghe 已提交
288 289
                title="升序排序"
                onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
A
afc163 已提交
290 291 292
            <i className="anticon anticon-caret-up"></i>
          </span>
          <span className={'ant-table-column-sorter-down ' +
293
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
Y
yiminghe 已提交
294 295
                title="降序排序"
                onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
A
afc163 已提交
296 297 298 299
            <i className="anticon anticon-caret-down"></i>
          </span>
        </div>;
      }
A
afc163 已提交
300 301 302 303 304
      column.title = <div>
        {column.title}
        {sortButton}
        {filterDropdown}
      </div>;
A
afc163 已提交
305
      return column;
A
afc163 已提交
306 307
    });
  },
A
afc163 已提交
308

309 310 311 312 313 314 315
  handleShowSizeChange(current, pageSize) {
    let pagination = objectAssign(this.state.pagination, {
      pageSize: pageSize
    });
    this.fetch({ pagination });
  },

316 317
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
318 319
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
320
    }
A
afc163 已提交
321 322 323 324
    let classString = 'ant-table-pagination';
    if (this.props.size === 'small') {
      classString += ' mini';
    }
A
afc163 已提交
325 326
    let total = this.state.pagination.total;
    if (!total && this.isLocalDataSource()) {
Y
yiminghe 已提交
327 328
      total = this.getLocalData().length;
    }
A
afc163 已提交
329
    return (total > 0) ? <Pagination className={classString}
Y
yiminghe 已提交
330 331 332
                       onChange={this.handlePageChange}
                       total={total}
                       pageSize={10}
333
                       onShowSizeChange={this.handleShowSizeChange}
A
afc163 已提交
334
      {...this.state.pagination} /> : null;
A
afc163 已提交
335
  },
A
afc163 已提交
336

Y
yiminghe 已提交
337
  prepareParamsArguments(state) {
338 339 340
    // 准备筛选、排序、分页的参数
    let pagination;
    let filters = {};
A
afc163 已提交
341
    let sorter = {};
Y
yiminghe 已提交
342 343 344 345 346
    pagination = state.pagination;
    this.props.columns.forEach((column) => {
      let colFilters = state.filters[this.getColumnKey(column)] || [];
      if (colFilters.length > 0) {
        filters[this.getColumnKey(column)] = colFilters;
347 348
      }
    });
Y
yiminghe 已提交
349 350 351 352 353
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
A
afc163 已提交
354 355
    }
    return [pagination, filters, sorter];
356
  },
Y
yiminghe 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369

  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 已提交
370
      // remote 模式使用 this.dataSource
Y
yiminghe 已提交
371
      let dataSource = this.getRemoteDataSource();
A
afc163 已提交
372
      let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
373
      return reqwest({
374
        url: dataSource.url,
375
        method: 'get',
A
afc163 已提交
376
        data: objectAssign(buildInParams, dataSource.data),
A
afc163 已提交
377
        headers: dataSource.headers,
378
        type: 'json',
A
afc163 已提交
379 380
        success: (result) => {
          if (this.isMounted()) {
A
afc163 已提交
381
            let pagination = objectAssign(
Y
yiminghe 已提交
382
              state.pagination,
A
afc163 已提交
383 384
              dataSource.getPagination.call(this, result)
            );
A
afc163 已提交
385
            this.setState({
Y
yiminghe 已提交
386
              loading: false,
387
              data: dataSource.resolve.call(this, result),
Y
yiminghe 已提交
388
              pagination: pagination
A
afc163 已提交
389 390 391
            });
          }
        },
A
afc163 已提交
392
        error: () => {
A
afc163 已提交
393
          this.setState({
Y
yiminghe 已提交
394 395
            loading: false,
            data: []
A
afc163 已提交
396 397 398
          });
        }
      });
Y
yiminghe 已提交
399 400 401
    }
  },

A
afc163 已提交
402
  findColumn(myKey) {
Y
yiminghe 已提交
403 404 405 406 407
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
408
  getLocalDataPaging() {
Y
yiminghe 已提交
409 410 411 412 413 414 415
    let data = this.getLocalData();
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
416
    } else {
Y
yiminghe 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      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 已提交
435
  getLocalData() {
Y
yiminghe 已提交
436
    let state = this.state;
437
    let data = this.state.dataSource;
Y
yiminghe 已提交
438 439 440 441 442 443 444 445 446
    // 排序
    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 已提交
447 448 449
        if (values.length === 0) {
          return;
        }
Y
yiminghe 已提交
450 451 452 453
        data = data.filter((record) => {
          return values.some((v)=> {
            return col.onFilter(v, record);
          });
454
        });
A
afc163 已提交
455
      });
A
afc163 已提交
456
    }
Y
yiminghe 已提交
457
    return data;
A
afc163 已提交
458
  },
Y
yiminghe 已提交
459

A
afc163 已提交
460
  componentDidMount() {
Y
yiminghe 已提交
461 462 463
    if (!this.isLocalDataSource()) {
      this.fetch();
    }
A
afc163 已提交
464
  },
465

Y
yiminghe 已提交
466 467 468 469
  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
    let classString = '';
Z
zhujun24 已提交
470
    let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
A
afc163 已提交
471
    if (this.state.loading && !this.isLocalDataSource()) {
A
afc163 已提交
472 473 474 475 476
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
477 478 479
    if (this.props.bordered) {
      classString += ' ant-table-bordered';
    }
Y
yiminghe 已提交
480
    columns = this.renderColumnsDropdown(columns);
A
afc163 已提交
481 482 483 484
    columns = columns.map((column, i) => {
      column.key = column.dataIndex || i;
      return column;
    });
A
afc163 已提交
485
    let emptyText;
486
    let emptyClass = '';
A
afc163 已提交
487
    if (!data || data.length === 0) {
488
      emptyText = <div className="ant-table-placeholder">
A
afc163 已提交
489 490
        <i className="anticon anticon-frown"></i>暂无数据
      </div>;
491
      emptyClass = ' ant-table-empty';
A
afc163 已提交
492
    }
493
    return <div className={'clearfix' + emptyClass}>
Y
yiminghe 已提交
494 495
      <Table
        {...this.props}
A
afc163 已提交
496
        data={data}
Y
yiminghe 已提交
497 498
        columns={columns}
        className={classString}
Z
zhujun24 已提交
499
        expandIconAsCell={expandIconAsCell}
Y
yiminghe 已提交
500
        />
A
afc163 已提交
501
      {emptyText}
502 503
      {this.renderPagination()}
    </div>;
A
afc163 已提交
504 505
  }
});
506 507 508

AntTable.DataSource = DataSource;

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