afnFiltering function never called

afnFiltering function never called

cesincocesinco Posts: 5Questions: 0Answers: 0
edited June 2013 in Plug-ins
First of all, thanks for the product - looks very thorough.

I'm trying to use the afnFiltering function to implement a customer filter but it seems that the function is never called despite my issuing the fnDraw() on the dataTable object. Is there any way to test whether the function gets called and that the failure of the filter code to actually filter out any rows is due to the code in the filter function itself? Here is the function as I have implemented it:

[code]
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
//alert('Custom filtering taking place NOW!!!');
var strFilters = new Array();
var boolReturn = true;
$("#ProductAccordion table:first thead tr td").each(function(index) {
strFilters[index] = '';
$(this).find("input[type='checkbox']:not('.allCheck')").each(function() {
if($(this).is(':checked')) {
strFilters[index] += '|' + $(this).attr('id');
}
});
if (strFilters[index] != '') {
strFilters[index] = strFilters[index].substr(1);
}
});
for (var i=0; i < strFilters.length; i++) {
var regExpCol = new RegExp(strFilters[i]);
boolReturn = boolReturn & regExpCol.test(aData[i]);
}
return boolReturn;
}
);
[/code]

The alert, even if uncommented, does not appear.

Tanks in advance.

Replies

  • cesincocesinco Posts: 5Questions: 0Answers: 0
    edited June 2013
    Sorry - meant to say fnDraw() and not fnRedraw() - my actual code has the correct function call. I have corrected it in my original posting already.
  • cmillercmiller Posts: 5Questions: 0Answers: 0
    I'm having this exact same issue. No matter what I do, the function inside of fn.dataTableExt.afnFiltering.push is never called. What are some of the common mistakes?
  • allanallan Posts: 57,686Questions: 1Answers: 9,195 Site admin
    Can you link to a test case showing the problem please: http://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read
  • mca87mca87 Posts: 5Questions: 0Answers: 0
    edited July 2013
    Did you find any solution for this?
    I am running in the exact same problem. Example of code:

    [code]
    oTable.dataTableExt.afnFiltering.push(
    function (oSettings, aData, iDataIndex) {
    alert('inside!');
    return true;
    }
    );

    alert(oTable.dataTableExt.afnFiltering.length + " filter pushed");

    $input.change(function () {
    alert('doing the call...');
    oTable.fnDraw();
    });
    [/code]

    The filtering function is correctly stored ("1 filter pushed"), but at fnDraw() (when I click on the specific $input) it does not even call that function.
  • allanallan Posts: 57,686Questions: 1Answers: 9,195 Site admin
    No - we never got a test case. Can you link to one showing it not working please?

    Allan
  • mca87mca87 Posts: 5Questions: 0Answers: 0
    Hi,

    I've found the problem:
    After 10min I was able to get it to work so I suppose it was some kind of cache problem from the browser however I am not sure since I was in debug mode and I was really seeing no call even after the first alert() saying that the event was fired.

    I was expecting it to filter all results but it doesn't do that at startup, we have to call fnDraw manually since the plugin is loaded after the table (I guess that's the reason).

    Thank you for your fast answer.
  • allanallan Posts: 57,686Questions: 1Answers: 9,195 Site admin
    > we have to call fnDraw manually since the plugin is loaded after the table (I guess that's the reason).

    Sounds about right to me. If the plug-in isn't available when the table is initialised, its certainly not going to run then :-)

    Allan
  • mca87mca87 Posts: 5Questions: 0Answers: 0
    I think so.
    I have a question, is there any other method that could make it without two calls (I mean the first fnDraw is done before the second that applies the plugin)?
    I am using a customized version of columnFilter and the problem is that for each checkbox filter field, for example every checkbox, I am adding this:
    [code]
    //function fnCreateCheckbox()

    //index = current aData position;
    //oTable_ID = current ID of the table

    oTable.dataTableExt.afnFiltering.push(
    function (oSettings, aData, iDataIndex) {
    if (oTable_ID != oSettings.sTableId)
    return true;
    switch (inputValue_Saved) {
    case 'true':
    return $(aData[index]).filter("input").is(":checked");
    case 'false':
    return !$(aData[index]).filter("input").is(":checked");
    default:
    return true;
    }
    }
    );
    [/code]
    1. If I understand this correctly, even if this works (tested), it's adding a function to every DataTable fnDraw call?
    2. Plus, (considering it's based on the columnFilter plugin) its initialization is made after the .dataTable(...) call. I did not find a way to NOT do a double call to fnDraw (I consider that .dataTable() make some kind of fnDraw call to display its content, which is normal).
    How could I make it to not have a second call?

    Because the other filters like the fnCreateInput seems to use fnFilter, and - at least on the server side mode - it doesn't do a second call to the database.
    However it's still the columnFilter plugin, it's called AFTER .datatable(...), but still it does not do 2 calls.

    It's the way I would like to work with my custom plugin.

    How could I not be forced to make a call to fnDraw in order to filter the data?
  • allanallan Posts: 57,686Questions: 1Answers: 9,195 Site admin
    Can you not just add the filter before the initialisation? Then it will be run immediately.

    I can't say about the column filter plug-in I'm afraid. That's third party and I don't know much about it.

    Allan
  • cesincocesinco Posts: 5Questions: 0Answers: 0
    Hi folks - sorry for not responding earlier. It seems that the notifications from datatables.net were going to my spam folder and I never saw that people had replied. In the end, I also did get it to work although I can't remember how at this point, and I'd have to dig through my code to try to learn how it was broken to begin with and what I did to fix it. At best, I can offer the modified code I used in place of the original in my initial posting (which uses some additional custom functions I had to write):
    [code]
    $.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
    var vMin = $("#slidMinVolt").slider("value");
    var vMax = $("#slidMaxVolt").slider("value");
    var vMinData = aData[6];
    var vMaxData = aData[7];
    var cMin = getNonLinearValue(intAllMinCap, intAllMaxCap, $("#slidMinCap").slider("value"));
    var cMax = getNonLinearValue(intAllMinCap, intAllMaxCap, $("#slidMaxCap").slider("value"));
    var cMinData = aData[8];
    var cMaxData = aData[9];

    if (
    vMin <= vMinData &&
    vMaxData <= vMax &&
    cMin <= cMinData &&
    cMaxData <= cMax
    ) {
    return true;
    }
    return false;
    }
    );
    [/code]
  • vaasugivvaasugiv Posts: 5Questions: 0Answers: 0
    Hi all,
    Im having the same problem, the filtering function successfully gets pushed into 'afnFiltering' array, but it's never been called when fnDraw is called. Could someone overcome this issue please explain as to what you did to solve this problem? Many thanks in advance.
    Thanks,
    Vaasugi.
  • allanallan Posts: 57,686Questions: 1Answers: 9,195 Site admin
    @vaasugiv - can you link to the page showing the issue so we can debug it please.

    Allan
  • ebynumebynum Posts: 1Questions: 0Answers: 0
    @allan – I think my issue might match this one. Everything looks like it's working except that the filter doesn't fire.
    Because I'm set to "bServerSide": true.
    It looks like the assumption is that if I'm using server-side processing, I can't ALSO do client-side filtering. Is that right? Is there any (simple) way around that?
    Thanks,
    Ed
  • allanallan Posts: 57,686Questions: 1Answers: 9,195 Site admin
    That is correct. If you have server-side processing enabled, then the processing (sorting, filtering etc) is done at the server. There is no way around that - client-side and server-side processing are two distinct modes in DataTables. For example if you have it filtering the server-side processing data on the client-side, it would only be filtering what you can currently see. While that might be what is wanted in some cases, it is not something that is supported by DataTables.

    Allan
This discussion has been closed.