JQuery datatables changing rowcallback after initialization

I need to access the existing fnRowCallback parameter, but everything I try seems to fail.

What 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 am getting an error so it seems to work. Also, if I do fnRowCallback in the initial datatable initialization, it works, but that is not what I want.

+3
source share
1 answer

I deleted my old answer, as I really did not understand your post (it was too late!)

It looks like you either need to use the unofficial API, or destroy the old table and restart it.

API (, ), ; 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");
            }

        }
    },
} );

: , ,

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);

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

+5

Source: https://habr.com/ru/post/1548512/


All Articles