DataGrid auto-set column width

2,695 views
Skip to first unread message

misko237

unread,
Oct 10, 2011, 9:49:33 AM10/10/11
to Google Web Toolkit
I have a problem with DataGrid in gwt 2.4. I made table with
CellTable, and everything worked fine. But I needed fixed header, and
then just replaced CellTable with DataGrid. I had problem with .css
file, but than has been resolved.

Now, the problem is that every column have fixed width. I need each
column width to be set as the widest cell in entire column.

Can someone help to make old looking, but with fixed header.

Thanks!

jaga

unread,
Oct 13, 2011, 8:57:36 AM10/13/11
to Google Web Toolkit
I have the same problem.

For me there are two solutions:
1) calculate the maximum field width at runtime by inspecting the row
data. Then set the field header size.
2) leave as stock and listen for a mouse click or similar on each
field header and then resize it.

misko237

unread,
Oct 14, 2011, 5:14:44 AM10/14/11
to Google Web Toolkit
I have tried the first solution, and I couldn't manage to read real
cell width at runtime. The second one is not suitable for me.

misko237

unread,
Oct 14, 2011, 9:13:13 AM10/14/11
to Google Web Toolkit
I have tried with first solution, but I couldn't manage to read real
cell element width. Did you succeed? Second solution is not an option
for me.

On Oct 13, 2:57 pm, jaga <j.annes...@gmail.com> wrote:

jaga

unread,
Oct 22, 2011, 5:18:02 PM10/22/11
to Google Web Toolkit
Okay - for the first solution I did as follows:
1) for each row of data
1.1) for each field
1.1.1) count the number of characters
1.1.2) keep a running maximum
2) finally you have an array of maximum sizes where each index
corresponds to a column, e.g. List<Integer> maxes;
3) for each cell column
3.1) set the width to the maximum using the unit EM.
Note: this is likely to make the column a little bigger than required.
You can also try the Unit EX

HTH



On Oct 14, 2:13 pm, misko237 <misk...@gmail.com> wrote:
> I have tried with first solution, but I couldn't manage to read real
> cell element width. Did you succeed? Second solution is not an option
> for me.
>

misko237

unread,
Oct 23, 2011, 10:36:13 AM10/23/11
to Google Web Toolkit
Yeah, it might be a solution (not so nice and clean, but solution)...

But, look at this
http://gwt.google.com/samples/Showcase/Showcase.html#!CwDataGrid

They've made GridTable without setting column width (there is no width
attribute on any element).

jaga

unread,
Oct 24, 2011, 5:07:31 AM10/24/11
to Google Web Toolkit
Hi,
Had a quick look at this:1) they set dataGrid.setWidth("100%") - but
this just scales it to the width of the screen. 2) they do set fixed
column widths - have a look at initTableColumns()
They are some useful calls available. Have a look at the
JavaDoc:http://google-web-toolkit.googlecode.com/svn/javadoc/latest/
com/google/gwt/user/cellview/client/
DataGrid.html#setTableWidth(double,
com.google.gwt.dom.client.Style.Unit)
James
On Oct 23, 3:36 pm, misko237 <misk...@gmail.com> wrote:
> Yeah, it might be a solution (not so nice and clean, but solution)...
>
> But, look at thishttp://gwt.google.com/samples/Showcase/Showcase.html#!CwDataGrid
>
> They've made GridTable without setting column width (there is no width
> attribute on any element).
>

misko237

unread,
Oct 24, 2011, 9:34:15 AM10/24/11
to Google Web Toolkit
OK it is 100% width, but on the link I sent to you, they have columns
with different width, and that width is not set. Am I wrong?

When I make DataGrid, I got equal column width. My table with divide
on equal parts.
eg. table width = 100px, and I have 5 columns -> every column will
have 20px width
Reply all
Reply to author
Forward
0 new messages