I am building lightweight MS Excel based on React. I am currently building a filtering system that will allow filtering rows based on filter input - each one for each column. It works fine if you filter only through one column.
Problem: If I decide to filter based on more than just 1 column, the newest filtering takes precedence and ignore all previous ones like they weren't there.
I assume I know what the problem is. In current shape, my script always filters data stored in preSearch array which always stores the old original data (const). I tried to perform a loop through all filter inputs but I have failed to join all the conditions using && inside Array.prototype.filter() method.
After clicking the toggle search button this is generated:
showSearchInputs() {
if (this.state.toggleSearch === true) {
return (
<tr onChange={(e) => this._searchData(e)}>
{
this.props.headers.map((a, b) => {
return (
<td key={b}><input data-idx={b} type="text" style={{fontSize: '11px', width: '150px'}} /></td>
)
})
}
</tr>
)
}
else {
return null
}
}
And then there is a method that listens for data change:
_searchData(e) {
console.log(e.target.dataset.idx, e.target.value);
if (!e.target.value.toLowerCase()) {
//restore full old data
this.setState({
data: this.preSearch
})
return
}
else {
var idx = e.target.dataset.idx
var searchdata = this.preSearch.filter(row => {
return row[idx].toString().toLowerCase().indexOf(e.target.value.toLowerCase()) > -1
})
this.setState({
data: searchdata,
})
}
}
Expected result: Filters overlapping (accumulating), currently the newest filter takes precedence Removing characters from the filter inputs also updates the result and aiming to the 0 filters and original data to be displayed (if the user removes all filters naturally)