code for filtering handsontables

2019-09-11 02:07发布

问题:

Please refer this question

ZekeDroid wrotes:

From here out to hide the non-matched rows, it gets tough. What I did was write a simple function that.

But grabbing the web for some hours I didn't found any code.

I will present just a readonly table but with many rows. And I have no idea how to write a plug-in by myself.

to be more specific: You discussed the filter problem on February with Micheael B and you wrote that you has wrote a little code using the search plugin. And it seems to me that Michael has used it with some problems.

And in turn it seems to me that Michael has access to your code, even there is no link in your answer. I'm searching the wwww (the whole world wide web) - but found nothing.

So my main question is: can you show me your code?

My request: I want to filter (shows only) the rows which match the search criteria. To colorize the matched cells is only a nice-to-have. Only colorizing as in the handsontable examples is not really meaningful if there are 2 hits and the table has 1000 rows.

回答1:

Refer to my question which is similar to this one but I also wanted to filter the column individually.

I did manage to code a solution that you will find in my own Q/A.

So in your case, if you want the code that hide the irrelevant row(s) based on one search criteria , the main function will look like this :

function filter(search) {
    var row, r_len;
    var data = myData;
    var array = [];
    for (row = 0, r_len = data.length; row < r_len; row++) {
        for(col = 0, c_len = data[row].length; col < c_len; col++) {
            if(('' + data[row][col]).toLowerCase().indexOf(search) > -1) {
                array.push(data[row]);
                break;
            }
        }
    }
    hot.loadData(array);
}

Find the complete solution in this JS Fiddle