LoneWolf
LoneWolf

Reputation: 581

How to create a Grid in GXT from object without having Model Class?

We want create a Grid in GXT where data in the Grid is the JSON object, which is the output of query fired from client.

Queries are quite random and have different columns which operated on different tables, we have no clue on it.

Strictly speaking we dont have any Model class to create Grid.

How can I solve this problem.

Thanks in Advance...

Upvotes: 1

Views: 349

Answers (1)

Darek Kay
Darek Kay

Reputation: 16649

GXT grids aren't meant to be that generic, but it can be done. This would be my approach:

You need some Model class to work with. Since you don't know what data it contains, you have to make that data abstract, too. If you don't know anything about the data types either, you can use Map<Integer, String> as: columnNumber, value.

You have to create (or reconfigure()) your grid after receiving your data. After parsing your json, you know how many columns (values per entry) and rows (entries) it contains. Let's assume, you keep all your rows in a list, so we have a List<Map<Integer, String>>, where each list entry represents a row, containing a map from columnNumber to the value. Then you need such a ValueProvider:

public class RowColumnValueProvider implements ValueProvider<Integer, String> {

    private final List<Map<Integer, String>> entries;
    private final int column;

    public RowColumnValueProvider(List<Map<Integer, String>> entries, int column) {
        this.entries = entries;
        this.column = column;
    }

    @Override
    public String getValue(Integer row) {
        return entries.get(row).get(column);
    }

    @Override
    public void setValue(Integer row, String value) {
        entries.get(row).put(column, value);
    }

    @Override
    public String getPath() {
        return String.valueOf(column);
    }
}

You then create ColumConfigs with different ValueProviders (new ValueProvider(columnIndex)), create a ColumnModel and assign it to your grid.

Upvotes: 2

Related Questions