Event subscription after initialization

Event subscription after initialization

brnullbrnull Posts: 4Questions: 0Answers: 0
edited February 2011 in General
I need to subscribe to the fnRowCallback of a table that has already been initialized. I've tried doing this by setting "bRetrieve": true in the orignal table initialization, but when I then try to create a fnRowCallback event by

[code]
$("#MyTable").dataTable({
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull)
{
console.log("Row Callback");
return nRow
}
});
[/code]

everything is reset in the datatable, but the row callback does work. Any help is much appreciated!

Thanks

Replies

  • brnullbrnull Posts: 4Questions: 0Answers: 0
    edited February 2011
    I have also tried

    [code]
    $("#MyTable").dataTable().fnSettings().fnRowCallback =
    function( nRow, aData, iDisplayIndex, iDisplayIndexFull)
    {
    console.log("Row Callback");
    return nRow
    }
    [/code]

    and it still seems to reset the datatable.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    The code in the second post should do the job. Which version of DataTables are you using? With 1.7 if #MyTable has been initialised already it should just return the object - if it hasn't been initialised then it will do so. If that doesn't help, can you post a link to an example please?

    Allan
  • brnullbrnull Posts: 4Questions: 0Answers: 0
    I'm using DataTables 1.7.5 and both of the above examples produce the same result. The table loses its original properties, but the row callbacks still perform as expected. I've written a work around as such:

    [code]
    $("#MyTable").dataTable({
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull) {return myRowCallback( nRow, aData, iDisplayIndex, iDisplayIndexFull);}
    });

    myRowCallback = function( nRow, aData, iDisplayIndex, iDisplayIndexFull)
    {
    return nRow;
    }
    [/code]

    Then outside of the class that handles the above initialization, we can override the row callback by:

    [code]
    myRowCallback = function( nRow, aData, iDisplayIndex, iDisplayIndexFull)
    {
    console.log("New fnRowCallback");
    return nRow;
    }
    [/code]

    This seems to work well. Is there anything I may be doing wrong in my first two posts that would let me not have to write the code in this way?

    Thanks.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    I've just tried this code in my demo setup:

    [code]
    $(document).ready(function() {
    $('#example').dataTable();
    $("#example").dataTable().fnSettings().fnRowCallback =
    function( nRow, aData, iDisplayIndex, iDisplayIndexFull)
    {
    console.log("Row Callback");
    return nRow
    }
    } );
    [/code]
    It works as I would expect - on the first draw (i.e. the initialisation) it will not show any log message. Then when you do the next draw (i.e. change page or sort etc) you get a log message for each log shown. Is that basically what you are looking for?

    Allan
  • brnullbrnull Posts: 4Questions: 0Answers: 0
    edited February 2011
    Allan,

    This code seems to work fine unless you define other attributes in the table's initialization. That is

    [code]
    $('#MyTable').dataTable({
    "bJQueryUI": true,
    "bPaginate": false,
    "bLengthChange": true,
    "bFilter": false,
    "bSort": false,
    "bInfo": false,
    "bAutoWidth": true,
    "aoColumns": [
    {"bVisible": 0 },
    {"bVisible": 0 },
    null,
    null,
    null,
    null,
    {"bVisible": 0 },
    null,
    {"bVisible": 0 },
    null,
    null,
    null,
    null,
    null,
    {"bVisible": 0 },
    null,
    null,
    null
    ]
    });
    [/code]

    and then executing

    [code]
    $("#MyTable").dataTable().fnSettings().fnRowCallback =
    function( nRow, aData, iDisplayIndex, iDisplayIndexFull)
    {
    console.log("Row Callback");
    return nRow
    }
    [/code]

    makes, in particular, bJQueryUI no longer work. I have not checked what other attributes are lost. As I said before, I have circumvented the problem by initializing the table with a stubbed in row callback function that can then later be overwritten. Let me know if you need any other feedback or have any ideas about why the table behaves this way.

    Thanks again
This discussion has been closed.