URGENT: How-to apply a filter function to only one datatable?

URGENT: How-to apply a filter function to only one datatable?

AdamOneAdamOne Posts: 6Questions: 0Answers: 0
edited July 2012 in General
Is it possible to apply a certain filter to only one datatable?
I have the following filter function that I am applying on document ready, I don't know if this is proper procedure, but as a side effect all dataTables will be affected by the filter. I would Like to affect only the $('#productTable'), but this selector appears to not have the desired effect.

//Filter Function in Stock
//$('#productTable').
$.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
var checked = $('#instock').is(':checked');
var qntStock = 1;
var stockCol = 3;

if (!checked) {
return true;
}
if (checked && aData[stockCol] > qntStock) {
return true;
}

return false;
});

Is it possible to apply a filter only to a particular table? How do I accomplish this?
I need to have this for a system that is going into production this next Monday! Any help is highly appreciated!

Replies

  • AdamOneAdamOne Posts: 6Questions: 0Answers: 0
    Any tips or ideas on how to solve this issue? (Sorry for the bump) but I really need to have this done this weekend or I will have to release it with the bug...
  • tja824tja824 Posts: 6Questions: 0Answers: 0
    @AdamOne,

    did you find a solution to this problem. I am experiencing the same. I will post back if I find something.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    You can add:

    [code]
    if ( document.getElementById('productTable') == oSettings.nTable ) {
    ... do filter
    }
    return true;
    [/code]

    Note that I've used getElementById rather than a jQuery selector because it is faster and you want all the speed possible here. Better yet would be caching the element into a "global" variable (not turely global, but locally).

    Regarding the urgent nature of your request - that's what the support options are there for http://datatables.net/support :-)

    Allan
  • tja824tja824 Posts: 6Questions: 0Answers: 0
    Allan, thank you for your quick response.

    The application I am working on 'navigates' between application sections (a list of customers, a list of users, a list of notifications, etc) by replacing the contents of a 'display' div on the main page of my application. I have a global set of javascript functions that are loaded in the main page and take care of navigation, handling dialog boxes and other user inputs and, most important to this discussion, retrieving data from the server to display in DataTables. For convenience in working with those global functions, each section DataTable has the id 'dataTable' (not very imaginative, I know).

    What I am finding is that when I load the DataTable in section A (in a 'generic' function which applies to all sections of my application):

    [code]
    $('#dataTable').dataTable( {
    "aaData": data.data,
    "bPaginate": false,
    "bInfo": false,
    "sDom": '<"dataTables_filter_toolbar">frtip'
    }
    );
    [/code]

    and then apply my filtering logic (in a function that is 'section-specific'):

    [code]
    $.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
    if ( aData[4] == "OPEN") {
    return true;
    }
    else if (dataTable_filter_showOpenOnly) { //this var is set true or false by a checkbox trigger
    return false;
    }

    return true;
    }
    );
    [/code]

    everything works as I expect it to, but when I load the DataTable in section B using the same generic method as above, the filtering for the table in A stays in effect. I figured that since I 'pushed' the filtering onto the $.fn.dataTableExt.aftFiltering array, I could just 0 the length to clear out the functioning, but that doesn't seem to work.

    I could go through my application and try to make unique ids for my dataTables in each application section to allow your previous suggestion to work, but that will introduce a number of other complexities as I work with the tables after they've been displayed. Is there a way to clear the filtering while I am navigating between sections that I am missing?

    Thanks again.
  • tja824tja824 Posts: 6Questions: 0Answers: 0
    I've also noticed that setting [quote]"bStateSave" : true[/quote] also affects my multiple DataTables, so if I sort the third column in one section of my application, then navigate to another section, the data there will be sorted by the third column. Are these settings keyed internally by the table id (which in my case is identical between sections), and would this be another reason to have my DataTable ids be unique across sections?
  • tja824tja824 Posts: 6Questions: 0Answers: 0
    Resolved my 'bStateSave' issue... I'm headed down the road of having unique ids for the tables.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    [code]
    $.fn.dataTableExt.afnFiltering.splice( 0, $.fn.dataTableExt.afnFiltering.length );

    or

    $.fn.dataTableExt.afnFiltering = [];
    [/code]

    should both wipe out the filtering array.

    This 'global' filtering can be quite frustrating I know :-(. I do plan on addressing this in future versions of DataTables: http://datatables.net/development/roadmap (v1.12).

    > ids be unique across sections?

    When you say a 'section' do you mean that there can be multiple sections on a page, or that they are on different pages? IDs have to be unique on a page.

    You might also want to look at how you can set defaults for DataTables, which it sounds like you want to do: http://datatables.net/release-datatables/examples/advanced_init/defaults.html

    Allan
  • AdamOneAdamOne Posts: 6Questions: 0Answers: 0
    edited July 2012
    Thank you for the prompt reply. I will be testing the solutions, the system is in production without the filters now, but it will be updated with the filters in 2 weeks.

    I don't understand how to attach this to my event... but i will try to apply it. Is there no way to do this on the constructor?

    How does the support system work? Would one hour be enough? I will definitively keep it in mind for a next time... datatables is quite extensive and I started working with it just a couple of weeks ago...
  • AdamOneAdamOne Posts: 6Questions: 0Answers: 0
    I get this when I try to use that code: Uncaught ReferenceError: oSettings is not defined
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > Is there no way to do this on the constructor?

    Currently no - it is planned for 1.12 - although it might get bumped up to 1.11 depending on how things shape up!

    > How does the support system work? Would one hour be enough?

    Typically yes :-). It will certainly be enough for me to scope out the issue with you and work towards a solution. Obviously more complex the issue, the longer it can take to solve.

    > I get this when I try to use that code: Uncaught ReferenceError: oSettings is not defined

    oSettings is being defined in your function header:

    [code]
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
    [/code]

    Possibly you've named it something else?

    Allan
This discussion has been closed.