SmartGWT ListGrid with DataSource, Filter on CellFormatter output

974 Views Asked by At

I'm using a SmartGWT ListGrid with a DataSource. I'm successfully using a CellFormatter to display numeric file size data as mixed text / data (i.e. "10 GB" rather than 10737418240). I have filtering set up.

What I'd like to do is to let the user filter on the CellFormatter output, rather than on the underlying data. IOW, let the user type "GB" into the filter box, and get all the files with sizes in the GB range. The DataSource is cached locally, so I don't have issues about going back to the server to get data.

Edit: the reason why I'm using a CellFormatter is because it want sorting to be correct, IOW when sorting in increasing order I want 200 KB to come before 10 GB, not after (and in a text sort they're reversed). Sorting is more important to me than filtering, so if I have to have both sorting and filtering target the same representation of the data, I'll just give up on filtering working.

Any help would be greatly appreciated. Thank you, Greg

1

There are 1 best solutions below

3
On BEST ANSWER

You have two options to do this. First is to return already modified values from your datasource, so instead of 10737418240 it should return "10 GB" string value.

The second approach seems better for me - you should use SimpleType functionality. There is an example for you:

public class PopulationType extends SimpleType {

    public PopulationType() {
        super("population", FieldType.TEXT);
        // format values in the grid
        this.setSimpleTypeValueExtractor(new SimpleTypeValueExtractor() {
            @Override
            public Object getAtomicValue(Object value) {
                if (value instanceof Integer && ((Integer) value) > 1000000) {
                    return ((Integer) value) / 1000000 + " Mln";
                }
                return "" + value;
            }
        });
    }
}

public void onModuleLoad() {
    final ListGrid countryGrid = new ListGrid();  
    countryGrid.setWidth100();
    countryGrid.setHeight100();
    countryGrid.setAutoFetchData(true);
    countryGrid.setShowFilterEditor(true);
    countryGrid.setShowAllRecords(true);
    WorldXmlDS ds = WorldXmlDS.getInstance();
    ds.getField("population").setType(new PopulationType());
    countryGrid.setDataSource(ds);
    countryGrid.draw();
}

You set your SimpleType instance to a field you want to format and set SimpleTypeValueExtractor to override getAtomicValue which is used for showing,filtering,sorting.

There are other methods you could override - e.g. if you need to edit values in your grid you should probably set SimpleTypeValueUpdater as well.