Formatting Values in Grid Columns

Hi,

I have a grid that reads values from a Bean Class.

private List<MyBean> results = new ArrayList<MyBean>(); private Grid<MyBean> myGrid = new Grid<>(MyBean.class); myGrid.setItems(results);

Sample Grid

| ID | Name | Amount |

| 00 | ABC | 1.09123|

I want to format the decimal values to round them off to 2 digits precision.



Modified Grid

| ID | Name | Amount |

| 00 | ABC | 1.10 |

How can I get the desired results?

Thanks in advance.

Hi,
Maybe the parts Generating columns and Column renderers (close to bottom of page) from the docs could help you: https://vaadin.com/docs/v8/framework/components/components-grid.html

-Katri

Grid.addColumn(…) accepts also lambda expression as a parameter for providing the value to Grid. So if the conversion or formating is simple enough, that can be used.

That was my first guess. However, when i try to use mygrid.getColumn(“Amount”).setRenderer(…) it says


“The method setRenderer(Renderer<? super capture#13-of ?>) in the type Grid.Column<MyBean,capture#13-of ?> is not applicable for the arguments ()”

I am not able to fetch the value of the Grid itself.

Ok, so i used

myGrid.addColumn(MyBean::getAmount, new NumberRenderer(new DecimalFormat("$ #.##"))); Result Grid

| ID | Name | Amount | |

| 00 | ABC | 1.98888 | $ 1.99 |

It seems to convert the column, but now i am getting an extra column with no heading, i want to update the existing “Amount” column.

Note: I am using setItems() to fetch fields from the bean and set them as column, in my application there are a lot of columns and only few require formatting so adding columns one by one is possible(but not ideal) but I wanted to know if there is any alternative.

I see, then your case is not very difficult, you should be able to use built in NumberRenderer


https://vaadin.com/download/release/8.1/8.1.7/docs/api/com/vaadin/ui/renderers/NumberRenderer.html

Just use it, you can give format specification in constructor parameter, in your case it is DecimalFormat


https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html

And here is example of format string to be used in your specific case


https://stackoverflow.com/questions/26706784/how-to-make-0-display-as-0-00-using-decimal-format

Thank you, but it still does not solve my problem.

//this works perfectly but creates a new column
myGrid.addColumn(MyBean::getAmount,
                new NumberRenderer(new DecimalFormat("$ #,##0.00")));

//I want to update the column instead of creating a new one but it throws me an error
myGrid.getColumn("amount").setRenderer(new NumberRenderer(new DecimalFormat("$ #,##0.00")));
error on Line 6:
The method setRenderer(Renderer<? super capture#6-of ?>) in the type Grid.Column<MyBean,capture#6-of ?> is not applicable for the arguments ()

What am I doing wrong??

Have you tried alternative method e.g.?

myGrid.getColumn(“amount”).setRenderer(MyBean::getAmount, new NumberRenderer(new DecimalFormat(“$ #,##0.00”)));

See more here: https://vaadin.com/download/release/8.1/8.1.7/docs/api/com/vaadin/ui/Grid.Column.html#setRenderer-com.vaadin.data.ValueProvider-com.vaadin.ui.renderers.Renderer-

And also refering to this issue: https://github.com/vaadin/framework/issues/10277

Here is one gotcha of Vaadin 8 Grid with the bean-scan method of generating columns. When you call getColumn(“propertyName”), that won’t know the correct generic value type of the column, since it is only determined runtime. You should try reading the column to a separate variable of the correct generic type (this probably requires a typecast) and that will then make the setRenderer also use the correct implied type.

-Olli

This is exactly what I was looking for! It worked like a charm, Thank you!
All I had to do was create a Column variable and typecast it before using setRenderer().

Thank you all for the suggestions!

hI sajid Sid,

I have the same problem but in different situation

Sample Grid

| state | Name |

| 00 | ABC |

I want to change the value of the state which 0 into a Available. how can I do this? How did u solved this problem… sorry I am just a beginner

Modified Grid

| state | Name |

| available | ABC |

Hi Adrian,

There is answer to your question in this thread:

[https://vaadin.com/forum/#!/thread/16947748]
(https://vaadin.com/forum/#!/thread/16947748)