PDA

View Full Version : Get all Grid Cell Widgets



terciofilho
27 Aug 2009, 12:37 PM
I need to get every Cell widget in a Grid, I did this:



this.grid.addListener(Events.ViewReady, new com.extjs.gxt.ui.client.event.Listener<GridEvent<ModelData>>()
{
public void handleEvent(final GridEvent<ModelData> be)
{
final int columns = be.getGrid().getColumnModel().getColumnCount();
final int[] maxWidth = new int[columns];

for (int i = 0; i < columns; i++)
{
System.out.println("Column: " + i);
for (int j = 0; j < tableStore.getCount(); j++)
{
System.out.println("Row: " + j);
final Widget cellWidget = be.getGrid().getView().getWidget(j, i);

if (cellWidget != null)
{
if (cellWidget instanceof Html)
{
final String myHtml = ((Html) cellWidget).getHtml();
final int width = TextMetrics.get().getWidth(myHtml);
if (width > maxWidth[i])
{
maxWidth[i] = width;
}
}
}
else
{
System.out.println("Widget null!");
}
}
}

for (int i = 0; i < maxWidth.length; i++)
{
System.out.println(i + "=" + maxWidth[i]);
}
}
});


But all I get is a null widget.

What I'm doing wrong?

Thanks.

Arno.Nyhm
28 Aug 2009, 4:01 AM
the getWidget you only get values if you use the widgetrenderer. otherwise it is null

you look more at the getCell function of GridView

I think you try to calculate the max width of a column and adjust the column size. i remember i answered to this question some days before. i will look for this thread.

PS: there is a small bug with the forum today. you can not answer then switch your javascript off

terciofilho
28 Aug 2009, 5:57 AM
Ohh thanks!

I changed my inner for to:



final Element cell = be.getGrid().getView().getCell(j, i);

if (cell != null)
{
final int width = TextMetrics.get().getWidth(cell.getInnerHTML());
if (width > maxWidth[i])
{
maxWidth[i] = width;
}
}

Arno.Nyhm
28 Aug 2009, 10:56 AM
pls look also here :-)
there is also info how to calculate it.

grid column width - works just for the header
http://extjs.com/forum/showthread.php?t=76607