PDA

View Full Version : grid column width - works just for the header



christian.schmuelling
4 Aug 2009, 11:00 PM
Hello.

I've some trouble with the Grid. The column width should fit to the largest Text in this column. I'm doing the following steps:
1. create Grid, ColumnModel an ColumnConfig - works fine
2. create a new GridCellRenderer for my ColumnConfig - works fine
3. Inside the GridCellRenderer I calculate the width of my text. (with TextMetrics) - works fine
4. Inside the GridCellRenderer I set the new Column with on the ColumnModel (myGrid.getColumnModel().setColumnWidth( colIndex, width );) :-/

The new width ist just visible at the column header. It's the correct calculated width. My content table didn't get the new column width. ??? I don't know whats going wrong. If you press on the column header and try to resize the column with (without moving to right or left) the column gets resized to the calculated width.

Who knows what's going wrong?
Is there a better way to fit all columns to the max text with?
Why ist just my grid header resizing?

I'm using GWT 1.7 and GXT 2.01. Developing under windows. Hosted mode an compiled project show the same result.

With best regards
Christian

christian.schmuelling
7 Aug 2009, 12:25 AM
Ok, nobody else has this problem? Or nobody else tried it befor?

Arno.Nyhm
7 Aug 2009, 4:40 AM
1. it would be nice if you give use a small working code snippet to see what you do :-)


2. i looked in the setColumnWidth command. and i see that it sets all the width of the rows but your gridcellrendere is called before the full gridview is displayed.


solutions:
1. calculate width with gridcellrenderer and set it later

you calculate your widths with the gridcellrenderer and remember the width of its column. after all rendering is finished then you set the column width your command.


2. calclulate the column widths later and set it later
this is more a genral solution. because you can leave already existing renderers at the grid and calculate the column width later and shrink it later.

PS: if you have a working code of it would be nice if you can post it here :-)
PS: you can use the event Events.ViewReady
grid.addListener(Events.ViewReady, new Listener<BaseEvent>() {

public void handleEvent(BaseEvent be) {
// do calculations
}
});

christian.schmuelling
7 Aug 2009, 6:42 AM
Thanks for your reply.

The following code works just in hosted mode:



lGrid.addListener( Events.ViewReady, new Listener<GridEvent>()
{
public void handleEvent( GridEvent pGridEvent )
{
int lCount = pGridEvent.getGrid().getColumnModel().getColumnCount();
int[] lMaxWidth = new int[lCount];

//calculate width for each row in my Model
for( int i = 0; i < _store.getCount(); i++ )
{
//calculate width for each column in my grid
for( int y = 0; y < lCount; y++ )
{
Widget lWidget = pGridEvent.getGrid().getView().getWidget( i, y );
if( lWidget != null )
{
int lWidth = lWidget.getOffsetWidth();
if( lWidth > lMaxWidth[y] )
{
lMaxWidth[y] = lWidth;
}
}
}
}

//calculate width for each column header
for( int y = 0; y < lCount; y++ )
{
String lHeader = pGridEvent.getGrid().getColumnModel().getColumnHeader( y );
if( lHeader != null )
{
int lWidth = TextMetrics.get().getWidth( lHeader );
if( lWidth > lMaxWidth[y] )
{
lMaxWidth[y] = lWidth;
}
}
}

//Set width on my table
for( int y = 0; y < lMaxWidth.length; y++ )
{
if( lMaxWidth[y] != 0 )
{
//I didn't calculate the width exact. so I'll add 15px foe each column
pGridEvent.getGrid().getColumnModel().getColumn( y ).setWidth( lMaxWidth[y] + 15 );
}
}

//Refresh the view
pGridEvent.getGrid().getView().refresh( true );
}
} );
In Internetexplorer (8) there'll be no content on my grid ... !?! And new column width isn't working.
In Firefox (3.5) the parent ContentPanel will not be resized automaticaly. My ContentPanel get scrollbars.

In hosted mode everything works fine. Column with is set right. ContentPanel got the same size like my grid.

christian.schmuelling
12 Aug 2009, 2:04 AM
Nothing from these ways seems to work on Firefox:



/* 1 */
int width = myWidget.getOffsetWidth();

/* 2 */
if( myWidget instanceof BoxComponent )
{
int width = ((BoxComponent)myWidget).getSize().width;
}

/* 3 */
int width = myWidget.getElement().getClientWidth();

/* 4 (ok, it looks strange, but it was my last idea)*/
int width = TextMetrics.get().getWidth( myWidget.toString() );


returns 0 in Firefox (3.5)

Tested at ViewReady event
(myGrid.addListener( Events.ViewReady, new Listener<GridEvent>())

christian.schmuelling
12 Aug 2009, 4:00 AM
This version works in Firefox :)


if( myWidget instanceof Html )
{

String myHtml = ((Html)myWidget).getHtml();
int width = TextMetrics.get().getWidth( myHtml );

}

Arno.Nyhm
13 Aug 2009, 6:25 AM
great!

can you please post the full listener?

kiran.vennampelli
18 May 2011, 9:18 PM
particular Row CSS setting is // headers you can use this.

grid.getRowFormatter().setStyleName(i, "display-RatePlan-Grid-Header"); //i is the row number


you can make the particular cell width 100 % using the below

grid.getCellFormatter().setWordWrap(i, j, false); //i row number j column number