/*
* File: jquery.dataTables.columnFilter.js
* Version: 1.5.6.
* Author: Jovan Popovic
*
* Copyright 2011-2014 Jovan Popovic, all rights reserved.
*
* This source file is free software, under either the GPL v2 license or a
* BSD style license, as supplied with this software.
*
* This source file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* Parameters:"
* @sPlaceHolder String Place where inline filtering function should be placed ("tfoot", "thead:before", "thead:after"). Default is "tfoot"
* @sRangeSeparator String Separator that will be used when range values are sent to the server-side. Default value is "~".
* @sRangeFormat string Default format of the From ... to ... range inputs. Default is From {from} to {to}
* @aoColumns Array Array of the filter settings that will be applied on the columns
*/
(function ($) {
$.fn.columnFilter = function (options) {
var asInitVals, i, label, th;
//var sTableId = "table";
var sRangeFormat = "From {from} to {to}";
//Array of the functions that will override sSearch_ parameters
var afnSearch_ = new Array();
var aiCustomSearch_Indexes = new Array();
var oFunctionTimeout = null;
var fnOnFiltered = function () { };
function _fnGetColumnValues(oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) {
///
///Return values in the column
///
///DataTables settings
///Id of the column
///Return only distinct values
///Return values only from the filtered rows
///Ignore empty cells
// check that we have a column id
if (typeof iColumn == "undefined") return new Array();
// by default we only wany unique data
if (typeof bUnique == "undefined") bUnique = true;
// by default we do want to only look at filtered data
if (typeof bFiltered == "undefined") bFiltered = true;
// by default we do not wany to include empty values
if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true;
// list of rows which we're going to loop through
var aiRows;
// use only filtered rows
if (bFiltered == true) aiRows = oSettings.aiDisplay;
// 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];
// ignore empty values?
if (bIgnoreEmpty == true && sValue.length == 0) continue;
// ignore unique values?
else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
// else push the value onto the result data array
else asResultData.push(sValue);
}
return asResultData.sort();
}
function _fnColumnIndex(iColumnIndex) {
if (properties.bUseColVis)
return iColumnIndex;
else
return oTable.fnSettings().oApi._fnVisibleToColumnIndex(oTable.fnSettings(), iColumnIndex);
//return iColumnIndex;
//return oTable.fnSettings().oApi._fnColumnIndexToVisible(oTable.fnSettings(), iColumnIndex);
}
function fnCreateInput(oTable, regex, smart, bIsNumber, iFilterLength, iMaxLenght) {
var sCSSClass = "text_filter form-control";
if (bIsNumber)
sCSSClass = "number_filter form-control";
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);
if (bIsNumber)
th.wrapInner('');
else
th.wrapInner('');
asInitVals[i] = label;
var index = i;
if (bIsNumber && !oTable.fnSettings().oFeatures.bServerSide) {
input.on('keyup', function () {
/* Filter on the column all numbers that starts with the entered value */
oTable.fnFilter('^' + this.value, _fnColumnIndex(index), true, false); //Issue 37
fnOnFiltered();
});
} else {
input.on('keyup', function () {
if (oTable.fnSettings().oFeatures.bServerSide && iFilterLength != 0) {
//If filter length is set in the server-side processing mode
//Check has the user entered at least iFilterLength new characters
var currentFilter = oTable.fnSettings().aoPreSearchCols[index].sSearch;
var iLastFilterLength = $(this).data("dt-iLastFilterLength");
if (typeof iLastFilterLength == "undefined")
iLastFilterLength = 0;
var iCurrentFilterLength = this.value.length;
if (Math.abs(iCurrentFilterLength - iLastFilterLength) < iFilterLength
//&& currentFilter.length == 0 //Why this?
) {
//Cancel the filtering
return;
}
else {
//Remember the current filter length
$(this).data("dt-iLastFilterLength", iCurrentFilterLength);
}
}
/* Filter on the column (the index) of this element */
oTable.fnFilter(this.value, _fnColumnIndex(index), regex, smart); //Issue 37
fnOnFiltered();
});
}
input.on('focus', function () {
if ($(this).hasClass("search_init")) {
$(this).removeClass("search_init");
this.value = "";
}
});
input.on('blur', function () {
if (this.value == "") {
$(this).addClass("search_init");
this.value = asInitVals[index];
}
});
}
function fnCreateRangeInput(oTable) {
//var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
th.html(_fnRangeLabelPart(0));
var sFromId = oTable.attr("id") + '_range_from_' + i;
var from = $('');
th.append(from);
th.append(_fnRangeLabelPart(1));
var sToId = oTable.attr("id") + '_range_to_' + i;
var to = $('');
th.append(to);
th.append(_fnRangeLabelPart(2));
th.wrapInner('');
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(
oTable.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
if (oTable.attr("id") != oSettings.sTableId)
return true;
// Try to handle missing nodes more gracefully
if (document.getElementById(sFromId) == null)
return true;
var iMin = document.getElementById(sFromId).value * 1;
var iMax = document.getElementById(sToId).value * 1;
var iValue = aData[_fnColumnIndex(index)] == "-" ? 0 : aData[_fnColumnIndex(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();
fnOnFiltered();
});
}
function fnCreateDateRangeInput(oTable) {
var aoFragments = sRangeFormat.split(/[}{]/);
th.html("");
//th.html(_fnRangeLabelPart(0));
var sFromId = oTable.attr("id") + '_range_from_' + i;
var from = $('');
from.datepicker();
//th.append(from);
//th.append(_fnRangeLabelPart(1));
var sToId = oTable.attr("id") + '_range_to_' + i;
var to = $('');
//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.wrapInner('');
to.datepicker();
var index = i;
aiCustomSearch_Indexes.push(i);
//------------start date range filtering function
//$.fn.dataTableExt.afnFiltering.push(
oTable.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
if (oTable.attr("id") != oSettings.sTableId)
return true;
var dStartDate = from.datepicker("getDate");
var dEndDate = to.datepicker("getDate");
if (dStartDate == null && dEndDate == null) {
return true;
}
var dCellDate = null;
try {
if (aData[_fnColumnIndex(index)] == null || aData[_fnColumnIndex(index)] == "")
return false;
dCellDate = $.datepicker.parseDate($.datepicker.regional[""].dateFormat, aData[_fnColumnIndex(index)]);
} catch (ex) {
return false;
}
if (dCellDate == null)
return false;
if (dStartDate == null && dCellDate <= dEndDate) {
return true;
}
else if (dStartDate <= dCellDate && dEndDate == null) {
return true;
}
else if (dStartDate <= dCellDate && dCellDate <= dEndDate) {
return true;
}
return false;
}
);
//------------end date range filtering function
$('#' + sFromId + ',#' + sToId, th).change(function () {
oTable.fnDraw();
fnOnFiltered();
});
}
function fnCreateColumnSelect(oTable, aData, iColumn, nTh, sLabel, bRegex, oSelected, bMultiselect) {
if (aData == null)
aData = _fnGetColumnValues(oTable.fnSettings(), iColumn, true, false, true);
var index = iColumn;
var currentFilter = oTable.fnSettings().aoPreSearchCols[i].sSearch;
if (currentFilter == null || currentFilter == "")//Issue 81
currentFilter = oSelected;
var r = '