[fnFilter] Display nothing if no checkboxes selected
[fnFilter] Display nothing if no checkboxes selected
tekuila
Posts: 24Questions: 5Answers: 0
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);
}
This discussion has been closed.
Answers
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
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
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?
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
@kthorngren I added 'checked' in the checkboxes so they're checked on pageload.
But how to make it work when unchecking just one checkbox?
Right now I get 0 results by unchecking one checkbox, but the others are checked.
http://live.datatables.net/fofimale/2/edit
The problem is the
filtered
array needs to have all the initial checked options.http://live.datatables.net/fofimale/3/edit
Kevin
Thanks, that works!