Skip to content

Commit

Permalink
Merge pull request #1 from renattomachado/master
Browse files Browse the repository at this point in the history
Fix set up data array for work with ajax source in datatables.
  • Loading branch information
pvblicltd committed Oct 30, 2015
2 parents 92f34ab + abe087c commit 84f5a37
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 94 deletions.
10 changes: 7 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-datatables-column-filter",
"version": "0.0.1",
"name": "jquery-datatables-columnfilter",
"version": "0.0.6",
"homepage": "https://code.google.com/p/jquery-datatables-column-filter/",
"authors": [
"Jovan Popovic"
Expand All @@ -19,5 +19,9 @@
"bower_components",
"test",
"tests"
]
],
"dependencies": {
"datatables": "1.10.7",
"select2": "4.0.0"
}
}
182 changes: 91 additions & 91 deletions jquery.dataTables.columnFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
var asInitVals, i, label, th;

//var sTableId = "table";
var sRangeFormat = "From {from} to {to}";
var sRangeFormat = "De {from} a {to}";
//Array of the functions that will override sSearch_ parameters
var afnSearch_ = new Array();
var aiCustomSearch_Indexes = new Array();
Expand Down Expand Up @@ -62,24 +62,24 @@

// use only filtered rows
if (bFiltered == true) aiRows = oSettings.aiDisplay;
// use all rows
// use all rows
else aiRows = oSettings.aiDisplayMaster; // all row numbers

// set up data array
var asResultData = new Array();

for (var i = 0, c = aiRows.length; i < c; i++) {
var iRow = aiRows[i];
var aData = oTable.fnGetData(iRow);
var sValue = aData[iColumn];
var aData = oTable.fnGetData(iRow, iColumn);
var sValue = aData;

// ignore empty values?
if (bIgnoreEmpty == true && sValue.length == 0) continue;

// ignore unique values?
// ignore unique values?
else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;

// else push the value onto the result data array
// else push the value onto the result data array
else asResultData.push(sValue);
}

Expand Down Expand Up @@ -143,7 +143,7 @@
iLastFilterLength = 0;
var iCurrentFilterLength = this.value.length;
if (Math.abs(iCurrentFilterLength - iLastFilterLength) < iFilterLength
//&& currentFilter.length == 0 //Why this?
//&& currentFilter.length == 0 //Why this?
) {
//Cancel the filtering
return;
Expand Down Expand Up @@ -175,7 +175,7 @@

function fnCreateRangeInput(oTable) {

//var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
//var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
th.html(_fnRangeLabelPart(0));
var sFromId = oTable.attr("id") + '_range_from_' + i;
var from = $('<input type="text" class="number_range_filter form-control" id="' + sFromId + '" rel="' + i + '"/>');
Expand Down Expand Up @@ -249,32 +249,27 @@
th.html("");
//th.html(_fnRangeLabelPart(0));
var sFromId = oTable.attr("id") + '_range_from_' + i;
var from = $('<input type="text" class="date_range_filter form-control" id="' + sFromId + '" rel="' + i + '"/>');
var from = $('<input type="text" class="col-sm-2 date_range_filter form-control" style="width:150px" id="' + sFromId + '" rel="' + i + '"/>');
from.datepicker();
//th.append(from);
//th.append(_fnRangeLabelPart(1));
var sToId = oTable.attr("id") + '_range_to_' + i;
var to = $('<input type="text" class="date_range_filter form-control" id="' + sToId + '" rel="' + i + '"/>');
var to = $('<input type="text" class="col-sm-2 date_range_filter form-control" style="width:150px" id="' + sToId + '" rel="' + i + '"/> ');
//th.append(to);
//th.append(_fnRangeLabelPart(2));

for (ti = 0; ti < aoFragments.length; ti++) {

if (aoFragments[ti] == properties.sDateFromToken) {
th.append(from);
} else {
if (aoFragments[ti] == properties.sDateToToken) {
th.append(to);
} else {
th.append(aoFragments[ti]);
th.append('<label class="col-sm-1">'+aoFragments[ti]+'<i class="icon-calendar"></i></label>');
}
}


}


th.wrapInner('<span class="filter_column filter_date_range" />');
th.wrapInner('<div class="row filter_column filter_date_range"/>');
to.datepicker();
var index = i;
aiCustomSearch_Indexes.push(i);
Expand Down Expand Up @@ -338,10 +333,11 @@
if (currentFilter == null || currentFilter == "")//Issue 81
currentFilter = oSelected;

var r = '<select class="search_init select_filter form-control" rel="' + i + '"><option value="" class="search_init">' + sLabel + '</option>';
if(bMultiselect) {
r = '<select class="search_init select_filter form-control" rel="' + i + '" multiple>';
}
var selectId = "select" + sLabel.split(" ").join('');
var r = '<select id="' + selectId + '" class="search_init select_filter form-control" rel="' + i + '"><option value="" class="search_init"> Filtrar ' + sLabel + '</option>';
if (bMultiselect) {
r = '<select id="' + selectId + '" class="search_init select_filter form-control" rel="' + i + '" multiple="multiple">';
}
var j = 0;
var iLen = aData.length;
for (j = 0; j < iLen; j++) {
Expand Down Expand Up @@ -369,49 +365,50 @@
var select = $(r + '</select>');
nTh.html(select);
nTh.wrapInner('<span class="filter_column filter_select" />');

if(bMultiselect) {
select.change(function () {
if ($(this).val() != "") {
$(this).removeClass("search_init");
} else {
$(this).addClass("search_init");
}
var selectedOptions = $(this).val();
var asEscapedFilters = [];
if(selectedOptions==null || selectedOptions==[]){
var re = '^(.*)$';
}else{
$.each( selectedOptions, function( i, sFilter ) {
asEscapedFilters.push( fnRegExpEscape( sFilter ) );
} );
var re = '^(' + asEscapedFilters.join('|') + ')$';
}

oTable.fnFilter( re, index, true, false );
});
} else {
select.change(function () {
//var val = $(this).val();
if ($(this).val() != "") {
$(this).removeClass("search_init");
} else {
$(this).addClass("search_init");
}
if (bRegex)
oTable.fnFilter($(this).val(), iColumn, bRegex); //Issue 41
else
oTable.fnFilter(unescape($(this).val()), iColumn); //Issue 25
fnOnFiltered();
});
if (currentFilter != null && currentFilter != "")//Issue 81
oTable.fnFilter(unescape(currentFilter), iColumn);
}

if (bMultiselect) {
select.change(function () {
if ($(this).val() != "") {
$(this).removeClass("search_init");
} else {
$(this).addClass("search_init");
}
var selectedOptions = $(this).val();
var asEscapedFilters = [];
if (selectedOptions == null || selectedOptions == []) {
var re = '^(.*)$';
} else {
$.each(selectedOptions, function (i, sFilter) {
asEscapedFilters.push(fnRegExpEscape(sFilter));
});
var re = '^(' + asEscapedFilters.join('|') + ')$';
}

oTable.fnFilter(re, index, true, false);
});
} else {
select.change(function () {
//var val = $(this).val();
if ($(this).val() != "") {
$(this).removeClass("search_init");
} else {
$(this).addClass("search_init");
}
if (bRegex)
oTable.fnFilter($(this).val(), iColumn, bRegex); //Issue 41
else
oTable.fnFilter(unescape($(this).val()), iColumn); //Issue 25
fnOnFiltered();
});
if (currentFilter != null && currentFilter != "")//Issue 81
oTable.fnFilter(unescape(currentFilter), iColumn);
}
select.select2();
}

function fnCreateSelect(oTable, aData, bRegex, oSelected, bMultiselect) {
var oSettings = oTable.fnSettings();
if ( (aData == null || typeof(aData) == 'function' ) && oSettings.sAjaxSource != "" && !oSettings.oFeatures.bServerSide) {
if ((aData == null || typeof (aData) == 'function') && oSettings.sAjaxSource != "" && !oSettings.oFeatures.bServerSide) {
// Add a function to the draw callback, which will check for the Ajax data having
// been loaded. Use a closure for the individual column elements that are used to
// built the column filter, since 'i' and 'th' (etc) are locally "global".
Expand All @@ -429,30 +426,30 @@
});
}
// Regardless of the Ajax state, build the select on first pass
fnCreateColumnSelect(oTable, typeof(aData) == 'function' ? null: aData, _fnColumnIndex(i), th, label, bRegex, oSelected, bMultiselect); //Issue 37
fnCreateColumnSelect(oTable, typeof (aData) == 'function' ? null : aData, _fnColumnIndex(i), th, label, bRegex, oSelected, bMultiselect); //Issue 37

}

function fnRegExpEscape(sText) {
return sText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};

function fnCreateDropdown(aData) {
var index = i;
var r = '<div class="dropdown select_filter form-control"><a class="dropdown-toggle" data-toggle="dropdown" href="#">' + label + '<b class="caret"></b></a><ul class="dropdown-menu" role="menu"><li data-value=""><a>Show All</a></li>', j, iLen = aData.length;

for (j = 0; j < iLen; j++) {
r += '<li data-value="' + aData[j] + '"><a>' + aData[j] + '</a></li>';
}
var select = $(r + '</ul></div>');
th.html(select);
th.wrapInner('<span class="filterColumn filter_select" />');
select.find('li').click(function () {
oTable.fnFilter($(this).data('value'), index);
});
}

function fnRegExpEscape( sText ) {
return sText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};

function fnCreateDropdown(aData) {
var index = i;
var r = '<div class="dropdown select_filter form-control"><a class="dropdown-toggle" data-toggle="dropdown" href="#">' + label + '<b class="caret"></b></a><ul class="dropdown-menu" role="menu"><li data-value=""><a>Show All</a></li>', j, iLen = aData.length;

for (j = 0; j < iLen; j++) {
r += '<li data-value="' + aData[j] + '"><a>' + aData[j] + '</a></li>';
}
var select = $(r + '</ul></div>');
th.html(select);
th.wrapInner('<span class="filterColumn filter_select" />');
select.find('li').click(function () {
oTable.fnFilter($(this).data('value'), index);
});
}




function fnCreateCheckbox(oTable, aData) {

if (aData == null)
Expand Down Expand Up @@ -591,15 +588,16 @@
text: "Close",
click: function () { $(this).dialog("close"); }
}
]
]
});


$('#' + buttonId).click(function () {

$('#' + checkToggleDiv).dialog('open');
var target = $(this);
$('#' + checkToggleDiv).dialog("widget").position({ my: 'top',
$('#' + checkToggleDiv).dialog("widget").position({
my: 'top',
at: 'bottom',
of: target
});
Expand All @@ -611,7 +609,8 @@

fnOnFiltered = function () {
var target = $('#' + buttonId);
$('#' + checkToggleDiv).dialog("widget").position({ my: 'top',
$('#' + checkToggleDiv).dialog("widget").position({
my: 'top',
at: 'bottom',
of: target
});
Expand Down Expand Up @@ -656,7 +655,7 @@
sRangeSeparator: "~",
iFilteringDelay: 500,
aoColumns: null,
sRangeFormat: "From {from} to {to}",
sRangeFormat: "De {from} a {to}",
sDateFromToken: "from",
sDateToToken: "to"
};
Expand Down Expand Up @@ -709,13 +708,14 @@

oHost = oTable.fnSettings().nTHead;


}

//$(sFilterRow + " th", oHost).each(function (index) {//bug with ColVis
$(aoFilterCells).each(function (index) {//fix for ColVis
i = index;
var aoColumn = { type: "text",
var aoColumn = {
type: "text",
bRegex: false,
bSmart: true,
iMaxLenght: -1,
Expand Down Expand Up @@ -763,8 +763,8 @@
case "checkbox":
fnCreateCheckbox(oTable, aoColumn.values);
break;
case "twitter-dropdown":
case "dropdown":
case "twitter-dropdown":
case "dropdown":
fnCreateDropdown(aoColumn.values);
break;
case "text":
Expand Down Expand Up @@ -826,4 +826,4 @@



})(jQuery);
})(jQuery);

0 comments on commit 84f5a37

Please sign in to comment.