Skip to content

Commit

Permalink
fix #141
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenFang committed Nov 15, 2015
1 parent 31305a5 commit cf6a933
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
65 changes: 39 additions & 26 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,6 @@ class BootstrapTable extends React.Component {
super(props);

this._attachCellEditFunc();
let {keyField} = props;
let customSortFuncMap = {};

if (!(typeof keyField === 'string' && keyField.length)) {
React.Children.forEach(this.props.children, column=> {
if (column.props.isKey) {
if (keyField != null) {
throw "Error. Multiple key column be detected in TableHeaderColumn.";
}
keyField = column.props.dataField;
}
}, this);
}

React.Children.forEach(this.props.children, column=> {
if (column.props.sortFunc) {
customSortFuncMap[column.props.dataField] = column.props.sortFunc;
}
}, this);

if (keyField == null)
throw "Error. No any key column defined in TableHeaderColumn. Use 'isKey={true}' to specify an unique column after version 0.5.4.";

if (!Array.isArray(this.props.data)) {
this.store = new TableDataStore(this.props.data.getData());
Expand All @@ -51,7 +29,7 @@ class BootstrapTable extends React.Component {
this.store = new TableDataStore(this.props.data);
}

this.store.setProps(this.props.pagination, keyField, customSortFuncMap, this.isRemoteDataSource());
this.initTable(this.props);

if (this.props.selectRow && this.props.selectRow.selected) {
this.store.setSelectedRowKey(this.props.selectRow.selected);
Expand All @@ -63,6 +41,40 @@ class BootstrapTable extends React.Component {
};
}

initTable(props){
let {keyField} = props;
let customSortFuncMap = {};

if (!(typeof keyField === 'string' && keyField.length)) {
React.Children.forEach(props.children, column=> {
if (column.props.isKey) {
if (keyField != null) {
throw "Error. Multiple key column be detected in TableHeaderColumn.";
}
keyField = column.props.dataField;
}
}, this);
}

React.Children.forEach(props.children, column=> {
if (column.props.sortFunc) {
customSortFuncMap[column.props.dataField] = column.props.sortFunc;
}
}, this);

if (keyField == null)
throw "Error. No any key column defined in TableHeaderColumn."+
"Use 'isKey={true}' to specify an unique column after version 0.5.4.";

this.store.setProps({
isPagination:props.pagination,
keyField: keyField,
customSortFuncMap: customSortFuncMap,
multiColumnSearch: props.multiColumnSearch,
remote: this.isRemoteDataSource()
});
}

getTableData() {
let result = [];
if (this.props.pagination) {
Expand All @@ -82,10 +94,11 @@ class BootstrapTable extends React.Component {
}

componentWillReceiveProps(nextProps) {
this.initTable(nextProps);
if (Array.isArray(nextProps.data)) {
this.store.setData(nextProps.data);
this.store.page(this.props.options.page || 1,
this.props.options.sizePerPage || Const.SIZE_PER_PAGE_LIST[0]);
this.store.page(nextProps.options.page || 1,
nextProps.options.sizePerPage || Const.SIZE_PER_PAGE_LIST[0]);
this.setState({
data: this.getTableData()
});
Expand Down Expand Up @@ -415,7 +428,7 @@ class BootstrapTable extends React.Component {
}

handleSearch(searchText) {
this.store.search(searchText, this.props.multiColumnSearch);
this.store.search(searchText);
let result;
if (this.props.pagination) {
let sizePerPage = this.refs.pagination.getSizePerPage();
Expand Down
18 changes: 10 additions & 8 deletions src/store/TableDataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ export class TableDataStore {
this.sortObj = null;
this.pageObj = {};
this.selected = [];
this.multiColumnSearch = false;
this.remote = false; // remote data
}

setProps(isPagination, keyField, customSortFuncMap, remote) {
this.keyField = keyField;
this.enablePagination = isPagination;
this.customSortFuncMap = customSortFuncMap;
this.remote = remote;
setProps(props) {
this.keyField = props.keyField;
this.enablePagination = props.isPagination;
this.customSortFuncMap = props.customSortFuncMap;
this.remote = props.remote;
this.multiColumnSearch = props.multiColumnSearch;
}

setData(data) {
Expand Down Expand Up @@ -176,7 +178,7 @@ export class TableDataStore {
}
}

search(searchText, multiColumnSearch) {
search(searchText) {
if (searchText.trim() === "") {
this.filteredData = null;
this.isOnFilter = false;
Expand All @@ -187,7 +189,7 @@ export class TableDataStore {
this.filteredData = this.data.filter(function (row) {
let valid = false;

if (multiColumnSearch) {
if (this.multiColumnSearch) {
searchTextArray = searchText.split(' ');
} else {
searchTextArray.push(searchText);
Expand All @@ -204,7 +206,7 @@ export class TableDataStore {
}
}
return valid;
});
}, this);
this.isOnFilter = true;
}
}
Expand Down

0 comments on commit cf6a933

Please sign in to comment.