Custom filtering
Custom filtering
m4k
Posts: 2Questions: 0Answers: 0
Hi,
I'm trying to make an custom function which will filter according to >, <, = or just a number. for example when we will search in a column with integers we can filter by greater than or less than sign.
The function is working the but I'm not able to keep the state for example while searching in two column filter happens from second.
I'm using jquery.dataTables.columnFilter.js from Jovan Popovic
Live example
http://www.indiavotes.com/nitipc/pcdetailslinlK/7225/27/2009
{edit}
result from debuger.
http://debug.datatables.net/ibawel
[code]
function fnCreateGtLtInput(oTable, regex, smart, bIsNumber, iFilterLength, iMaxLenght) {
sCSSClass = "number_filter";
label = label.replace(/(^\s*)|(\s*$)/g, "");
var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
var search_init = 'search_init ';
var inputvalue = label;
if (currentFilter != '' && currentFilter != '^') {
if (bIsNumber && currentFilter.charAt(0) == '^')
inputvalue = currentFilter.substr(1); //ignore trailing ^
else
inputvalue = currentFilter;
search_init = '';
}
var input = $('');
if (iMaxLenght != undefined && iMaxLenght != -1) {
input.attr('maxlength', iMaxLenght);
}
th.html(input);
th.wrapInner('');
asInitVals[i] = 'E.g. < 5';
var index = i;
oTable.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex){
//if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) {
var asdd = aData[_fnColumnIndex(index)];
var iMin = input[0].value;
var iValue = aData[_fnColumnIndex(index)].replace( /<.*?>/g, "" ).replace(/\,/g,"").replace(/\%/g,"");
iValue = iValue == "-" ? 0 : iValue.replace(/<.*?>/g, "").replace(/[,%]/g,'') * 1;
var cond = iMin.charAt(0);
if ( iMin == "" ){
//|| iMin.substr(1) == ''
return true;
}
if(cond == '>'){
var s = iMin.substr(1);
if (s < iValue) {
return true;
}
}else if(cond == '<'){
var s = iMin.substr(1);
if (s > iValue) {
return true;
}
}else if(cond == '='){
var s = iMin.substr(1);
if(s == iValue){
return true;
}
}else{
var re = new RegExp( "^" + iMin, "g");
if(String(iValue).match(re)){
//String(iValue).indexOf(iMin) !== -1 ||
return true;
}else if(iMin !== iValue){
return true;
}
else{
return false;
}
}
return false;
});
input.keyup(function (e) {
current_input = _fnColumnIndex(index);
oTable.fnDraw();
fnOnFiltered();
});
input.focus(function () {
if ($(this).hasClass("search_init")) {
$(this).removeClass("search_init");
this.value = "";
}
});
input.blur(function () {
if (this.value == "") {
$(this).addClass("search_init");
this.value = asInitVals[index];
}
});
}
[/code]
I'm trying to make an custom function which will filter according to >, <, = or just a number. for example when we will search in a column with integers we can filter by greater than or less than sign.
The function is working the but I'm not able to keep the state for example while searching in two column filter happens from second.
I'm using jquery.dataTables.columnFilter.js from Jovan Popovic
Live example
http://www.indiavotes.com/nitipc/pcdetailslinlK/7225/27/2009
{edit}
result from debuger.
http://debug.datatables.net/ibawel
[code]
function fnCreateGtLtInput(oTable, regex, smart, bIsNumber, iFilterLength, iMaxLenght) {
sCSSClass = "number_filter";
label = label.replace(/(^\s*)|(\s*$)/g, "");
var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
var search_init = 'search_init ';
var inputvalue = label;
if (currentFilter != '' && currentFilter != '^') {
if (bIsNumber && currentFilter.charAt(0) == '^')
inputvalue = currentFilter.substr(1); //ignore trailing ^
else
inputvalue = currentFilter;
search_init = '';
}
var input = $('');
if (iMaxLenght != undefined && iMaxLenght != -1) {
input.attr('maxlength', iMaxLenght);
}
th.html(input);
th.wrapInner('');
asInitVals[i] = 'E.g. < 5';
var index = i;
oTable.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex){
//if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) {
var asdd = aData[_fnColumnIndex(index)];
var iMin = input[0].value;
var iValue = aData[_fnColumnIndex(index)].replace( /<.*?>/g, "" ).replace(/\,/g,"").replace(/\%/g,"");
iValue = iValue == "-" ? 0 : iValue.replace(/<.*?>/g, "").replace(/[,%]/g,'') * 1;
var cond = iMin.charAt(0);
if ( iMin == "" ){
//|| iMin.substr(1) == ''
return true;
}
if(cond == '>'){
var s = iMin.substr(1);
if (s < iValue) {
return true;
}
}else if(cond == '<'){
var s = iMin.substr(1);
if (s > iValue) {
return true;
}
}else if(cond == '='){
var s = iMin.substr(1);
if(s == iValue){
return true;
}
}else{
var re = new RegExp( "^" + iMin, "g");
if(String(iValue).match(re)){
//String(iValue).indexOf(iMin) !== -1 ||
return true;
}else if(iMin !== iValue){
return true;
}
else{
return false;
}
}
return false;
});
input.keyup(function (e) {
current_input = _fnColumnIndex(index);
oTable.fnDraw();
fnOnFiltered();
});
input.focus(function () {
if ($(this).hasClass("search_init")) {
$(this).removeClass("search_init");
this.value = "";
}
});
input.blur(function () {
if (this.value == "") {
$(this).addClass("search_init");
this.value = asInitVals[index];
}
});
}
[/code]
This discussion has been closed.
Replies