diff --git a/src/BootstrapTable.js b/src/BootstrapTable.js index 514808b76..39540400f 100644 --- a/src/BootstrapTable.js +++ b/src/BootstrapTable.js @@ -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()); @@ -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); @@ -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) { @@ -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() }); @@ -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(); diff --git a/src/store/TableDataStore.js b/src/store/TableDataStore.js index d3864215c..903e3fd62 100644 --- a/src/store/TableDataStore.js +++ b/src/store/TableDataStore.js @@ -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) { @@ -176,7 +178,7 @@ export class TableDataStore { } } - search(searchText, multiColumnSearch) { + search(searchText) { if (searchText.trim() === "") { this.filteredData = null; this.isOnFilter = false; @@ -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); @@ -204,7 +206,7 @@ export class TableDataStore { } } return valid; - }); + }, this); this.isOnFilter = true; } }