fnRowCallback and fnUpdate

fnRowCallback and fnUpdate

kkudikkudi Posts: 77Questions: 0Answers: 0
edited May 2010 in General
Hi,
is there a a way to force fnRowCallback to be called on both fnUpdate and fnAddData?

As far as I can tell it only gets called on fnAddData.

Replies

  • kkudikkudi Posts: 77Questions: 0Answers: 0
    If there's a workaround and someone knows the answer please let me know as this is quite important

    Thanks
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi kkudi,

    I never thought of that before... It does make sense though! I'll look into it for the next release, and see if it's a sensible thing to do (which on first glance it is!).

    Allan
  • kkudikkudi Posts: 77Questions: 0Answers: 0
    Allan, thanks for your reply. ( what you have created is a remarkable)

    do you think it could be easy to make the change myself? im not sure how the callback is getting called within fnAddData though.

    From my understanding, the row callback function gets called when the table is drawn and not data is added ( unless you redraw each time you add data). If that's the case, doesnt fnUpdate call draw too?

    Thanks
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi kkudi,

    Sorry yes - you are quite correct and I'm getting myself confused :-). fnRowCallback is called whenever the row is display (I was thinking of fnRender which is done only when the row is added).

    fnUpdate will redraw be default (it can be overridden with a parameter). So assuming your new row is being displayed, it should have the callback function acting on it it. Is it not? Can you link to an example?

    Allan
  • kkudikkudi Posts: 77Questions: 0Answers: 0
    I am not able to give you a link, I'm sorry
    But here's the callback function when the table is initialized

    [code]

    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
    decorateRow(nRow);
    return nRow;
    }


    function decorateRow(row) {
    $(row).children().each(function(index, td){
    if ($(td).html().indexOf("-") === 0) {
    $(td).css("color", "red");
    }
    else {
    $(td).css("color", "");
    }
    });

    }


    [/code]

    when fnAddData is called, the callback is called.

    on fnUpdate(row, rowElem) the callback is not called

    It says in the API page that an optional value of redrawing can be used, but default is true.
  • kkudikkudi Posts: 77Questions: 0Answers: 0
    I have also tried fnUpdate(row, rowElem, true) and no difference was observed.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi kkudi,

    I've just tried the following code on my zero config page ( http://datatables.net/examples/basic_init/zero_config.html ), and it appears to work as expected for me:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
    decorateRow(nRow);
    return nRow;
    }
    } );

    $('#example').dataTable().fnUpdate( ["-","1","2","-","4"], $('#example tbody tr:eq(0)')[0] );
    } );

    function decorateRow(row) {
    $(row).children().each(function(index, td){
    if ($(td).html().indexOf("-") === 0) {
    $(td).css("color", "red");
    }
    else {
    $(td).css("color", "");
    }
    });
    }
    [/code]
    Perhaps you could paste in your fnUpdate call?

    Allan
  • kkudikkudi Posts: 77Questions: 0Answers: 0
    Thanks for your help in this so far

    my fnUpdate is a simple fnUpdate(row, rowElem) with row being an array of strings ( length is same number as columns) and rowElem is the tr element i want to update.

    So I have copied and pasted your code onto a newly created page with 1.6.2 and it doesn't work.

    With 1.7 beta it works fine.

    so clearly, there must be something in 1.6.2 that prevents the callback from being called on an fnUpdate.

    andreas
  • kkudikkudi Posts: 77Questions: 0Answers: 0
    ok found the bug.
    in 1.7 you have

    /* Redraw the table */
    if ( typeof bRedraw == 'undefined' || bRedraw )
    {
    _fnReDraw( oSettings );
    }


    whereas in 1.6.2 you have

    /* Redraw the table */
    if ( typeof bRedraw != 'undefined' && bRedraw )
    {
    _fnReDraw( oSettings );
    }


    :)
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Ah yes sorry. In the mists of everything else that has changed in 1.7 I had completely forgotten about that - sorry. Good to hear you've got it sorted now :-)

    Regards,
    Allan
  • brucebruce Posts: 3Questions: 0Answers: 0
    Hi Allan!

    Maybe there is an other issue, at least it was not working as I thought before:
    I use a DataTable with a sAjaxSource and now I want to change the contents of one row. After I do that (calling the backend, the backend is updating the row in the database) I want to update the DataTable without reloading all the data from the server because of performance and comfortability reasons. So I call
    [code]
    var pos = datatable.fnGetPosition(row);
    datatable.fnUpdate(data, pos, 0, false, false);
    [/code]
    But this is not calling fnRowCallback on the specified row (as I expected before). Maybe you could change that, otherwise this is a workaround for all running into the same problem:
    [code]
    datatable.fnSettings().fnRowCallback(row, data, 0, 0);
    [/code]

    Greetings,
    bruce
  • richardluckrichardluck Posts: 1Questions: 0Answers: 0
    Has this issue been resolved? I, too, am using sAjaxSource and want to change the contents of one row after some processing, and am unable to get fnUpdate to do so.

    Richard
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi Richard,

    In burce's case above, fnRowCallback was not being called because he's explicitly telling DataTables not to redraw the table (parameter 4 is false) and fnRowCallback is called only when a row is displayed (@bruce - if you read this, sorry I didn't get back to you at this time!). It looks like you are in the same boat with passing false.

    Are you using server-side processing? If so, fnUpdate isn't a particularly useful function for you since its client-side only (like fnAddData and fnDeleteRow) - and there isn't any data stored on the client-side - its all on the server, and every draw must make a call to get the latest data.

    Perhaps you could explain a bit more about what you are trying to do, and if you can, run your table through the debugger ( http://debug.datatables.net ) so I can see and understand its configuration.

    Allan
  • fuzzfuzz Posts: 1Questions: 0Answers: 0
    HI Allan,

    I'm using server-side processing too. One row looks like this:

    [code]row = [
    {"track_id":"128","rating":"2"},
    "1",
    null,
    {"track_id":"128"},
    "foo.mp3",
    {"track_id":"128","rating":"2"}][/code]

    During some backend events some of this entries have to be updated. The event contains (along with other data) just the same data like the ajax calls und I want to inject this row into the table (instead of an upgrade of the whole data). And I'm using "fnRender" to convert the above data to HTML strings.

    Is there a solution for this combination? Using "server-side processing" and "replacing single rows" with fnRender on client side?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    With server-side processing, each row in the table has the life time of only one draw. As soon as you redraw, the TR is destroyed and the data retrieved from the server for the next draw (since it is sever-side processing, data is not held at the client-side, only displayed).

    Allan
This discussion has been closed.