Change the displayed column in datatables for reference field

295 Views Asked by At

I have some tables with id and descriptive data. In an additional table these tables are referenced.

Table A
id int (key)
name string
description string


Table B
id int (key)
name string
description string

Table mix
id_A FK(id tableA)
id_B FK(id tableB)

If i now create a datatable for the mix table

web mvc datatables add --type ~.web.mixController

all fields (name and description) of table A and B are shown.

1) How can I change this that only the name is shown?

2) It seems that filtering on the reference columns is not working? I get an exception: org.gvnix.web.datatables.util.DatatablesUtils - Exception preparing filter for entity

2

There are 2 best solutions below

2
On

The easiest way to modify the a entity representation is customize its toString method. By default, Spring Roo generates this as template using a Apache Commons utility which uses reflections to generate a representation string. This method is generated by @RooToString annotation and you can found it in the {entityClass}_Roo_ToString.aj file. To customize it just push-in (see Spring Roo documentation about it) the method.

About the error, as I comment on your question, we need more information to identify the problem. Please, copy some lines of stack trace.

Regards, Chema.

0
On

After digging into the source code of the controller I realized the controllers retrieve data to datatables trough the method datatablesUtilsBean_dtt.populateDataSet and it use the autowired property conversionService annotation.

So in order to change the value of the displayed field is necessary to modify the controller class ApplicationConversionServiceFactoryBean in the converter methods for example:

getMixToStringConverter() 

where Mix is the table converting to string and the return value of this method is the String representation of your entity. you could change your method like this to only show the name:

return new StringBuilder().append(mix.getTableA().getName).append(mix.getTableB().getName()).toString();