Hide rows from a array list

Hide rows from a array list

cce1911cce1911 Posts: 12Questions: 4Answers: 1

I have a datatable with rows in it. Column 1 contains the ID and I've added the rowId = ID during init. Elsewhere on the page, I generate an array of ID's that I want to hide. It's not obvious to me how I would go about iterating over the hideIDs array and hiding just those rows in the datatable. How do I loop over each row and hide it if the rowId is in the hideIDs array?

This seems like is should be easy, but I'm stuck. Please point me in the right direction...

Answers

  • cce1911cce1911 Posts: 12Questions: 4Answers: 1

    Here is what I came up with. Not sure if it's the most efficient way to do this, but it works for me.

    // restore all rows to the myTable
    $.fn.dataTable.ext.search.pop();
    myTable.draw();
    // filter the table
    $.fn.dataTable.ext.search.push(
        function(settings, data, dataIndex) {
            // need this if multiple tables are on the same page
            if(settings.sTableId != "my_table") return true;
            // rowId is set during init. Must add # for the selector
            var rid = '#'+ myTable.row(dataIndex).node().id;
            if(hideIDs.indexOf(rid) == -1 ){
                return true;
            } else {
                return false;
            }
        }
    );
    myTable.draw();
    
This discussion has been closed.