1

我正在使用带有数据源的 SmartGWT ListGrid。我成功地使用 CellFormatter 将数字文件大小数据显示为混合文本/数据(即“10 GB”而不是 10737418240)。我有过滤设置。

我想做的是让用户过滤 CellFormatter 输出,而不是基础数据。IOW,让用户在过滤框中输入“GB”,得到所有大小在GB范围内的文件。数据源在本地缓存,所以我没有关于返回服务器获取数据的问题。

编辑:我使用 CellFormatter 的原因是因为它希望排序正确,IOW 在按递增顺序排序时,我希望 200 KB 出现在 10 GB 之前,而不是之后(并且在文本排序中它们被反转)。排序对我来说比过滤更重要,所以如果我必须让排序和过滤都以相同的数据表示为目标,我将放弃过滤工作。

任何帮助将不胜感激。谢谢你,格雷格

4

1 回答 1

2

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.

于 2014-08-13T09:08:48.080 回答