implement remote resolver

This commit is contained in:
AllenFang
2017-12-24 16:56:05 +08:00
parent 90bea38900
commit 4c89f91a83
2 changed files with 39 additions and 26 deletions

View File

@@ -11,46 +11,22 @@ import {
wrapWithPagination
} from './table-factory';
import remoteResolver from './props-resolver/remote-resolver';
import _ from './utils';
const withDataStore = Base =>
class BootstrapTableContainer extends Component {
class BootstrapTableContainer extends remoteResolver(Component) {
constructor(props) {
super(props);
this.store = new Store(props.keyField);
this.store.data = props.data;
this.handleUpdateCell = this.handleUpdateCell.bind(this);
this.handleRemotePageChange = this.handleRemotePageChange.bind(this);
this.handleRemoteFilterChange = this.handleRemoteFilterChange.bind(this);
}
componentWillReceiveProps(nextProps) {
this.store.data = nextProps.data;
}
getNewestState(state = {}) {
return {
page: this.store.page,
sizePerPage: this.store.sizePerPage,
filters: this.store.filters,
...state
};
}
handleRemotePageChange() {
this.props.onTableChange('pagination', this.getNewestState());
}
// refactoring later for isRemotePagination
handleRemoteFilterChange(isRemotePagination) {
const newState = {};
if (isRemotePagination) {
const options = this.props.pagination.options || {};
newState.page = _.isDefined(options.pageStartIndex) ? options.pageStartIndex : 1;
}
this.props.onTableChange('filter', this.getNewestState(newState));
}
handleUpdateCell(rowId, dataField, newValue) {
const { cellEdit } = this.props;
// handle cell editing internal

View File

@@ -0,0 +1,37 @@
import _ from '../utils';
export default ExtendBase =>
class RemoteResolver extends ExtendBase {
getNewestState(state = {}) {
const store = this.store || this.props.store;
return {
page: store.page,
sizePerPage: store.sizePerPage,
filters: store.filters,
...state
};
}
isRemotePagination() {
const { remote } = this.props;
return remote === true || (_.isObject(remote) && remote.pagination);
}
isRemoteFiltering() {
const { remote } = this.props;
return remote === true || (_.isObject(remote) && remote.filter);
}
handleRemotePageChange() {
this.props.onTableChange('pagination', this.getNewestState());
}
handleRemoteFilterChange() {
const newState = {};
if (this.isRemotePagination()) {
const options = this.props.pagination.options || {};
newState.page = _.isDefined(options.pageStartIndex) ? options.pageStartIndex : 1;
}
this.props.onTableChange('filter', this.getNewestState(newState));
}
};