Best way to hide all rows with no filtering

Best way to hide all rows with no filtering

wcmaneswcmanes Posts: 19Questions: 1Answers: 0

I have a page where I need to hide all rows of the table unless a filter condition is applied, then only the filtered rows should be displayed. If the filter is removed, all rows should be hidden again. Anyone know a simple way to do this?

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    Using a custom filter I think: http://legacy.datatables.net/development/filtering#row_filters (I'm still working on updating the plug-in dev docs for the new site...).

    You'd check to see if there is a filtering condition - if not, reject all rows. Otherwise, just return true and let the default operation work.

    Allan

  • wcmaneswcmanes Posts: 19Questions: 1Answers: 0

    Hi Allan,

    As always, thanks for your great software and support. Decided to get a little familiar with the new API. Below is what I came up with and it seems to work the way I want. Wondering if there could be any issues with this. Only global search is needed.

    zTable = $('#StuFindTbl').DataTable({ ... });
    var sNoMatch = "aabbccddeeff"; 
    zTable.search(sNoMatch).draw();
    zTable.on( 'search.dt', function () {
      if (zTable.search() == '') zTable.search(sNoMatch);
    });
    

    Bill

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    The conditional search will put the no match string into the table's global filter, but not actually apply the filter. In order to have it applied you need to call draw - but at that point it will do a filter on that data and give you no results.

    I think a custom filter is probably the best way to go.

    Allan

This discussion has been closed.