Range filter doesn't work..
Range filter doesn't work..
KimBruil
Posts: 4Questions: 2Answers: 0
Hey guys,
I've typed some javascript.. But I need a range filter for star ratings. It isn't working. Can you people see what is wrong with it??
/* Custom filtering function which will search data in column four between two values */
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
// var min = parseInt($('#min').val(), 10);
// var max = parseInt($('#max').val(), 10);
var min = parseInt($('#min').val(), 10);
var max = parseInt($('#max').val(), 10);
var age = parseFloat(data[3]) || 0; // use data for the star column
if ((isNaN(min) && isNaN(max)) ||
(isNaN(min) && age <= max) ||
(min <= age && isNaN(max)) ||
(min <= age && age <= max)) {
return true;
}
return false;
}
);
var table = $('#example').DataTable(
{
"processing": true,
"serverSide": true,
"bStateSave": true,
"select": true,
"ajax": "{{ route('datatable-beoordeling') }}",
"columns": [
{data: 'voornaam'}
{data: 'naam'},
{data: 'titel'},
{data: 'rating'}
{data: 'comment'},
// {data: 'id'},
],
});
$('#min, #max').keyup(function () {
table.draw();
});
This discussion has been closed.
Answers
I'm guessing the problem is with this line:
var age = parseFloat(data[3]) || 0; // use data for the star column
I suspect of you look in the browser's console you will see an error pointing to this line. Since you are using
columns.data
you need to access your data as an object not an array. Try changing the line to this:var age = parseFloat(data.rating) || 0; // use data for the star column
If this doesn't help then we will need to see your page or a test case replicating the issue to help diagnose.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
No this didn't work.
Hi @KimBruil ,
You're going to have to help us to help you - in what way didn't it work!?
We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. 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