1

I'm using jQuery datatables. I'd like to hide all rows with class="hidden".

This code:

var table = $('#table1').DataTable();
table.rows('.hidden').hide();

It's not working (rows are not hidden), and I see this text in console:

table.rows(...).hide is not a function

How can I use class="hidden" to hide all rows?


HTML code:

<tr class="">
    <td>1</td>
    <td>ABC</td>
    <td>17</td>
</tr>

<tr class="hidden">
    <td>2</td>
    <td>DEF</td>
    <td>22</td>
</tr>

<tr class="">
    <td>3</td>
    <td>GHI</td>
    <td>55</td>
</tr>

<tr class="hidden">
    <td>4</td>
    <td>JKL</td>
    <td>11</td>
</tr>
<input id="hideRows" name="hideRows" type="checkbox">

JS code:

$('#table1').DataTable();

$('#hideRows').change(function() 
{
    var table = $('#table1').DataTable();
    $('tr.hidden').hide();
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
        if ($(oSettings.nTable).hasClass('hidden')) {
            return aData[16] == '' || $('#hideRows').is(':checked');
        } else return true;
    });
    table.draw();
});

1 Answer 1

2

The following solutions assume that the rows have already been marked with the hidden class.

To use DataTable filtering to hide the rows, implement a filter function, like this:

$.fn.dataTable.ext.search.push(
   function(settings, data, dataIndex) {
      return $(table.row(dataIndex).node()).hasClass('hidden');
   }
);

While it's not recommended, you can use jQuery to hide the rows, but there are gotchas. (There are valid reasons that you may need to do this, but they will not be covered here.) Simply query the hidden class of the HTML tr elements using a regular jQuery selector, and then apply .hide(), like this:

$('tr.hidden').hide();

If you use non-filter functions to hide the rows (either using the jQuery method above, or with CSS styling on the hidden class), there may be side effects. See jQuery DataTable - Hide rows the intended way for additional information. If you display / filtering issues, you'll have to let DataTables know which rows have already been hidden:

$.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
    if ($(oSettings.nTable).hasClass('hidden')) {
        return aData[16] == '' || $('#chkShowExcluded').is(':checked');
    } else return true;
});

Note that after using any of these methods, you may have to force a redraw after applying the hidden classes to the table rows, like this:

table.draw();
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Michael, Thanks for your answer. I've been trying to use your code, rows has been removed,but I still see old number of entries, even though table has been updated. Can you check my code ? -> wklej.org/id/2638128
It is very hard to read code included in a comment. Can you update your question with the information? If you click the edit link below the question, you can copy the code in there. It looks like you may have to include more code, as well, such as the code that adds the hidden class.
OK, I updated my first comment and i added link to code :-)
OK, for future reference, when someone on StackOverflow says "Question", they mean the original question that was asked, all the way at the top of the page. What you edited was a comment that happened to ask a follow-up question, but would still be called a comment. What I need to see is how the hidden class gets added to the rows and how the #hideRows id is used with the table. These are the keys to figuring out why the code isn't working, but they're only referenced here, so I can't tell how they were established.
You might want to take a look at How to create a Minimal, Complete, and Verifiable example to understand how to provide the right level of information to debug an issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.