Multiple Check boxes to update table
Multiple Check boxes to update table
Hi,
I am new datatable and need some help.
I have a table that loads correctly, but I want to put two check boxes that both will act on the same column.
When check box 1 is tick it hides some rows based on column x (say column 4), when check box 2 is checked is display some rows based on a another value in the same column (column 4).
The way i would like to operate is both check boxes will act on the same column say column 4:
if check box 1 is ticked, then check box 1needs be unchecked and the data as per check box 1 criteria is displayed.
if check box 2 is ticked, then check box 1needs be unchecked and the data as per check box 2 criteria is displayed.
Now I have this half working , second method (check box 2) is working but I cant't seem to get the check box 1 to behave the same way.
Below is the code I am using.
$(document).ready( function () {
.
.
.
.
.
$.fn.dataTable.ext.search.push(
function (settings, searchData, index, rowData, counter) {
var checkedcmp = $('input:checkbox[name="chk_boxcmp"]').is(':checked');
var checked = $('input:checkbox[name="chk_box"]').is(':checked');
if (checked && searchData[4] != '') {
document.getElementById("chk_boxcmp").checked = false;
//document.getElementById("chk_boxcmp").disabled = true;
return false;
}
if (checkedcmp && searchData[4] == '' ) {
document.getElementById("chk_box").checked = false;
//document.getElementById("chk_box").disabled = true;
return false;
}
// Otherwise display the row
return true;
});
var table = $('#stocktbl').DataTable();
$('input:checkbox').on('change', function () {
// Run the search plugin
table.draw();
}
);
} );
Any help will be appreciated.
Thanks
George
This question has accepted answers - jump to:
Answers
HI
I have created a sample here (clone the original one I used as a base)
url: http://live.datatables.net/fixuroja/1/edit
Thanks.
You don't want to update the checkboxes in the search plugin. That code is run for each row so it would be inefficient. The best place is to uncheck the other checkbox in the
change
event. For example:http://live.datatables.net/kehozene/1/edit
Kevin
Hi Kevin,
Thanks for that, it works like a charm.
Is there a way to set one of them as the default once the data has been loaded?
Thanks again.
George
Yes, you can use jQuery to set the checkbox, for example
$('#chk_box').prop('checked', true);
. If you set it before Datatables is initialized then it will take affect when the data is loaded. Here is the updated example:http://live.datatables.net/kehozene/2/edit
KEvin
Hi Kevin,
Thanks for this but it is not working on system.
The check box is checked but the table is not filters..
Below is the code that i am using, i do have a few custom buttons.
```Script Code:
You need to invoke the plugin code,
$.fn.dataTable.ext.search.push(...)
before initializing Datatables. Otherwise it won't be ready when Datatables initializes and won't be executed on table load. You can refer to the example to see this. Move it between lines 3 and 4.Kevin
Hi Kevin,
Thanks that worked, had to make a little adjustment.
Thanks for all your help with this learnt a lot from it.