I have database where in column "BIRTH_DATE" there is date (for example 2015-06-26). I use DataTables to display information for users. I want to make number range search. But when I am using DataTables plugin ColumnFilter and try to use number-range type filter it doesn't works.
As soon as I enter any value to from or to field it tells me that there are no results. But if in same column there is a row where date is written like that 20150626 filter shows it. So as I understand problem is in symbol - in middle of my number. How could I make filter ignore - sign?
Number-Range filter code:
function fnCreateCharRangeInput() {
th.html(_fnRangeLabelPart(0));
var sFromId = sTableId + 'range_from_' + i;
var from = $('<input type="text" class="number_range_filter" id="' + sFromId + '" rel="' + i + '"/>');
th.append(from);
th.append(_fnRangeLabelPart(1));
var sToId = sTableId + 'range_to_' + i;
var to = $('<input type="text" class="number_range_filter" id="' + sToId + '" rel="' + i + '"/>');
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 which will filter data in column four between two values
* Author: Allan Jardine, Modified by 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 somebody could help me to make this filter ignore input format just run simple action like for example:
Select * from table where BIRTH_DATE between '2010' and '2011-12'
Because query like that works fine in sql.
What is the type of your column
BIRTH_DATE?My advice would be to make it a
datetime(something looking like2015-06-26 16:10:18.820, although it can also be without the precise hour).Datatables can sort by
datatimeif you set your column type todate.See here for a more detailled description of column.type in DataTables https://datatables.net/reference/option/columns.type