Best practice to filter data based on external radio buttons
Best practice to filter data based on external radio buttons
![pharmonie](https://secure.gravatar.com/avatar/f2389935fbc4f86ec67e82a889cc6f83/?default=https%3A%2F%2Fvanillicon.com%2Ff2389935fbc4f86ec67e82a889cc6f83_200.png&rating=g&size=120)
Hi,
what is best practice to filter the data with radio buttons?
I am calling the datatable like:
var table = $('#tasks_table').DataTable({
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"processing": true,
"serverSide": true,
"ajax": {
"method": "POST",
"url": "<?=base_url();?>data_table",
"data": function ( d ) {
d.business = "<?=$settings->business;?>";
}
}
});
With the custom post data d.business
I use a WHERE business = $business
in MySQL.
The $settings->business;
is the value I want to filter.
I have this form prior to my table:
<form id="filter_business">
<p>
<span>Filter Business</span><br>
<label>
<input name="business" value="all" type="radio" <?=$settings->business=="all" ? 'checked' : '';?> />
<span>All </span>
</label>
<label>
<input name="business" value="systems_eu" type="radio" <?=$settings->business=="systems_eu" ? 'checked' : '';?> />
<span>Systems EU </span>
</label>
<label>
<input name="business" value="systems_na" type="radio" <?=$settings->business=="systems_na" ? 'checked' : '';?> />
<span>Systems NA </span>
</label>
<label>
<input name="business" value="fastener" type="radio" <?=$settings->business=="fastener" ? 'checked' : '';?> />
<span>Fastener </span>
</label>
</p>
</form>
I think about running a new query with new WHERE criteria and reload the table, but I think it makes more sence to load the enitre data and filter it later. And start with "all".
This question has an accepted answers - jump to answer
Answers
You are using server-side processing and passing the filter value to the server on each query, so all you need to do is call
draw()
whenever the radio button selection is changed.That said - you need to change
d.business = "<?=$settings->business;?>";
to be dynamic. At the moment it is static - you need to evaluate it whenever that function is run:should do it.
Allan
Sound good, but my updated code doesn't redraw, when I switch the radios:
I recognized two things: "Test" gets logged to the console and a "processing..." String appears in the middle of the table but stays and the table doesn't draw again.
Overall I recognize, that when I generate my table from a php array with a foreach loop, my pagination is working. When I switch to server-side, It loads all rows without limit, but the pagination buttons are there and do not work.
Thinking about that... I'd prefer a solution that doesn't use server-side ajax and I'd like to switch back to php array.
Does your
Test
message print there? And does thedata
function inside theajax
object execute?I don't really know what you mean I'm afraid.
Allan
Yes, they do.
I mean, that I load the entire data from MySQL and buld the table like
And call it like
The radio button form remains. How can I now redraw the datatable und filter on the business value?
Your server script is not returning data or there is an error. Check your browser's console.
You might not need server side processing (which requires the server script to perform sorting, searching and paging) but can use ajax with client side processing. How many rows do you have? You can try removing
serverSide: true
then usesearch
orcolumn().search()
instead ofdraw()
to search the data client side.Kevin
The recommendation with
column().search()
works, but applies the filter to the search input field. Thus I can't perfom another search.Is it possible to hide the rows instead of searching, so that I'd use the search for other values on that filter?
You can use a search plugin which will be independent of the
search()
andcolumn().search()
APIs. Here is an example:http://live.datatables.net/rosahuka/1/edit
It uses
draw()
to have the plugin run when the checkbox is clicked.Kevin
That's exactly what I'm looking for! Thank you!
Just one question and then I'm fully fine: How can I modify the code so that I can have one checkbox filter for multiple values?
In the given example, there will be one checkbox like
You can use something like Javascript includes(). Convert the values into an array then use something like
myArray.include(searchData[x]
wherex
is the column. You just need to use the Javascript methods that meet your requirement.Kevin
Thank you. That's working. Just for reference, if someone runs into the same needs, here is the working code: