refresh issues

refresh issues

lothar83frlothar83fr Posts: 7Questions: 0Answers: 0
edited January 2010 in Bug reports
Hi Allan,
I noted some problems due to the refresh.

I use the server-side process to fill my table.

I refresh my table like that:
[code]
function fnAutoReload(){
oTable.fnDraw(false);
setTimeout(function(){ fnAutoReload(); }, 2000); //2s
}
[/code]

[fnInitComplete In datatable init]
[code]
"fnInitComplete": function() {
fnAutoReload();
}
[/code]

Each row of my table is clickable so i do that:
[For bind]
[code]
[...]
"fnDrawCallback": function() {
fnAddEventClickForEachRow();
},
[...]
[/code]
[code]
function fnAddEventClickForEachRow ( )
{
$('#tbl_lst_ejection tbody tr').each( function () {
$(this).click(fnOpenDetails);
$(this).css('cursor', 'pointer');
} );
}
[/code]

[For unbind]
[code]
[...]
"fnServerData": function ( sSource, aoData ,fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": function (oSettings) {

$('#tbl_lst_ejection tbody tr').each( function () {
$(this).unbind('click',fnOpenDetails);
} );
fnCallback(oSettings);

}
} );
},
[...]
[/code]

So, the issue is: some times (during the refresh delay), when I click on a row, that not execute the fnOpenDetails function...
If I reduce the refresh delay (ex: 100 ms), the problem increase and it's impossible to call the function.

The second issue is that if I want insert an html input (ex:; with the "fnRender" function), on each refresh, the input loose his previous value.
How i can avoid that? It's possible to not refresh a column and keep the previous value?

Thanks.
Jean-philippe

Replies

  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Hi Jean-philippe,

    1. My guess is that this is sometimes an issue because you manage to click on the row basically in between line 12 and 13 of your unbind code block - which is unfortunate timing. To be honest I'm not sure there is a way to make this always work, since what you are doing already is correct - it's just that the process takes a finite amount of time. So I'd suggest two options:

    1.1: Use the 'processing' display to cover the whole table when it's reloading capture the client events, and let the user know visually that there is something going on.
    1.2: Don't remove the event listener - but then I think you'll end up with a memory leak, which would be bad news if you are refreshing every 2 seconds.

    Sorry - it's a bit of a tricky one that one... You want to deal with an event on an element, knowing that in a fraction of a second it will be deleted from the DOM!

    2. The refresh implicitly implies that all values will be taken again from the server, so if it's not saved on the server, then it will take whatever value the server has. What you are looking for is an update rather than refresh, so only data which has changed will be modified. This isn't available in DataTables server-side processing.

    Is your data basically the same for each refresh, and you are updating live information (like stocks or something?). If so, perhaps it would be better to use sAjaxSource for the first load and then poll the Ajax source yourself and update the table if needed. That would probably solve both of your problems, but it might not be appropriate to your situation.

    Regards,
    Allan
  • lothar83frlothar83fr Posts: 7Questions: 0Answers: 0
    edited January 2010
    Hi Allan,

    Thanks for your help, I follow your advise and i refresh only if the data change but i want to be sure that code not imply memory leak:

    [code]
    $.ajax( {
    "dataType": 'json',
    "type": "GET",
    "url": sSource,
    "data": aoData,
    "success": function (oSettings) {


    //tester si les valeurs on chang
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Hi Jean-Plilippe,

    Nice clean looking code - I like it :-)

    Yup - I believe you should be safe with it like that, as long as no other events are attached to your table elements, which they probably aren't.

    As a small side-note, you could make your unbind slightly more efficient by just doing:

    [code]
    ('#tbl_lst_ejection tbody tr').unbind('click',fnOpenDetails);
    [/code]
    Indeed - even the 'fnOpenDetails' is optional, but basically jQuery will do the loop for you internally.

    Regards,
    Allan
This discussion has been closed.