filter only works after sort

filter only works after sort

rookie_no_1rookie_no_1 Posts: 4Questions: 0Answers: 0
edited July 2013 in General
Hello,
I'm building custom filter functions which consider the content of an element attribute for sorting. This only works after a sort has been done, not before - and the solution described here (trigger a sort after loading of document is done) doesn't solve the issue:
http://datatables.net/forums/discussion/2886/filtering-only-works-after-sorting/p1

Being a rookie I'm at a loss as to what to do now - here's my page; the filtering is not working in the seventh column (titled "link"):
http://neue-definitionen.de/test/examples/api/multi_filter_custom.html
(It's work in progress - that's why it currently is a page adapted from the official download).

Here's the script code from the page, including the custom sort/filter functions - thanks in advance for your advice!
[code]
$.fn.dataTableExt.afnSortData['dom-CP-links'] = function ( oSettings, iColumn )
{
return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
return $('td:eq('+iColumn+') a', tr).attr('href');
} );
}

$.fn.dataTableExt.ofnSearch['dom-CP-links'] = function ( sData ) {
var myarr = sData.split(".");
return myarr[1];
}

var asInitVals = new Array();

$(document).ready(function() {
var oTable = $('#example').dataTable( {
"aoColumns": [
null,
null,
null,
null,
null,
null,
{ "sFilterDataType": "dom-CP-links", "sSortDataType": "dom-CP-links" }
],
"oLanguage": {
"sSearch": "Search all columns:",
"bStateSave": false,
"sPaginationType": "full_numbers",
"oLanguage": { "sSearch": "Filter:" },
"bJQueryUI": true
}
} );

/*
* Support functions to provide a little bit of 'user friendlyness' to the textboxes in
* the footer
*/
$("thead input").each( function (i) {
asInitVals[i] = this.value;
} );

$("thead input").focus( function () {
if ( this.className == "search_init" )
{
this.className = "";
this.value = "";
}
} );

$("thead input").blur( function (i) {
if ( this.value == "" )
{
this.className = "search_init";
this.value = asInitVals[$("thead input").index(this)];
}
} );

$.fn.dataTableExt.oApi.fnFilterAll = function (oSettings, sInput, iColumn, bRegex, bSmart) {
var settings = $.fn.dataTableSettings;

for (var i = 0; i < settings.length; i++) {
settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
}
};


$("input[name='typesZiel[]']").click(function () {
var stringofstuff = "";
var firstChecked = true;
$.each($("input[name='typesZiel[]']"), function () {
if ($(this).is(":checked")) {
if (firstChecked) {
stringofstuff += $(this).val();
firstChecked = false;
} else {
//add the OR operator
stringofstuff += "|" + $(this).val();
}
}
});
//2nd parameter is the column to filter
oTable.fnFilter(stringofstuff, 1, true, false);
});
$("input[name='typesStart[]']").click(function () {
var stringofstuff = "";
var firstChecked = true;
$.each($("input[name='typesStart[]']"), function () {
if ($(this).is(":checked")) {
if (firstChecked) {
stringofstuff += $(this).val();
firstChecked = false;
} else {
//add the OR operator
stringofstuff += "|" + $(this).val();
}
}
});
//2nd parameter is the column to filter
oTable.fnFilter(stringofstuff, 0, true, false);
});
$("input[name='typesCP[]']").click(function () {
var stringofstuff = "";
var firstChecked = true;
$.each($("input[name='typesCP[]']"), function () {
if ($(this).is(":checked")) {
if (firstChecked) {
stringofstuff += $(this).val();
firstChecked = false;
} else {
//add the OR operator
stringofstuff += "|" + $(this).val();
}
}
});
//2nd parameter is the column to filter
oTable.fnFilter(stringofstuff, 6, true, false);
});

oTable.fnSort( oTable.fnSettings().aaSorting );

} );
[/code]

Best,
R.

Replies

  • rookie_no_1rookie_no_1 Posts: 4Questions: 0Answers: 0
    Solved it by doing initial sort expressively on the column that the filter didn't work on until it was filtered - changed line 120 in above example to:
    [code]
    oTable.fnSort( [ [6,'asc'] ] );[/code]
  • davinjdavinj Posts: 1Questions: 0Answers: 0
    Hello I am also having this problem and would like another alternative.
This discussion has been closed.