I have an HLayout which has a form on the left and a grid on the right. I have sized the form to be exactly 300px wide and I would like the grid to expand and contract as needed to fit the remaining space. The user can change the data source for the grid at runtime and I would like the grid to resize itself to accommodate the new column set, using as much space as is available and, if necessary flowing off the page to the right with a horizontal scroll bar. I've tried using grid.setAutoFitData(Autofit.HORIZONTAL) and grid.redraw() after setting the datasource and loading the grid with data, but the grid always remains at the same width it was originally, based on the initial datasource which has just a few columns. When I switch to a datasource with more columns they just get scrunched down into the same amount of space with lots of open whitespace unused. I've tried putting the grid into a separate layout and also without a layout and nothing seems to work. How should I construct the canvas so the grid will expand/contract as needed?
Announcement
Collapse
No announcement yet.
X
-
I can't seem to get this to work. I've used the code from the Pattern Reuse example in the SmartGWT Showcase. The difference is that I've put the grid on the right hand side of the layout instead of the left so that it can expand to the right as needed. I added the code you suggested to the setDataSource method which now looks like this.Code:public void setDatasource(DataSource datasource) { this.datasource = datasource; grid.setDataSource(datasource); form.setDataSource(datasource); saveButton.disable(); grid.fetchData(); // Resize the grid to fit the new data source ListGrid g = new ListGrid(); // offscreen grid g.setOverflow(Overflow.VISIBLE); g.setBodyOverflow(Overflow.VISIBLE); g.setShowHeader(false); g.setDataSource(datasource); ListGridField[] gf = g.getFields(); for (int i=0; i<gf.length; i++) { grid.getField(i).setWidth(gf[i].getWidth()); } grid.markForRedraw(); }
Comment
-
I may have stumbled upon a solution, but it has a bad side effect. Using grid.setFixedFieldWidths(false); has exactly the desired effect on the column widths. They size themselves automagically to the contents of each column. The only problem is that the column headings do not size accordingly, so you end up with column headings that don't line up with the columns. Screen shot attached.
A note in the javadocs says "NOTE: the header does not automatically respond to expanded field widths". Is there a way to make the header respond appropriately?Last edited by jay.l.fisher; 1 Dec 2009, 06:43.
Comment
-
Hi Jay,
Yes, headerless autoFit is currently the state of this feature. This can be combined with offscreen drawing to do one-shot autofit of a grid by drawing a parallel grid offscreen, measuring rendered fieldWidths, and applying them to the fields of a newly created grid.
We do plan an enhancement to do something close to this automatically.
Comment
-
Originally posted by IsomorphicThis can be combined with offscreen drawing to do one-shot autofit of a grid by drawing a parallel grid offscreen, measuring rendered fieldWidths, and applying them to the fields of a newly created grid..
Comment
-
Still not able to get this to work. Here's my latest code, added to the Pattern Reuse example.
Code:public void setDatasource(DataSource datasource) { this.datasource = datasource; grid.setDataSource(datasource); form.setDataSource(datasource); saveButton.disable(); grid.fetchData(); // Resize the grid to fit the new data source ListGrid g = new ListGrid(); // offscreen grid g.setOverflow(Overflow.VISIBLE); g.setBodyOverflow(Overflow.VISIBLE); g.setAutoFitData(Autofit.HORIZONTAL); g.setFixedFieldWidths(false); g.setShowHeader(true); g.setDataSource(datasource); g.fetchData(); g.hide(); g.draw(); ListGridField[] gf = g.getFields(); for (int i=0; i<gf.length; i++) { SC.logWarn("Width should change from " + grid.getField(i).getWidth() + " to " + gf[i].getWidth()); grid.getField(i).setWidth(gf[i].getWidth()); } grid.redraw(); }
Code:10:15:12.219:WARN:Log:Width should change from null to null 10:15:12.220:WARN:Log:Width should change from null to null 10:15:12.221:WARN:Log:Width should change from null to null 10:15:12.222:WARN:Log:Width should change from null to null 10:15:12.223:WARN:Log:Width should change from 21 to 21
Comment
Comment