index.jsx 12.9 KB
Newer Older
A
afc163 已提交
1
import React from 'react';
A
afc163 已提交
2
import jQuery from 'jquery';
A
afc163 已提交
3
import Table from 'rc-table';
A
afc163 已提交
4
import Dropdown from '../dropdown';
A
afc163 已提交
5
import Checkbox from '../checkbox';
A
afc163 已提交
6
import FilterMenu from './filterMenu';
7
import Pagination from '../pagination';
A
afc163 已提交
8
import objectAssign from 'object-assign';
Y
yiminghe 已提交
9
import equals from 'is-equal-shallow';
A
afc163 已提交
10

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

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

Y
yiminghe 已提交
18 19 20 21
function getColumnKey(col, index) {
  return col.key || col.dataIndex || index;
}

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

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

A
afc163 已提交
39 40 41 42 43 44
  clone(config) {
    if (config) {
      return new DataSource(objectAssign(config, this.config));
    } else {
      return this;
    }
45 46 47 48
  }
}

var AntTable = React.createClass({
Y
yiminghe 已提交
49
  getInitialState() {
A
afc163 已提交
50
    return {
Y
yiminghe 已提交
51
      // 减少状态
A
afc163 已提交
52
      selectedRowKeys: [],
Y
yiminghe 已提交
53 54 55 56 57 58 59 60 61 62
      // only for remote
      data: [],
      filters: {},
      loading: !this.isLocalDataSource(),
      sortColumn: '',
      sortOrder: '',
      sorter: null,
      pagination: this.hasPagination() ? objectAssign({
        pageSize: 10
      }, this.props.pagination) : {}
A
afc163 已提交
63 64
    };
  },
Y
yiminghe 已提交
65

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

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

A
afc163 已提交
80
  componentWillReceiveProps(nextProps) {
Y
yiminghe 已提交
81
    if (('pagination' in nextProps) && nextProps.pagination !== false) {
A
afc163 已提交
82
      this.setState({
Y
yiminghe 已提交
83
        pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
A
afc163 已提交
84 85
      });
    }
Y
yiminghe 已提交
86 87 88 89 90 91 92 93 94
    if (!this.isLocalDataSource()) {
      if (!equals(nextProps, this.props)) {
        this.setState({
          selectedRowKeys: [],
          loading: true
        }, this.fetch);
      }
    }
    if (nextProps.columns !== this.props.columns) {
A
afc163 已提交
95
      this.setState({
Y
yiminghe 已提交
96
        filters: {}
A
afc163 已提交
97 98 99
      });
    }
  },
A
afc163 已提交
100 101

  hasPagination(pagination) {
Y
yiminghe 已提交
102 103
    if (pagination === undefined) {
      pagination = this.props.pagination;
A
afc163 已提交
104
    }
Y
yiminghe 已提交
105 106
    return pagination !== false;
  },
A
afc163 已提交
107 108

  isLocalDataSource() {
Y
yiminghe 已提交
109
    return Array.isArray(this.props.dataSource);
A
afc163 已提交
110
  },
A
afc163 已提交
111 112

  getRemoteDataSource() {
Y
yiminghe 已提交
113
    return this.props.dataSource;
A
afc163 已提交
114
  },
A
afc163 已提交
115

A
afc163 已提交
116
  toggleSortOrder(order, column) {
117 118
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
J
jljsj 已提交
119
    let sorter;
Y
yiminghe 已提交
120 121
    if (this.isLocalDataSource()) {
      sorter = function () {
122
        let result = column.sorter.apply(this, arguments);
123
        if (sortOrder === 'ascend') {
124
          return result;
125
        } else if (sortOrder === 'descend') {
126 127 128
          return -result;
        }
      };
A
afc163 已提交
129
    }
Y
yiminghe 已提交
130
    this.fetch({
A
afc163 已提交
131
      sortOrder: sortOrder,
J
jljsj 已提交
132 133
      sortColumn: sortColumn,
      sorter: sorter
Y
yiminghe 已提交
134
    });
A
afc163 已提交
135
  },
A
afc163 已提交
136

Y
yiminghe 已提交
137 138 139 140 141 142 143 144
  handleFilter(column, filters) {
    filters = objectAssign({}, this.state.filters, {
      [this.getColumnKey(column)]: filters
    });
    this.fetch({
      selectedRowKeys: [],
      filters: filters
    });
A
afc163 已提交
145
  },
A
afc163 已提交
146

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

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

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

201
  renderSelectionCheckBox(value, record, index) {
Y
yiminghe 已提交
202
    let rowIndex = this.getRecordKey(record, index); // 从 1 开始
A
afc163 已提交
203
    let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
Y
yiminghe 已提交
204 205
    return <Checkbox checked={checked} onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
  },
A
afc163 已提交
206 207

  getRecordKey(record, index) {
Y
yiminghe 已提交
208
    return record.key || index;
209
  },
A
afc163 已提交
210

211
  renderRowSelection() {
Y
yiminghe 已提交
212
    let columns = this.props.columns.concat();
213
    if (this.props.rowSelection) {
Y
yiminghe 已提交
214 215 216 217 218 219 220 221 222 223 224
      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}/>;
225 226 227 228
      let selectionColumn = {
        key: 'selection-column',
        title: checkboxAll,
        width: 60,
A
afc163 已提交
229 230
        render: this.renderSelectionCheckBox,
        className: 'ant-table-selection-column'
231 232
      };
      if (columns[0] &&
Y
yiminghe 已提交
233
        columns[0].key === 'selection-column') {
234 235 236 237 238 239 240
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
Y
yiminghe 已提交
241

A
afc163 已提交
242
  getCurrentPageData() {
Y
yiminghe 已提交
243 244 245
    return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
  },

A
afc163 已提交
246
  getColumnKey(column) {
Y
yiminghe 已提交
247 248 249 250
    return column.key || column.dataIndex;
  },

  renderColumnsDropdown(columns) {
Y
yiminghe 已提交
251
    return columns.map((column, i) => {
Y
yiminghe 已提交
252
      column = objectAssign({}, column);
Y
yiminghe 已提交
253
      let key = this.getColumnKey(column);
A
afc163 已提交
254
      let filterDropdown, menus, sortButton;
255
      if (column.filters && column.filters.length > 0) {
Y
yiminghe 已提交
256 257
        let colFilters = this.state.filters[key] || [];
        menus = <FilterMenu column={column}
Y
yiminghe 已提交
258
                            selectedKeys={colFilters}
Y
yiminghe 已提交
259
                            confirmFilter={this.handleFilter}/>;
A
afc163 已提交
260
        let dropdownSelectedClass = '';
Y
yiminghe 已提交
261
        if (colFilters.length > 0) {
A
afc163 已提交
262 263 264
          dropdownSelectedClass = 'ant-table-filter-selected';
        }
        filterDropdown = <Dropdown trigger="click"
Y
yiminghe 已提交
265
                                   overlay={menus}>
A
afc163 已提交
266
          <i title="筛选" className={'anticon anticon-bars ' + dropdownSelectedClass}></i>
A
afc163 已提交
267 268 269
        </Dropdown>;
      }
      if (column.sorter) {
Y
yiminghe 已提交
270 271 272 273 274 275
        var colKey = getColumnKey(column, i);
        let isSortColumn = (getColumnKey(this.state.sortColumn, i) === colKey);
        if (isSortColumn) {
          column.className = column.className || '';
          column.className += ' ant-table-column-sort';
        }
A
afc163 已提交
276 277
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
278
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
Y
yiminghe 已提交
279 280
                title="升序排序"
                onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
A
afc163 已提交
281 282 283
            <i className="anticon anticon-caret-up"></i>
          </span>
          <span className={'ant-table-column-sorter-down ' +
284
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
Y
yiminghe 已提交
285 286
                title="降序排序"
                onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
A
afc163 已提交
287 288 289 290 291
            <i className="anticon anticon-caret-down"></i>
          </span>
        </div>;
      }
      column.title = [
Y
yiminghe 已提交
292
        column.title,
A
afc163 已提交
293 294 295
        sortButton,
        filterDropdown
      ];
A
afc163 已提交
296
      return column;
A
afc163 已提交
297 298
    });
  },
A
afc163 已提交
299

300 301
  renderPagination() {
    // 强制不需要分页
Y
yiminghe 已提交
302 303
    if (!this.hasPagination()) {
      return null;
A
afc163 已提交
304
    }
A
afc163 已提交
305 306 307 308
    let classString = 'ant-table-pagination';
    if (this.props.size === 'small') {
      classString += ' mini';
    }
Y
yiminghe 已提交
309 310 311 312
    let total;
    if (this.isLocalDataSource()) {
      total = this.getLocalData().length;
    }
A
afc163 已提交
313
    return <Pagination className={classString}
Y
yiminghe 已提交
314 315 316
                       onChange={this.handlePageChange}
                       total={total}
                       pageSize={10}
317
      {...this.state.pagination} />;
A
afc163 已提交
318
  },
A
afc163 已提交
319

Y
yiminghe 已提交
320
  prepareParamsArguments(state) {
321 322 323
    // 准备筛选、排序、分页的参数
    let pagination;
    let filters = {};
A
afc163 已提交
324
    let sorter = {};
Y
yiminghe 已提交
325 326 327 328 329
    pagination = state.pagination;
    this.props.columns.forEach((column) => {
      let colFilters = state.filters[this.getColumnKey(column)] || [];
      if (colFilters.length > 0) {
        filters[this.getColumnKey(column)] = colFilters;
330 331
      }
    });
Y
yiminghe 已提交
332 333 334 335 336
    if (state.sortColumn &&
      state.sortOrder &&
      state.sortColumn.dataIndex) {
      sorter.field = state.sortColumn.dataIndex;
      sorter.order = state.sortOrder;
A
afc163 已提交
337 338
    }
    return [pagination, filters, sorter];
339
  },
Y
yiminghe 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352

  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 已提交
353
      // remote 模式使用 this.dataSource
Y
yiminghe 已提交
354
      let dataSource = this.getRemoteDataSource();
A
afc163 已提交
355
      let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
A
afc163 已提交
356
      return jQuery.ajax({
357
        url: dataSource.url,
A
afc163 已提交
358
        data: objectAssign(buildInParams, dataSource.data),
A
afc163 已提交
359 360
        headers: dataSource.headers,
        dataType: 'json',
A
afc163 已提交
361 362
        success: (result) => {
          if (this.isMounted()) {
A
afc163 已提交
363
            let pagination = objectAssign(
Y
yiminghe 已提交
364
              state.pagination,
A
afc163 已提交
365 366
              dataSource.getPagination.call(this, result)
            );
A
afc163 已提交
367
            this.setState({
Y
yiminghe 已提交
368
              loading: false,
369
              data: dataSource.resolve.call(this, result),
Y
yiminghe 已提交
370
              pagination: pagination
A
afc163 已提交
371 372 373
            });
          }
        },
A
afc163 已提交
374
        error: () => {
A
afc163 已提交
375
          this.setState({
Y
yiminghe 已提交
376 377
            loading: false,
            data: []
A
afc163 已提交
378 379 380
          });
        }
      });
Y
yiminghe 已提交
381 382 383
    }
  },

A
afc163 已提交
384
  findColumn(myKey) {
Y
yiminghe 已提交
385 386 387 388 389
    return this.props.columns.filter((c) => {
      return this.getColumnKey(c) === myKey;
    })[0];
  },

A
afc163 已提交
390
  getLocalDataPaging() {
Y
yiminghe 已提交
391 392 393 394 395 396 397
    let data = this.getLocalData();
    let current, pageSize;
    let state = this.state;
    // 如果没有分页的话,默认全部展示
    if (!this.hasPagination()) {
      pageSize = Number.MAX_VALUE;
      current = 1;
398
    } else {
Y
yiminghe 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
      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 已提交
417
  getLocalData() {
Y
yiminghe 已提交
418 419 420 421 422 423 424 425 426 427 428
    let state = this.state;
    let data = this.props.dataSource;
    // 排序
    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 已提交
429 430 431
        if (values.length === 0) {
          return;
        }
Y
yiminghe 已提交
432 433 434 435
        data = data.filter((record) => {
          return values.some((v)=> {
            return col.onFilter(v, record);
          });
436
        });
A
afc163 已提交
437
      });
A
afc163 已提交
438
    }
Y
yiminghe 已提交
439
    return data;
A
afc163 已提交
440
  },
Y
yiminghe 已提交
441

A
afc163 已提交
442
  componentDidMount() {
Y
yiminghe 已提交
443 444 445
    if (!this.isLocalDataSource()) {
      this.fetch();
    }
A
afc163 已提交
446
  },
447

Y
yiminghe 已提交
448 449 450 451 452
  render() {
    let data = this.getCurrentPageData();
    let columns = this.renderRowSelection();
    let classString = '';
    if (this.state.loading && this.isLocalDataSource()) {
A
afc163 已提交
453 454 455 456 457
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
A
afc163 已提交
458 459 460
    if (this.props.bordered) {
      classString += ' ant-table-bordered';
    }
Y
yiminghe 已提交
461
    columns = this.renderColumnsDropdown(columns);
462
    return <div className="clearfix">
Y
yiminghe 已提交
463 464 465 466 467 468
      <Table
        {...this.props}
        data={data || []}
        columns={columns}
        className={classString}
        />
469 470
      {this.renderPagination()}
    </div>;
A
afc163 已提交
471 472
  }
});
473 474 475

AntTable.DataSource = DataSource;

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