How to get row node or classes from parameter “settings” in $.fn.dataTable.ext.search function.

How to get row node or classes from parameter “settings” in $.fn.dataTable.ext.search function.

SabastianSabastian Posts: 2Questions: 0Answers: 0

I would like to do some filtering similar to the example at https://datatables.net/examples/plug-ins/range_filtering.html. Only I would like to filter based on if the table row has a class. Is there any way inside a new search function to get the row node or classes. I assume it would have to be from the “settings” parameter that is passed into the function. Bellow is a sample of a nonworking class check in side a new search function so you can see the context I am trying to find this data in. Thanks for your help.

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex )
    {
        // How to do something similar to the following check.
        if (settings.row(dataIndex).node().hasClass("classy"))
        {
            return true;
        } else {
            return false;
        }
    }
);

Replies

  • allanallan Posts: 61,732Questions: 1Answers: 10,110 Site admin

    Its really a private API, but the way to do this is settings.aoData[ dataIndex ].nTr. That will give you the row node.

    The proper way is to use:

    var api = new $.fn.dataTable.Api( settings );
    var tr = api.row( dataIndex ).node();
    

    But that creates a new API instance every time the function is called which is slow.

    Perhaps the best option would be to store the API instance in a scope which the search function can be use. That way you don't need to use the private API (which could change in future, without warning!) and don't have the performance overhead issue.

    This is the part of DataTables that is probably most in need of fixing / improving!

    Allan

  • SabastianSabastian Posts: 2Questions: 0Answers: 0

    Thanks @allan. That is exactly the information I needed. In mean time I had figured out that I could make a new api, but did not understand all the implications of doing that, so I will see what I can do to store it some place.

This discussion has been closed.