Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Craig Efrein on Feb 05, 2009 07:35
open dhtmlx forum
grid.getRowsNum() returns 0 if grid is loaded by loadXML

Hello,

I am trying to determine the number of lines in my grid after using the command grid.loadXML(url1);

So far the command grid.getRowsNum() returns 0.

If I add the rows manually with grid.addRow(new_row,'',new_index);, I get an accurate row count.


Is there anyway to return the number of rows after loading a grid with loadXML?

Thanks,
Craig
Answer posted by Support on Feb 05, 2009 07:45
Please be sure that you call the command after data is really loaded ( loadXML is async. )
You may need to use onXLE event or second parameter of loadXML command to catch the moment when data really loaded. 
Answer posted by Craig Efrein on Feb 06, 2009 03:04
I did the following to my grid
grid.attachEvent('onXLE',resize_grid);

In the function resize_grid, I have the following commands
    var num_rows_this_grid = grid.getRowsNum();
     
    /* resize grids */
    if(num_rows_this_grid > num_rows){
        grid.setColWidth(index, newwidth);
    } else {
        grid.setColWidth(index, oldwidth);
    }

This function resizes a column in the grid to make room for the scroll bar that appears after a certain number of lines are added.  
In IE6, I get an error and the grid doesn't initalize.   There is no explanation for the error, and I can't reproduce it in firefox 2 or firefox 3.

If I call the same function from a button on the form, I don't get any errors.

Thanks,
Craig
Answer posted by Support on Feb 06, 2009 03:59
Please be sure that newwidth and oldwidth values are pure numbers in any situation.
IE will throw an error if incorrect ( not numeric ) size will be provided ( while FF will just ignore it ) , so it very similar to your situation. 

By the way, if you need to adjust grid , to prevent h-scroller, you can use enableAutoWidth mode or just set one of columns as flexible ( * as size )
Answer posted by Craig Efrein on Feb 06, 2009 04:44
The suggestion you sent was helpful but doesn't quite display the grid like I want when there is a vertical scroll bar.

The command grid.setColWidth(index, newwidth); works some of the time.  I've noticed that when loadXML takes a long time to complete, IE errors out when it tries to set the column width.  When loadXML loads the grid quickly, grid.setColWidth(index, newwidth); works in IE.

The numbers I'm sending are whole numbers
Answer posted by Craig Efrein on Feb 06, 2009 05:19
I finally figured out where the error is coming from, line 118 in the dhtmlxgrid.js file

this.hdr.rows[0].cells[i].style.width=this.cellWidthPX[i]+"px"

windows is throwing an exception
Answer posted by Support on Feb 06, 2009 06:32
Do you have some initial value size defined for each column ?
( grid.setInitWidths command with values for all columns )

Answer posted by Craig Efrein on Feb 06, 2009 06:35
Yes the inital column widths are set and the values are already loaded by loadXML

Answer posted by Support on Feb 06, 2009 07:11
The error at mentioned line can occur only in two cases
a) initial width of column was not set
b) setColWidth was used with non-numeric value

If issue still occurs for you - please provide any kind of sample where problem can be reconstructed ( you can send such info directly to support@dhtmlx.com
Answer posted by Craig Efrein on Feb 09, 2009 00:16
I have created a function called resizeGrids
/* name of the grid, number of rows, column index, old width, new width */
resizeGrids(mygrid_intervenant, 3, 3, 475, 459);

so if the number of rows in the mygrid_intervenant grid is greater than 3, reduce the size of the third column to make place for the scroll bar

Here is the function that resizes the grids
/* resize the grids if more than a certain number of rows are added */
function resizeGrids(grid, num_rows, index, oldwidth, newwidth){
    var num_rows_this_grid = grid.getRowsNum();
   
    //alert(num_rows_this_grid);
   
    /* resize grids */
    if(num_rows_this_grid > num_rows){
        grid.setColWidth(index, newwidth);
    } else {
        grid.setColWidth(index, oldwidth);
    }

}

This function is called by the onXLE event.

Errors only occur, if I resize the grid after data has been loaded by loadXML.   The other problem is that the error on line 118 in the dhtmlxgrid.js file doesn't always happen.   If data is returned quickly by loadXML, IE doesn't error out.   I should also mention that there are two grids in the same form
Answer posted by Support on Feb 11, 2009 06:17
The same code works correctly in local samples ( attached )

>> The other problem is that the error on line 118 in the dhtmlxgrid.js file doesn't always happen. If data is returned quickly by loadXML, IE doesn't error out.  
It seems that it somehow caused by timing, please be sure that you have calling the correct grid instances from each onXLE handler. 
Also, be sure that you are loading page by http:// like url, when page loaded directly from filesystem ( c:/some.html ) IE can change order of onXLE calls. 
Attachments (1)