php - DataTables columnFilter number-range filtering -
i have database in column "birth_date"
there date (for example 2015-06-26
). use datatables
display information users. want make number range search. when using datatables
plugin columnfilter
, try use number-range
type filter doesn't works.
as enter value from
or to
field tells me there no results. if in same column there row date written 20150626
filter shows it. understand problem in symbol -
in middle of number. how make filter ignore -
sign?
number-range filter code:
function fncreatecharrangeinput() { th.html(_fnrangelabelpart(0)); var sfromid = stableid + 'range_from_' + i; var = $('<input type="text" class="number_range_filter" id="' + sfromid + '" rel="' + + '"/>'); th.append(from); th.append(_fnrangelabelpart(1)); var stoid = stableid + 'range_to_' + i; var = $('<input type="text" class="number_range_filter" id="' + stoid + '" rel="' + + '"/>'); th.append(to); th.append(_fnrangelabelpart(2)); th.wrapinner('<span class="filtercolumn filter_number_range" />'); var index = i; aicustomsearch_indexes.push(i); //------------start range filtering function /* custom filtering function filter data in column 4 between 2 values * author: allan jardine, modified jovan popovic */ $.fn.datatableext.afnfiltering.push( function (osettings, adata, idataindex) { var imin = document.getelementbyid(sfromid).value * 1; var imax = document.getelementbyid(stoid).value * 1; var ivalue = adata[index] == "-" ? 0 : adata[index] * 1; if (imin == "" && imax == "") { return true; } else if (imin == "" && ivalue < imax) { return true; } else if (imin < ivalue && "" == imax) { return true; } else if (imin < ivalue && ivalue < imax) { return true; } return false; } ); //------------end range filtering function $('#' + sfromid + ',#' + stoid, th).keyup(function () { var imin = document.getelementbyid(sfromid).value * 1; var imax = document.getelementbyid(stoid).value * 1; if (imin != 0 && imax != 0 && imin > imax) return; otable.fndraw(); }); }
edit:2015-06-29
or maybe me make filter ignore input format run simple action example:
select * table birth_date between '2010' , '2011-12'
because query works fine in sql.
i have faced problem:
there might 2 reasons:
1) value of imin
,imax
, ivalue
must in seconds means in number of milliseconds since 1970/01/01
check gettime()
eg
var imin = document.getelementbyid(sfromid).value
because comparison (imin == "" && ivalue < imax)
using arithmetic operators (=,<,>
) value of these 3 variable must numeric.
2) please confirm first: guess date format 2015-06-26
have convert date format 2015/06/26
make work. not sure why in cases jquery doesn't accept 1970-01-01
, 1970/01/01
works perfectly.
have on function
$.fn.datatableext.afnfiltering.push( function(osettings, adata, idataindex) { if(chart.xmindate != '' && chart.xmaxdate != ''){ mindatefilter = new date( chart.xmindate.replace(/\-/g,'/') ).gettime(); maxdatefilter = new date( chart.xmaxdate.replace(/\-/g,'/') ).gettime(); adata._date = new date( adata[3].replace(/\-/g,'/') ).gettime(); if (mindatefilter) { if (adata._date < mindatefilter) { return false; } } if (maxdatefilter) { if (adata._date > maxdatefilter) { return false; } } return true; } return true; } );
in function
var imin = document.getelementbyid(sfromid).value * 1; /*same chart.xmindate */ var imax = document.getelementbyid(stoid).value * 1; /*same chart.xmaxdate */ var ivalue = adata[index] == "-" ? 0 : adata[index] * 1; /*same adata._date */
in line
mindatefilter = new date( chart.xmindate.replace(/\-/g,'/') ).gettime();
i have replaced -
/
after have created date object , have used gettime()
function on number of milliseconds since 1970/01/01
so guess(as have no idea html) you.
Comments
Post a Comment