Search code examples
javagwtgxt

GXT 3 Use ListStore to display one row per value of a Set


i'm using a Liststore to display data from a DTO object into a grid. most of the object attributes are string and can easily be displayed. But one of the parameters is a Set of strings To sum up, my object looks as following:

public class MyObject{ 
    private String param1; // "val1"
    private String param2; // "val2"
    private Set<String> param3; // param3 contains "value3-1", "value3-2" and "value3-3"
    ...
}

I'd like to display a row in my grid for each element in my param3. Something like that:

param1 | param2 | param3
val1 | val2 | value3-1
val1 | val2 | value3-2
val1 | val2 | value3-3

Do you know a simple way to do this by manipulating the ListStore?

Thank you


Solution

  • Each item in the ListStore corresponds to a row in the grid - as such, you need to put data in the grid. You should be able to easily iterate over the list of MyObject instances and convert them into MyObjectRow instances - which could even contain a reference to the 'real' MyObject instance for easier editing/updating.

    However, since it is a Set, you'll want to be careful - sets have no order! This means you might not get value3-1, value3-2 value3-3, but they could arrive in any order. Strongly consider using a List instead of order matters to you at all.

    With a List then, you could have MyObjectRow look like this:

    public class MyObjectRow {
        private MyObject wrapped;
        private int index;
    
        //...
        public String getParam1() {
            return wrapped.getParam1();
        }
        public String getParam2() {
            return wrapped.getParam2();
        }
        public String getParam3() {
            return wrapped.getParam3().get(index);
        }
    }
    

    Then, for each MyObject, make N MyObjectRow, where N is the number of items in param3.