[fnFilter] Display nothing if no checkboxes selected

[fnFilter] Display nothing if no checkboxes selected

tekuilatekuila Posts: 24Questions: 5Answers: 0
edited July 2019 in Free community support

Right now my datatable is displaying all records, if no checkboxes are selected.

How to display no results, if no checkboxes are selected?

function filterCourses() {
  otable = $('#datatable').dataTable();
  //build a regex filter string with an or(|) condition
  var courses = $('input:checkbox[name="status"]:checked').map(function() {
    return '^' + this.value + '\$';
  }).get().join('|');
  //filter in column 0, with an regex, no smart filtering, no inputbox,not case sensitive
  otable.fnFilter(courses, 10, true, false, false, false);
}

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Hi @tekuila ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    My suggestion would be to use a Search Plugin. I adapted one of my examples to show one way to do this:
    http://live.datatables.net/ciyalayo/1/edit

    Kevin

  • tekuilatekuila Posts: 24Questions: 5Answers: 0
    edited July 2019

    Hi @kthorngren thanks, this is almost perfect!

    However, my client wants the checkboxes to be checked on page load, and if I do that in your example, then the checkboxes in your example have to be unchecked first in order to send the value in the array.

    How to make your code work if all checkboxes are checked on page load? :smile:

    • Jonas
  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    One way is to move the search plugin code into the initComplete option, like this:
    http://live.datatables.net/fofimale/1/edit

    This way its not used when the table loads but used for each search after.

    Kevin

  • tekuilatekuila Posts: 24Questions: 5Answers: 0
    edited July 2019

    @kthorngren I added 'checked' in the checkboxes so they're checked on pageload.
    But how to make it work when unchecking just one checkbox? :neutral:
    Right now I get 0 results by unchecking one checkbox, but the others are checked.

    http://live.datatables.net/fofimale/2/edit

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    The problem is the filtered array needs to have all the initial checked options.
    http://live.datatables.net/fofimale/3/edit

    Kevin

  • tekuilatekuila Posts: 24Questions: 5Answers: 0

    Thanks, that works! :)

This discussion has been closed.