Skip to content

Commit

Permalink
fix #354
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenFang committed Mar 25, 2016
1 parent 85b3e72 commit 239d8b1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 57 deletions.
55 changes: 22 additions & 33 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@ class BootstrapTable extends Component {
if (Util.canUseDOM()) {
this.isIE = document.documentMode;
}
if (!Array.isArray(this.props.data)) {
this.store = new TableDataStore(this.props.data.getData());
this.props.data.clear();
this.props.data.on('change', (data) => {
this.store.setData(data);
this.setState({
data: this.getTableData()
});
});
} else {
const copy = this.props.data.slice();
this.store = new TableDataStore(copy);
}

this.store = new TableDataStore(this.props.data.slice());

this.initTable(this.props);

Expand Down Expand Up @@ -145,27 +134,27 @@ class BootstrapTable extends Component {
componentWillReceiveProps(nextProps) {
this.initTable(nextProps);
const { options, selectRow } = nextProps;
if (Array.isArray(nextProps.data)) {
this.store.setData(nextProps.data.slice());
let page = options.page || this.state.currPage;
const sizePerPage = options.sizePerPage || this.state.sizePerPage;

// #125
if (!options.page &&
page >= Math.ceil(nextProps.data.length / sizePerPage)) {
page = 1;
}
const sortInfo = this.store.getSortInfo();
const sortField = options.sortName || (sortInfo ? sortInfo.sortField : undefined);
const sortOrder = options.sortOrder || (sortInfo ? sortInfo.order : undefined);
if (sortField && sortOrder) this.store.sort(sortOrder, sortField);
const data = this.store.page(page, sizePerPage).get();
this.setState({
data,
currPage: page,
sizePerPage
});

this.store.setData(nextProps.data.slice());
let page = options.page || this.state.currPage;
const sizePerPage = options.sizePerPage || this.state.sizePerPage;

// #125
if (!options.page &&
page >= Math.ceil(nextProps.data.length / sizePerPage)) {
page = 1;
}
const sortInfo = this.store.getSortInfo();
const sortField = options.sortName || (sortInfo ? sortInfo.sortField : undefined);
const sortOrder = options.sortOrder || (sortInfo ? sortInfo.order : undefined);
if (sortField && sortOrder) this.store.sort(sortOrder, sortField);
const data = this.store.page(page, sizePerPage).get();
this.setState({
data,
currPage: page,
sizePerPage
});

if (selectRow && selectRow.selected) {
// set default select rows to store.
const copy = selectRow.selected.slice();
Expand Down
5 changes: 1 addition & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import BootstrapTable from './BootstrapTable';
import TableHeaderColumn from './TableHeaderColumn';
import { TableDataSet } from './store/TableDataStore';

if (typeof window !== 'undefined') {
window.BootstrapTable = BootstrapTable;
window.TableHeaderColumn = TableHeaderColumn;
window.TableDataSet = TableDataSet;
}
export {
BootstrapTable,
TableHeaderColumn,
TableDataSet
TableHeaderColumn
};
20 changes: 0 additions & 20 deletions src/store/TableDataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* eslint no-console: 0 */
/* eslint eqeqeq: 0 */
import Const from '../Const';
const EventEmitter = require('events').EventEmitter;

function _sort(arr, sortField, order, sortFunc) {
order = order.toLowerCase();
Expand All @@ -22,25 +21,6 @@ function _sort(arr, sortField, order, sortFunc) {
return arr;
}

export class TableDataSet extends EventEmitter {
constructor(data) {
super(data);
this.data = data;
}

setData(data) {
this.emit('change', data);
}

clear() {
this.data = null;
}

getData() {
return this.data;
}
}

export class TableDataStore {

constructor(data) {
Expand Down

0 comments on commit 239d8b1

Please sign in to comment.