index.jsx 10.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';
A
afc163 已提交
9

U
ustccjw 已提交
10
export default React.createClass({
A
afc163 已提交
11
  getInitialState() {
12 13 14 15 16 17 18
    // 支持两种模式
    if (Array.isArray(this.props.dataSource)) {
      this.mode = 'local';
      // 保留原来的数据
      this.originDataSource = this.props.dataSource.slice(0);
    } else {
      this.mode = 'remote';
A
afc163 已提交
19
      this.dataSource = objectAssign({
A
afc163 已提交
20
        resolve: function(data) {
21
          return data || [];
A
afc163 已提交
22 23 24 25
        },
        getParams: function() {},
        getPagination: function() {}
      }, this.props.dataSource);
26
    }
27 28

    let noPagination = (this.props.pagination === false);
A
afc163 已提交
29
    let pagination = this.initPagination(this.props.pagination);
30

A
afc163 已提交
31
    return {
A
afc163 已提交
32 33
      selectedRowKeys: [],
      loading: false,
34
      pagination: pagination,
35
      noPagination: noPagination,
A
afc163 已提交
36
      data: []
A
afc163 已提交
37 38
    };
  },
A
afc163 已提交
39 40
  getDefaultProps() {
    return {
A
afc163 已提交
41
      prefixCls: 'ant-table',
A
afc163 已提交
42
      useFixedHeader: false,
A
afc163 已提交
43 44
      rowSelection: null,
      size: 'normal'
A
afc163 已提交
45 46
    };
  },
A
afc163 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  componentWillReceiveProps(nextProps) {
    if ('pagination' in nextProps) {
      let noPagination = (nextProps.pagination === false);
      this.setState({
        pagination: this.initPagination(nextProps.pagination),
        noPagination: noPagination
      });
    }
  },
  initPagination(pagination) {
    return objectAssign({
      pageSize: 10,
      total: this.props.dataSource.length
    }, pagination);
  },
A
afc163 已提交
62
  toggleSortOrder(order, column) {
63 64
    let sortColumn = this.state.sortColumn;
    let sortOrder = this.state.sortOrder;
J
jljsj 已提交
65
    let sorter;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    // 同时允许一列进行排序,否则会导致排序顺序的逻辑问题
    if (sortColumn) {
      sortColumn.className = '';
    }
    if (sortColumn !== column) {  // 当前列未排序
      sortOrder = order;
      sortColumn = column;
      sortColumn.className = 'ant-table-column-sort';
    } else {                      // 当前列已排序
      if (sortOrder === order) {  // 切换为未排序状态
        sortOrder = '';
        sortColumn = null;
      } else {                    // 切换为排序状态
        sortOrder = order;
        sortColumn.className = 'ant-table-column-sort';
      }
A
afc163 已提交
82
    }
83
    if (this.mode === 'local') {
J
jljsj 已提交
84
      sorter = function() {
85
        let result = column.sorter.apply(this, arguments);
86
        if (sortOrder === 'ascend') {
87
          return result;
88
        } else if (sortOrder === 'descend') {
89 90 91
          return -result;
        }
      };
A
afc163 已提交
92
    }
A
afc163 已提交
93 94
    this.setState({
      sortOrder: sortOrder,
J
jljsj 已提交
95 96
      sortColumn: sortColumn,
      sorter: sorter
A
afc163 已提交
97
    }, this.fetch);
A
afc163 已提交
98
  },
A
afc163 已提交
99
  handleFilter(column) {
J
jljsj 已提交
100 101
    let columnIndex = this.props.columns.indexOf(column);
    let filterFns = [];
102
    if (this.mode === 'local') {
J
jljsj 已提交
103
      filterFns[columnIndex] = function(record) {
104 105 106 107 108 109
        if (column.selectedFilters.length === 0) {
          return true;
        }
        return column.selectedFilters.some(function(value) {
          return column.onFilter.call(this, value, record);
        });
J
jljsj 已提交
110
      };
111
    }
J
jljsj 已提交
112 113 114
    this.setState({
      filterFns: filterFns
    }, this.fetch);
A
afc163 已提交
115
  },
A
afc163 已提交
116 117
  handleSelect(rowIndex, e) {
    let checked = e.target.checked;
118
    if (checked) {
A
afc163 已提交
119
      this.state.selectedRowKeys.push(rowIndex);
120
    } else {
A
afc163 已提交
121
      this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i) {
A
afc163 已提交
122
        return rowIndex !== i;
123 124 125 126 127 128
      });
    }
    this.setState({
      selectedRowKeys: this.state.selectedRowKeys
    });
    if (this.props.rowSelection.onSelect) {
A
afc163 已提交
129 130 131 132 133
      let currentRow = this.state.data[rowIndex - 1];
      let selectedRows = this.state.data.filter((row, i) => {
        return this.state.selectedRowKeys.indexOf(i + 1) >= 0;
      });
      this.props.rowSelection.onSelect(currentRow, checked, selectedRows);
134 135
    }
  },
A
afc163 已提交
136 137 138
  handleSelectAllRow(e) {
    let checked = e.target.checked;
    let selectedRowKeys = checked ? this.state.data.map(function(item, i) {
139
        return i + 1;
A
afc163 已提交
140 141 142
      }) : [];
    this.setState({
      selectedRowKeys: selectedRowKeys
143 144
    });
    if (this.props.rowSelection.onSelectAll) {
A
afc163 已提交
145 146 147 148
      let selectedRows = this.state.data.filter((row, i) => {
        return selectedRowKeys.indexOf(i + 1) >= 0;
      });
      this.props.rowSelection.onSelectAll(checked, selectedRows);
A
afc163 已提交
149
    }
A
afc163 已提交
150
  },
151
  handlePageChange(current) {
A
afc163 已提交
152
    let pagination = this.state.pagination || {};
153 154 155 156 157
    if (current) {
      pagination.current = current;
    } else {
      pagination.current = pagination.current || 1;
    }
A
afc163 已提交
158 159 160
    this.setState({
      pagination: pagination
    }, this.fetch);
161 162
  },
  renderSelectionCheckBox(value, record, index) {
A
afc163 已提交
163 164
    let rowIndex = index + 1; // 从 1 开始
    let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
A
afc163 已提交
165
    return <Checkbox checked={checked} onChange={this.handleSelect.bind(this, rowIndex)} />;
166 167 168 169 170 171 172
  },
  renderRowSelection() {
    var columns = this.props.columns;
    if (this.props.rowSelection) {
      let checked = this.state.data.every(function(item, i) {
        return this.state.selectedRowKeys.indexOf(i + 1) >= 0;
      }, this);
A
afc163 已提交
173
      let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow} />;
174 175 176 177
      let selectionColumn = {
        key: 'selection-column',
        title: checkboxAll,
        width: 60,
A
afc163 已提交
178 179
        render: this.renderSelectionCheckBox,
        className: 'ant-table-selection-column'
180 181 182 183 184 185 186 187 188 189
      };
      if (columns[0] &&
          columns[0].key === 'selection-column') {
        columns[0] = selectionColumn;
      } else {
        columns.unshift(selectionColumn);
      }
    }
    return columns;
  },
A
afc163 已提交
190
  renderColumnsDropdown() {
191
    return this.props.columns.map((column) => {
A
afc163 已提交
192 193 194 195
      if (!column.originTitle) {
        column.originTitle = column.title;
      }
      let filterDropdown, menus, sortButton;
196
      if (column.filters && column.filters.length > 0) {
197
        column.selectedFilters = column.selectedFilters || [];
A
afc163 已提交
198
        menus = <FilterMenu column={column} confirmFilter={this.handleFilter.bind(this, column)} />;
A
afc163 已提交
199
        let dropdownSelectedClass = '';
200
        if (column.selectedFilters && column.selectedFilters.length > 0) {
A
afc163 已提交
201 202 203 204 205 206
          dropdownSelectedClass = 'ant-table-filter-selected';
        }
        filterDropdown = <Dropdown trigger="click"
          closeOnSelect={false}
          overlay={menus}>
          <i title="筛选" className={'anticon anticon-bars ' + dropdownSelectedClass}></i>
A
afc163 已提交
207 208 209
        </Dropdown>;
      }
      if (column.sorter) {
210
        let isSortColumn = (this.state.sortColumn === column);
A
afc163 已提交
211 212
        sortButton = <div className="ant-table-column-sorter">
          <span className={'ant-table-column-sorter-up ' +
213
                           ((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
A
afc163 已提交
214 215 216 217 218
            title="升序排序"
            onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
            <i className="anticon anticon-caret-up"></i>
          </span>
          <span className={'ant-table-column-sorter-down ' +
219
                           ((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
A
afc163 已提交
220 221 222 223 224 225 226 227 228 229 230
            title="降序排序"
            onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
            <i className="anticon anticon-caret-down"></i>
          </span>
        </div>;
      }
      column.title = [
        column.originTitle,
        sortButton,
        filterDropdown
      ];
A
afc163 已提交
231
      return column;
A
afc163 已提交
232 233
    });
  },
234 235
  renderPagination() {
    // 强制不需要分页
236
    if (this.state.noPagination) {
237
      return '';
A
afc163 已提交
238
    }
A
afc163 已提交
239 240 241 242 243
    let classString = 'ant-table-pagination';
    if (this.props.size === 'small') {
      classString += ' mini';
    }
    return <Pagination className={classString}
244 245
      onChange={this.handlePageChange}
      {...this.state.pagination} />;
A
afc163 已提交
246
  },
247 248 249 250
  prepareParamsArguments() {
    // 准备筛选、排序、分页的参数
    let pagination;
    let filters = {};
A
afc163 已提交
251
    let sorter = {};
252 253 254 255 256 257 258
    pagination = this.state.pagination;
    this.props.columns.forEach(function(column) {
      if (column.dataIndex && column.selectedFilters &&
          column.selectedFilters.length > 0) {
        filters[column.dataIndex] = column.selectedFilters;
      }
    });
A
afc163 已提交
259 260 261 262 263 264
    if (this.state.sortColumn && this.state.sortOrder &&
        this.state.sortColumn.dataIndex) {
      sorter.field = this.state.sortColumn.dataIndex;
      sorter.order = this.state.sortOrder;
    }
    return [pagination, filters, sorter];
265
  },
266
  fetch() {
267
    if (this.mode === 'remote') {
A
afc163 已提交
268 269
      // remote 模式使用 this.dataSource
      let dataSource = this.dataSource;
A
afc163 已提交
270 271 272 273
      this.setState({
        loading: true
      });
      jQuery.ajax({
274
        url: dataSource.url,
A
afc163 已提交
275 276 277
        data: dataSource.getParams.apply(this, this.prepareParamsArguments()) || {},
        headers: dataSource.headers,
        dataType: 'json',
A
afc163 已提交
278 279
        success: (result) => {
          if (this.isMounted()) {
A
afc163 已提交
280
            let pagination = objectAssign(
A
afc163 已提交
281 282 283
              this.state.pagination,
              dataSource.getPagination.call(this, result)
            );
A
afc163 已提交
284
            this.setState({
285
              data: dataSource.resolve.call(this, result),
A
afc163 已提交
286 287
              pagination: pagination,
              loading: false
A
afc163 已提交
288 289 290
            });
          }
        },
A
afc163 已提交
291
        error: () => {
A
afc163 已提交
292 293 294 295 296
          this.setState({
            loading: false
          });
        }
      });
297
    } else {
A
afc163 已提交
298
      let data = this.props.dataSource;
A
afc163 已提交
299 300
      let current, pageSize;
      // 如果没有分页的话,默认全部展示
301
      if (this.state.noPagination) {
302
        pageSize = Number.MAX_VALUE;
A
afc163 已提交
303 304 305 306 307
        current = 1;
      } else {
        pageSize = this.state.pagination.pageSize;
        current = this.state.pagination.current;
      }
J
jljsj 已提交
308 309 310 311 312 313 314 315 316 317 318
      // 排序
      if (this.state.sortOrder && this.state.sorter) {
        data = data.sort(this.state.sorter);
      } else {
        data = this.originDataSource.slice();
      }
      // 筛选
      if (this.state.filterFns) {
        this.state.filterFns.forEach(function(filterFn) {
          if (typeof filterFn === 'function') {
            data = data.filter(filterFn);
A
afc163 已提交
319
          }
J
jljsj 已提交
320 321 322 323 324 325 326 327 328 329 330 331
        });
      }
      // 分页
      data = data.filter(function(item, i) {
        if (i >= (current - 1) * pageSize &&
            i < current * pageSize) {
          return item;
        }
      });
      // 完成数据
      this.setState({
        data: data
A
afc163 已提交
332
      });
A
afc163 已提交
333 334 335
    }
  },
  componentDidMount() {
A
afc163 已提交
336
    this.handlePageChange();
A
afc163 已提交
337
  },
A
afc163 已提交
338
  render() {
339 340
    this.props.columns = this.renderRowSelection();

A
afc163 已提交
341
    var classString = '';
A
afc163 已提交
342
    if (this.state.loading) {
A
afc163 已提交
343 344 345 346 347
      classString += ' ant-table-loading';
    }
    if (this.props.size === 'small') {
      classString += ' ant-table-small';
    }
348 349 350 351

    return <div className="clearfix">
      <Table data={this.state.data}
      columns={this.renderColumnsDropdown()}
A
afc163 已提交
352
      className={classString}
353 354 355
      {...this.props} />
      {this.renderPagination()}
    </div>;
A
afc163 已提交
356 357
  }
});