Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Vladislav on Nov 03, 2009 13:45
open dhtmlx forum
How to attach an event handler to a grid after all records had been "really!" loaded ?

Hello,

Does any one know how can I attach an event handler or capture 'finish loading' moment somehow ?
I have a grid loaded from XML:

if (bReload)
mygrid.clearAndLoad(strUrl, OnGridLoaded)
else
mygrid.load(strUrl, OnGridLoaded);

// if we load the grid having a single row show it's details automatically
function OnGridLoaded()
{
if (mygrid.getRowsNum() == 1)
{
var lnk = window.document.getElementById("lnkSelectRow0");
if (lnk != null) lnk.click();
}
}

Grid has paging turned on, and XML gets returned from server side (aspx) by pages.
What I want is to auto-click a link in the first cell After all records had been loaded.
Problem is OnGridLoaded() doesn't really work. This is because (I explored it using IE8's debugger)
data are not loaded at the time grid calls this function (even if it is called OnGridLoaded by someone!)
So when I try to get this link it doesn't exist yet.

How can I get my link so I can click it then ?

Thanks,

Vlad

Answer posted by dhxSupport on Nov 05, 2009 09:04
>>What I want is to auto-click a link in the first cell After all records had been loaded.
Unfortunately it's not clear which link do you want to click. Do you want to open first cell's editor when rows are loaded? 
>>Problem is OnGridLoaded() doesn't really work.
Unfortunately we cannot reproduce this issue locally. 
To check if rows were loaded you can use "onXLE" event. Please find more information here http://dhtmlx.com/dhxdocs/doku.php?id=dhtmlxgrid:event_onxle
Answer posted by Vladislav on Nov 05, 2009 12:02

>> Unfortunately it's not clear which link do you want to click. Do you want to open first cell's editor when rows are loaded? 

I have grid and detail tabs beneath it. When user clicks an image on the row (a link with an image in the first cell) it populates detail tabs.

Now if it happens that the grid has only a single row, I need to make detail tabs to be populated automatically.

mygrid.clearAndLoad(strUrl, OnGridLoaded)

I do it in OnGridLoaded function.

// if we load a grid having a single row - show it's details automatically

function OnGridLoaded()

{

if (mygrid.getRowsNum() == 1)

{

var lnk = window.document.getElementById("lnkSelectRow0");

if (lnk != null)

{

var attr = lnk.getAttribute("onclick");

typeof attr == 'function' ? attr() : eval(attr);

}

else

window.setTimeout(OnGridLoaded, 100); // wait till the link is loaded

}

}

This function has setTimeout now. It waits for the grid to finish loading (probably in background?). If I remove this setTimeout call lnk variable will be equal to null! I fixed it but it took me a day to figure out what was happening.

Also: Lots of function do not work reliably, I can state it as a first time user for your grid. For example I was trying too configure grid with XML, columns, (btw add this on load event handler there) and it always showed me some internal grid's JS errors. SO finally I configured my columns in code. Grid NEEDs error handling (a customer cannot find out a reason of error judging by some line numbers in internal grid's code)

Vlad

 

Answer posted by Alex (support) on Nov 09, 2009 08:42

Hello,

you use dynamic loading, don't you ?

In this case there is no easy way to know when all rows are loaded. 

The second parameter of the load method is function that is called right after the initial xml is loaded. So, it is called only once (in case of dynamic paging when the 1st page is loaded). 

As the possible solution we can provide you the following approach:

- to set onXLE event handler (onXLE event will be called each time the portion of data is loaded)

- to check if all rows are already loaded

grid.attachEvent("onXLE",function(){
  for(var i=0; i < this.rowsBuffer.length;i++)
      if(!this.rowsBuffer[i]) return;

  alert("All rows are loaded");
})


rowsBuffer is private property (array of grid rows).

Answer posted by Vlad on Nov 09, 2009 11:40

Yes, I use paging, not sure if is dynamic or static, but we load rows in pages not the whole DB.

Thanks for the answer!

Vlad