javascript - Table filter not working with backspaces -
i'm working on reproducing live filter box handsontable based on built in search functionality @ http://docs.handsontable.com/0.15.0-beta6/demo-search-for-values.html.
right i've got basic setup working @ http://jsfiddle.net/ul3l4tel/4/
as explained in docs, in code if enter search string, matching cells outputted console using following function:
handsontable.dom.addevent(searchfiled, 'keyup', function (event) { var queryresult = hot.search.query(this.value); console.log(queryresult); // ... });
i want grab rows in data array match search string , filter original data 'data' search string before redisplaying table. partially works using:
handsontable.dom.addevent(searchfiled, 'keyup', function (event) { // debugger hot.loaddata(tdata); var queryresult = hot.search.query(this.value); rows = getrowsfromobjects(queryresult); console.log('searchfiled',searchfiled.value); console.log('rows',rows); console.log('tdata', tdata); var filtered = tdata.filter(function (d, ix) { return rows.indexof(ix) >= 0; }); console.log('filtered', filtered); hot.loaddata(filtered); });
however when run see following in console when enter 'n' followed backspacing (to empty searchstring):
enter 'n':
rows [0, 1] searchfiled n rows [0, 1] tdata [array[4], array[4], array[4], array[4]] filtered [array[4], array[4]]
backspace (to empty search value):
rows [] searchfiled rows [] tdata [array[4], array[4], array[4], array[4]] filtered []
how can fix empty string again show entire unfiltered table?
you add condition inside of .filter()
method return rows if value searchfiled.value
empty or undefined:
var filtered = tdata.filter(function(_, index) { if (searchfiled.value) { return rows.indexof(index) >= 0; } else { return true; } });
alternatively, here one-liner same thing (although less readable):
var filtered = tdata.filter(function(_, index) { return !searchfiled.value || rows.indexof(index) >= 0; });
Comments
Post a Comment