Show icon in DataGrid

Hi,
i’m working with a DataGrid where i want to display a simple icon (from CubaIcon) in a column.
My code is:

 jobTable.addGeneratedColumn("icon", new DataGrid.ColumnGenerator<SchedulerJobTemplate, CubaIcon>( 
 {
        @Override
        public CubaIcon getValue(DataGrid.ColumnGeneratorEvent<SchedulerJobTemplate> event) {
            return CubaIcon.valueOf(event.getItem().getIcon());
        }

        @Override
        public Class<CubaIcon> getType() {
            return CubaIcon.class;
        }
    });

    jobTable.getColumn("icon").setRenderer(jobTable.createRenderer(DataGrid.ImageRenderer.class));

The result is:
28

I’ve tried to debug WebImageRender and also CubaImageRender that seems to find the theme resource … but without correct rendering.
Thanks

This is how I have done it in the past (sorry for Groovy)

In the init method:
pricesTable.setIconProvider(new CustomIconProvider<Price>())

In the controller

/**
 * An inner class that provides an error icon to the table when the row has an error.
 * Usually we would use an anonymous class but it is confusing constructing 
 * a non static inner class in Groovy.
 */
class CustomIconProvider extends IconProvider<Price>
{
 @Nullable
 @Override
 public String getItemIcon(Price price) {
 return price.hasError() ? "font-icon:WARNING" : null
 }
}
1 Like

Many thanks,
i will use this way.

Hi,

Currently, in order to use CubaIcon as a content of a generated column, you need to implement a custom renderer. I admit that this is inconvenient and I created a GitHub issue.

Regards,
Gleb

Thanks Gleb,
at the moment i used a component generated column with a label where i set the icon.
I will change to the new Renderer when will be released.
Kind regards

1 Like

I seen was fixed into 7.0, for 6.10 no correct ? is there workaround to use cuba icon into datagrid ?
setIconProvider is for table not for datagrid

Actually, this issue was implemented for 7.1, since it’s not a bug but a feature. Unfortunately, we have no plans to backport it. Nevertheless, you can use our code as a foundation for your own implementation for 6.10+ version.