Search code examples
javagxt

GXT ValueProvider dynamic value


I need to dinamically add columns to GXT grid. I can do that, but problem occurs, when I want to input data for rows. Thing is, that not all rows have specific column. So what I want to achieve is to check if given row has specific column and return proper value.

Problem is, that ValueProvider for my column doesn't allow to use arguments in it's methods. So I can't pass column name to ValueProvider, so it could check if given column exists in specific row and return proper data.

Here is my column:

ColumnConfig<SomeClass, String> column = new ColumnConfig<SomeClass, String> (props.attributeValue(name), 150, name);

Here is my ValueProvider

ValueProvider<LimitDTO, String> attributeValue(String name);

And here is my implementation (simplified):

public String getAttributeValue(String name) {
    if(this.attributes.get(name) == null) {
        return "";
    } else {
        return this.attributes.get(name);
    }
}

But I get build error:

Method public abstract com.sencha.gxt.core.client.ValueProvider<com.example.SomeClass, java.lang.String> attributeValue(java.lang.String s) must not have parameters

SOLUTION

Thanks to your answers I was able to do it. This is my implementation of ValueProvider in case someone will look for solution. It wasn't so hard after all :)

public class CustomValueProvider implements ValueProvider<SomeClass, String> {

    public String column;

    public CustomValueProvider(String column) {
        this.column = column;
    }

    @Override
    public String getValue(SomeClass object) {
        if(object.getAttributes().get(column) == null) {
            return "";
        } else {
            return object.getAttributes().get(column);
        }
    }

    @Override
    public void setValue(SomeClass object, String value) {
    }

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

}

And here is how I used it

LimitsValueProvider lvp = new LimitsValueProvider(name);
ColumnConfig<SomeClass, String> newColumn = new ColumnConfig<>(lvp, 150, name);

Thanks a lot!


Solution

  • I would suggest, do not use

    props.attributeValue(name)
    

    Instead, you can follow the post Dynamic charts in GXT 3 and you can create your own dynamic value providers (See the section value providers), which will take columnId (path) as input and peform the same functionality.

    Remember ValueProvider is just an interface and using GWT.create you provides its default implementation.