3

I need to get my hands on existing datatable's fnRowCallback setting but everything I try seems to fail.

What have I tried:

1:

var dt = $('#table').dataTable({
            "bRetrieve": true,
            "fnRowCallback" : function (nRow, aData, iDisplayIndex, iDisplayIndexFull)    {
             console.warn("working");
             }
        );

2:

var dt = $('#table').dataTable({"bRetrieve": true});

dt.fnSettings().fnRowCallback = function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        console.warn("working");
    }

No luck, but if I change

var dt = $('#table').dataTable({"bRetrieve": true});
dt.fnSettings().sAjaxSource = "invalid url";

I get an error so that one seems to work. Also if I do fnRowCallback in the original datatable initialisation it works, but that's not what I want.

1 Answer 1

5

I deleted my old answer since I really didn't understand your post (it was late!)

It seems like you either have to use unofficial API, or destroy the old table and re-init it.

Unofficial API way (might not work in future version), tested; working in dataTable 1.9.2

$("#table").dataTable().fnSettings().aoDrawCallback.push( {
    "fn": function (oSettings) {
        if ( oSettings.aiDisplay.length == 0 ) {
            return;
        }

        var nTrs = $('#table tbody tr');
        var iColspan = nTrs[0].getElementsByTagName('td').length;
        var sLastGroup = "";
        for ( var i=0 ; i<nTrs.length ; i++ ) {

            var iDisplayIndex = oSettings._iDisplayStart + i;
            data = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex] ]._aData;


            // Now you can access things like
            if (data.percentage > 80) {
                // And access the rows like this
                $(nTrs[i]).addClass("highlight-green");
            }

        }
    },
} );

Correct way: Save old settings, destroy, re-init

var oTable = $('#table').dataTable();

var oldoptions = oTable.fnSettings();

var newoptions = $.extend(oldoptions, {
    fnRowCallback: function(nRow, aData, iDisplayIndex, iDisplayIndexFull)    {
     console.warn("working");
    }
})

oTable.fnDestroy();

$('#table').dataTable(newoptions);

reference: http://datatables.net/forums/discussion/2737/addchange-callback-after-initialization-or-else-clone-settings-to-re-build-table/p1

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, I'll test this out once I get back to work in 2013 :) Happy holidays
You may also add "bDestroy": true option in the new init settings instead of calling fnDestroy()
@RoyLing Why use push instead of = when setting aoDrawCallback using the first way?
@YanYang if you read the doc of DataTables, aoDrawCallback is actually an array of objects with fn property, ie. callback, that said, it is like listeners for draw event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.