I have a Table with a colour coded and styled button in each row. I have been trying to recreate this using the 7.4 Grid with a ButtonRenderer in the column, however I can see no way to style the Button in the ButtonRenderer.
Can somebody please tell me how I am supposed to do that?
Well, the primary style is set in ButtonRenderer.createWidget() with setPrimaryStyleName(), but in your case you probably want to set or add additional styles in render() with setStyleName() or addStyleName() (maybe also use button.removeStyleName()), as you want to have it based on the value. You could alternatively just modify the style attribute of the button there.
com.vaadin.client.renderers.ButtonRenderer - implement your style logic in the render() method here
com.vaadin.ui.renderers.ButtonRenderer - mostly just extend as is, nothing else is needed but the constructors
com.vaadin.client.connectors.ButtonRendererConnector - create your renderer instead of ButtonRenderer and have @Connect bind to your server-side renderer class
Yes. The package structure must be like in
regular client-server integration . The client-side code (the client-side renderer and the connector) must be under the “client” package under the package of the widgetset descriptor. The server-side renderer class must be with your other server-side code.
You probably won’t need the shared state or other classes, only the three mentioned classes, which should be fairly simple.
There’s also
here some general stuff about component extensions (the Grid renderers are one kind of extensions).
very cool. I have my new button renderer extension compiling and applying a fixed style.
Can you suggest what the correct mechanism is for rendering the button in a different colour (i.e. styling) based on the item in the row. This item will never change and so once rendered the button does not need to change.
Would shared state be the correct way forward with this? Or is there another mechanism for dynamically styling each renderer based on the row?
Yes, at least if you extend ClickableRenderer instead of ButtonRenderer, you can use other data types than the String for values, and can get the style attribute or whatever from the property value.
Or, if you extend ButtonRenderer, which was my original suggestion, you can just encode necessary stuff (such as a style) in the String value, which is usually used for the caption, and decode that in the render() method and use however you like. It’s maybe a bit hacky that way, but anyhow.
As my buttons always have the same lable, but different colours depending on the status, I am passing the status through as a string and using that to set the style whilst setting a constant lable. Works very well, and isn’t hacky at all
I need to do this as well. Nothing complicated, just something equivalent to say doing an .addStyleName(BUTTON_LINK) to the button created by the renderer.
Conrad, could you please provide us with a working example on how to implement my own ButtonRenderer in order to add custom styles, such as icons, to a Button? I’ve been stuck on this for days. Thanks.
In principle you could also use
ClickableTextRenderer Add-on by making sure the content of your cell is some HTML text like the result of
FontAwesome.TRASH.getHtml()
That will give you a “delete” icon which can perform some action when clicked upon. However I’m guessing GridActionRenderer Add-on is the most appropriate for your case.
Finally there’s the
Context Menu Add-on by Vaadin Ltd, which isn’t a renderer at all but a feature by which you can add a right-click menu to rows in a grid. It is an alternative to ‘action buttons’ in the grid itself.