im trying to insert 2 input fields to filter my json sourced datatable. But I have no success following the example following the custom filter example. Can anyone point me at the right direction? thanks so much!
I think the problem is that your iMin and iMax variables is set only once. So when 'keyup' event is fired, its still getting the data of iMin and iMax variables and not the current value of the two input fields.
Replies
Have you tried these? http://datatables.net/examples/server_side/custom_vars.html.
codingavenue
I made adjustment to my json generator file and it now parse the GET variables I pass it. One last question to make this perfect.
How do I redraw table on keypress in this case? the sample code:
[code]
/* Add event listeners to the two range filtering inputs */
$('#min').keyup( function() { oTable.fnDraw(); } );
$('#max').keyup( function() { oTable.fnDraw(); } );
[/code]
is not grabbing the new input field value...
Glad I could help. Could you post your js code?
codingavenue
[code]
var iMin = document.getElementById('min').value;
var iMax = document.getElementById('max').value;
var oTable = $('#data').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "list_processing.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
/* Add some extra data to the sender */
aoData.push( { "name": "start", "value": iMin, } );
aoData.push( { "name": "end", "value": iMax, } );
$.getJSON( sSource, aoData, function (json) {
/* Do whatever additional processing you want on the callback, then tell DataTables */
fnCallback(json)
} );
}
});
$('#min').keyup( function() { oTable.fnDraw(); } );
$('#max').keyup( function() { oTable.fnDraw(); } );
[/code]
and in the list_processing.php, i have
[code]
/* Filtering */
if( mysql_real_escape_string( $_GET['sSearch'] ) != "" AND $_GET['start'] != "" AND $_GET['end'] != ""){
$sWhere .= "WHERE (tbl_list_2.from LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
"tbl_list_2.to LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%') AND ".
"(tbl_list_2.date >= $_GET[start] && tbl_list_2.date <= $_GET[end])";
}
elseif ($_GET['start'] != "" OR $_GET['end'] != ""){
$sWhere .= "WHERE (tbl_list_2.date >= $_GET[start] && tbl_list_2.date <= $_GET[end])";
}elseif ( mysql_real_escape_string( $_GET['sSearch'] ) != "")
{
$sWhere .= "WHERE (tbl_list_2.from LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
"tbl_list_2.to LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%')";
}
[/code]
I think the problem is that your iMin and iMax variables is set only once. So when 'keyup' event is fired, its still getting the data of iMin and iMax variables and not the current value of the two input fields.
Hope that works.
codingavenue
[code]var iVersion = aData[0] == "-" ? 0 : aData[0]*1;[/code]
Having you tried your code without the custom filtering function?
codingavenue