PDA

View Full Version : Grid<Bean> when bean contains some list.



valery.stroeder
28 Jan 2010, 1:08 AM
Hello everybody,

I'm facing the following problem :

I have grid displaying a list of beans (for example a Grid<Employee>). Suppose these beans and its properties:

Employee.java


String lastname
String firstname
List<Address> addresses

Address.java


String postalCode
String town
Street street

Street.java


String streetName
Integer streetNumber


My actual grid display lastname and firstname without any problem via the ColumnModel build with a columnConfig.
To display lastname and firstname I just have two columnConfigs :



myColumnConfig.setId("firstname");
myColumnConfig.setId("lastname");


What about the address ? I tried to display it using :


myColumnConfig.setId("addresses");

But it only displays a sort of : [myPackage.address@351b]

Is there a way to display the first address for example ? Is there a way to display the street class properties' values of the first address ?

Thanks for your help.

micgala
28 Jan 2010, 2:52 AM
Hi.

Best way would be splitting Visual Beans (grid BeanModel) from application Beans (your Employee with a list of addresses).

So then, you would manually add to the visual bean only the properties you need (street of the first address, for example).

You would, then, manually prepare the visual bean from your application bean.

Hope this helps you.

Regards,
Michel.

sven
28 Jan 2010, 2:56 AM
myColumnConfig.setDataIndex("addresses[0].town");


That will display the down of the first address.

valery.stroeder
28 Jan 2010, 3:19 AM
Thanks both for your answers, it helps me a lot.

Is there a way to make a sort of query ?

Suppose the addresses' list is an history list

Address.java


String postalCode
Date startdate
Date endDate

Should I get the only address where endDate is null ?

micGala I understand your solution and its advantages. But I'd prefere don't use special beans for visual if possible.

sven
28 Jan 2010, 3:21 AM
micGala I understand your solution and its advantages. But I'd prefere don't use special beans for visual if possible.

You should always use DTOs to only transfer the data you want to display in theend.

valery.stroeder
28 Jan 2010, 3:37 AM
OK, but it's not the choice made for this application, the client receive the entire beans with its associated beans (I can't change it).

This example doesn't work for me

myColumnConfig.setDataIndex("addresses[0].town");Should I change something else ? the setID for example ?

If it works, should I do something like this ?

myColumnConfig.setDataIndex("adresses[0].street.name"+" "+
"adresses[0].street.number");

sven
28 Jan 2010, 3:59 AM
That cannot work, a dataindex is only ONE field. if you need to display multiply once, simple define a custom gridcellrenderer

valery.stroeder
28 Jan 2010, 4:23 AM
Thanks for your help. If someone else need this, here is my solution :



columnConfig = new ColumnConfig();
columnConfig.setHeader("addresse");
columnConfig.setRenderer(new GridCellRenderer<BeanModel>() {
public Object render(final BeanModel beanModel, final String s, final ColumnData columnData, final int i, final int i1, final ListStore<BeanModel> beanModelListStore, final Grid<BeanModel> beanModelGrid) {
List<Address> addresses;
String returnTemp;

returnTemp = "/";
adresses = new ArrayList(((Employee) beanModel.getBean()).getAddresses());
for (Address address : addresses) {
if (address.getEndDate() == null) {
try {
returnTemp = address.getStreetName() + " "+address.getStreetNumber();
} catch (NullPointerException e) {
returnTemp="/";
}
}
}
return returnTemp;
}
});
This code display the streetNumber and StreetName of an employee's address.
Here I suppose only one address per employee hasn't any endDate.