1

I am using jQuery DataTables and I would like to know how I can filter OUT rows of my table to show and hide them depending on the state of a checkbox. If my 'Hide' checkbox is checked then hide rows where class=var and if checkbox is not checked show rows where class=var

I have setup a small demo with the ability to hide/remove the rows I want but this doesn't allow the rows to reappear.

http://jsfiddle.net/bcraig/cY8Cn/2/

$('#stock').DataTable({
    "sDom": '',
    "infoEmpty": "No entries to show", 
    "aaSorting": [ ], 
    "aoColumnDefs": [{ "bSortable": false,  "aTargets": [ 0 ]}],
});
var oTable = $('#stock').DataTable();   

$('#hide').click(function() {
    if ($(this).is(':checked')) {
        oTable.row('.takenstock, .takensold').remove().draw(true);
        $('label').text("Show taken");
    } else {
        oTable.draw();
        $('label').text("Hide taken");
    }
});

1 Answer 1

1

You have to use the DataTables filters, I changed the code to be like below, you can check it in action in fiddle example:

http://jsfiddle.net/cY8Cn/4/

Also here you are the filtering documentation from DataTables: http://www.datatables.net/development/filtering

$('#stock').DataTable({
    "sDom": '',
    "infoEmpty": "No entries to show", 
    "aaSorting": [ ], 
    "aoColumnDefs": [{ "bSortable": false,  "aTargets": [ 0 ]}],
});
var oTable = $('#stock').DataTable();   

$.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
      var nTr = oSettings.aoData[ iDataIndex ].nTr;

      if (($(nTr).hasClass('takenstock') || $(nTr).hasClass('takensold')) 
          && $('#hide').is(':checked')) {
          return false;
      }
      else {
          return true;
      }
    }
);

$('#hide').click(function() {
    oTable.draw();

    if ($(this).is(':checked')) {
        $('label').text("Show taken");
    } else {
        $('label').text("Hide taken");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Unchecking the box and clicking Hide/Show Taken still results in the row behind hidden even after unchecking the box. Also only works with the first set of input after clicking Hide/Show taken

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.