-
5 Jan 2012 9:03 AM #1
Unanswered: Grid row rendering?
Unanswered: Grid row rendering?
I have a standard grid containing some domain objects (which have String/Integer/Boolean values) with a PropertyAccess to load those values automatically. This works very good and all values are rendered by the corresponding object values.
Now I want to format/render my row by the object's boolean value. Assuming it's true, all corresponding cells should be rendered as <b>value</b> - and normally otherwise. The only way I've found so far was to create my column:
and to define the Cell:Code:ColumnConfig c = new ColumnConfig(new IdentityValueProvider<MyObject>(), ....);
Having several columns I need to implement a different cell for each of them, containing almost the same code except the value.getName() part - seems pretty redundant to me. Is there no other way to achieve this?Code:column.setCell(new AbstractCell<MyObject>() { @Override public void render(com.google.gwt.cell.client.Cell.Context context, MyObject value, SafeHtmlBuilder sb) { if (value.isActive()) sb.appendHtmlConstant("<b>" + value.getName() + "</b>"); else sb.appendEscaped(value.getName()); } });
Using Firebug, I just added style="font-weight: bold" to the <tr> (or <table>) element containing my desired row. Is there any way to step into the row rendering process and add my style depending on the underlying object's value?
-
5 Jan 2012 9:31 AM #2
You could create a base class that has all your logic. You also do not have to use a IdentityValueProvider and so you would not need different cells. Instead of using AbstractCell you can also use TextCell with a custom SafeHtmlRendererHaving several columns I need to implement a different cell for each of them, containing almost the same code except the value.getName() part - seems pretty redundant to me. Is there no other way to achieve this?
-
11 Jan 2012 2:42 AM #3
Thank you for your reply.
The problem is: such a custom cell renderer has no knowledge about other cells or object values (which is understandable) - and that's the point: I want to set a different style/renderer depending on values from another column. Maybe that's what you mean by "create a base class that has all your logic" - but how exactly would that look like?
-
25 Jan 2012 5:02 PM #4
Hi,
You can access other columns in your custom render() method. Construct your grid as normal (with a store and column model).
For example:
Then access your model (row) in the render method:Code:@Inject public AbstractAdministrationView() { super(); this.setGrid(new ContextAreaGrid(new ContextAreaModelListStore(), getContextAreaColumModel())); this.grid.setSize(CONTEXT_AREA_WIDTH, CONTEXT_AREA_HEIGHT); // add the Grid to the View's layout container this.panel.add(this.grid, new VerticalLayoutData(1, 1)); ... }
The context passed to your render() method includes the index of the row (context.getIndex())Code:public ColumnModel<ContextAreaModel> getContextAreaColumModel() { ... ColumnConfig<ContextAreaModel, String> column1DisplayNameColumnConfig = new ColumnConfig<ContextAreaModel, String>(property.column1DisplayName(), ContextAreaGrid.DISPLAY_NAME_COLUMN_WIDTH, ""); column1DisplayNameColumnConfig.setCell(new AbstractSafeHtmlCell<String>(SimpleSafeHtmlRenderer.getInstance()) { @Override protected void render(Context context, SafeHtml data, SafeHtmlBuilder sb) { if (data == null) { return; } ContextAreaModel model = (ContextAreaModel) getGrid().getStore().get(context.getIndex()); sb.appendHtmlConstant("<span>" + "<b>" + data.asString() + "</b><br />" + model.getColumn1Description() + "</span>"); } }); ... List<ColumnConfig<ContextAreaModel, ?>> columnConfigList = new ArrayList<ColumnConfig<ContextAreaModel, ?>>(); columnConfigList.add(column1IconColumnConfig); columnConfigList.add(column1DisplayNameColumnConfig); columnConfigList.add(column2IconColumnConfig); columnConfigList.add(column2DisplayNameColumnConfig); ColumnModel<ContextAreaModel> columnModel = new ColumnModel<ContextAreaModel>(columnConfigList); return columnModel; }
Cheers
Rob
http://code.google.com/p/gwt-cx/
-
26 Jan 2012 4:38 AM #5
I think this is GXT 2 code, but nevertheless it led me to the solution

Big Thanks!


Reply With Quote