Custom Range Search - Server Side
Custom Range Search - Server Side
pettedemon
Posts: 38Questions: 6Answers: 0
Hi,
I create a server side table.
I add search on every column.
this is how I add the search
var table = $('#canzoni_player').DataTable( {
"processing": true,
"serverSide": true,
"autoWidth": false,
"ajax": "scripts/tabella_playlist.php",
"lengthMenu": [[18, 50,100, 250, 500, -1], [18, 50,100, 250, 500, "Tutti"]],
"language": {
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Italian.json"
},
initComplete: function() {
var api = this.api();
// Apply the search
api.columns([0, 1, 3, 4]).every(function() {
var that = this;
$('input', this.footer()).on('keyup change', function() {
if (that.search() !== this.value) {
that
.search(this.value)
.draw();
}
});
});
}
So I want to add a range filter only in one column
- I add the text input
<tbody><tr>
<td>Minimo BPM</td>
<td><input type="text" id="min" name="min"></td>
</tr>
<tr>
<td>Massimo BPM</td>
<td><input type="text" id="max" name="max"></td>
</tr>
</tbody>
I add the extended search for the column 3
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
var min = parseInt( $('#min').val(), 10 );
var max = parseInt( $('#max').val(), 10 );
var age = parseFloat( data[3] ) || 0; // use data for the age column
if ( ( isNaN( min ) && isNaN( max ) ) ||
( isNaN( min ) && age <= max ) ||
( min <= age && isNaN( max ) ) ||
( min <= age && age <= max ) )
{
return true;
}
return false;
}
);
So I have to add the code to initialize the search
$('#min, #max').keyup( function() {
table.columns( [3] )
table.search(this.value);
table.draw();
} );
I add this after
initComplete: function() {
var api = this.api();
but it doesn't work. It search on the column 1,2 and not a range
This discussion has been closed.
Replies
Hi @pettedemon ,
I'm guess you based the code on this example.
The problem with your code is in the
keyup
handler. You don't need lines 2 or 3 as in the example above, it only needs theapi draw()
. As the table is drawn, because of the search extension, it'll now check on the values of the two input cells.Hope that helps,
Cheers,
Colin
Hi @colin ,
I found a solution
here the min & max range
here the server-side
here the column search
So i have an other problem.
the min & max work , but not perfect, if I set min=110 & max=120 the system show me the result from 112...
Thanks
Hi,
so the problem is it search only the second value, 120, and in my case it found 112 and 120 because in these numbers there the "12" number...
So my custom range doesn't work....
Hi,
if I use only
it doesn't work
Looking at the above block of coder, you've got that
search()
on line 21 still. If you look at my first comment above, I was saying you don't need to have that.Cheers,
Colin
Hi colin,
I try comment the .search but it doesn't work.
At now with this code
`
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
var min = parseInt( $('#min').val(), 10 );
var max = parseInt( $('#max').val(), 10 );
var BPM = parseInt( data[3] ) || 0; // use data for the age column
`
it doesn't work.
So at the "keyup" on #min it search the min value, but when I type on #max it search the max value
Thanks
Hi,
maybe the problem is the server side function?
I try without server-side and it works, but with server side there is this problem.
thanks
The range search is a client side solution. For server side processing you need to have your server script perform the search and return the appropriate results.
Kevin
Hi,
so I cannot use the server side search, I have to do a custom ajax process with custom query?
Is there any example?
At now I use the array with the ssp.class.php
thanks
Hi,
I search and I found a solution
I do a custom function
then in my .php file I write a simple condition
Maybe is useful for someone.