Change columnDefs:targets when radio button checked
Change columnDefs:targets when radio button checked
Hi all,
I would like to change the columnDefs : targets when the user clicks on one of two radio buttons.
The 1st radio button will change the "Search" to just use the 1st column where the ID's are stored. The 2nd radio button will re-enable the entire table search.
Here is what I have so far...
to initialize the DT:
var accounts_table = $('#grid').DataTable({ all my goodies in here which work });
then I have a listener:
var radios = document.forms["targetresolution"].elements["optradio"];
for (var i = 0, max = radios.length; i < max; i++) {
radios[i].onclick = function () {
if (this.value = "ByID") {
$('.accounts_table').dataTable({
"columnDefs": [
{ "targets": [0], "searchable": true },
{ "targets": '_all', "searchable": false }
]
});
}
else {
$('.accounts_table').dataTable({
"columnDefs": [
{ "targets": '_all', "searchable": true }
]
});
}
}
}
I can put an alert in the "if..." and each pops accordingly. I just dont know how to call the existing datatable in order to inject the new columndefs.
This question has accepted answers - jump to:
Answers
I made this a bit more simple:
Just need to know how to change columnDefs:targets with a listener.
Is this post correct from davidkonrad?
"There is no way that you can manipulate columnDefs after the dataTable is initialized."
https://stackoverflow.com/questions/37332416/how-to-define-columndefs-after-datatables-initialization
In order to change the Datatables options you would need to use destroy the Datatable, using either
destroy()
ordestroy
and re-init it with all the options and data. Don't think this is what you want.It sounds like you are only using the default search input and would like to affect what it searches with the checkbox. You will need to use a search plugin for this. The search plugin would be used when you check the box to search only the column. Otherwise the search will work normally.
Here is an example you can start from:
http://live.datatables.net/wecuvaho/1/edit
Kevin
Hello Kevin, thank you for the reply.
I went another direction with this and dont know if this post should be deleted?
Instead of destroying/recreating the table, I simply put a keyup event on the search. I then pass the value and redraw. I have two radio buttons in the HTML both named "optradio". One has a value of "ByID" and the other...well doesn't really matter.
The code:
This way, I simply check for the state of my "ID" radio button, if it is checked, pass the search value to the column([0,1]).
I hope I am not screwing anything up doing this!
I will also try your other suggestions.
You will need to change this to:
columns([0,1])
. Notice the plural columns. This search will be anAND
search between the two columns. If you want or then look at this thread:https://datatables.net/forums/discussion/comment/126687
Otherwise your search will work as an AND or if its just one column.
Kevin