如何正确地将数据添加到GXT网格?

时间:2013-04-25 12:39:36

标签: java gxt asynccallback

我有一个webapp,我需要从文件中获取一些数据并填写到表中。以下是包含此表的页面代码:

public class Rules extends ContentPanel{
    private final ServerManagementAsync serverManagementSvc = GWT.create(ServerManagement.class);
    private ArrayList<PropertyItem> propslist;
    private ArrayList<PropertyItem> itemArrayList;
    private EditorGrid<PropertyItem> grid;

    public Rules(final String customerId){
        setLayout(new FlowLayout(10));

        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

        ColumnConfig column = new ColumnConfig();
        column.setId("name");
        column.setHeader("Name");
        column.setWidth(220);

        TextField<String> text = new TextField<String>();
        text.setAllowBlank(false);
        column.setEditor(new CellEditor(text));
        configs.add(column);

        column = new ColumnConfig();
        column.setId("type");
        column.setHeader("Type");
        column.setWidth(220);

        TextField<String> typeText = new TextField<String>();
        typeText.setAllowBlank(false);
        column.setEditor(new CellEditor(typeText));
        configs.add(column);

        column = new ColumnConfig();
        column.setId("value");
        column.setHeader("Value");
        column.setWidth(220);
        configs.add(column);

        final ListStore<PropertyItem> store = new ListStore<PropertyItem>();
        propslist = getPropslist(customerId);

        for (PropertyItem propertyItem: propslist){
            store.insert(propertyItem, 0);
        }

        ColumnModel cm = new ColumnModel(configs);

        setHeading("Settings");
        setFrame(true);
        setWidth(600);
        setLayout(new FitLayout());

        grid = new EditorGrid<PropertyItem>(store, cm);
        grid.setAutoExpandColumn("name");
        grid.setBorders(true);
        add(grid);

        ToolBar toolBar = new ToolBar();
        setTopComponent(toolBar);
        setButtonAlign(Style.HorizontalAlignment.RIGHT);

        addButton(new Button("Refresh", new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                store.insert(getPropslist(customerId), 5);
            }
        }));
        addButton(new Button("Add", new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                store.commitChanges();
            }
        }));


    }

    public ArrayList<PropertyItem> getPropslist(String customerId){
        itemArrayList = new ArrayList<PropertyItem>();
        AsyncCallback<ArrayList<PropertyItem>> callback = new AsyncCallback<ArrayList<PropertyItem>>() {
            @Override
            public void onFailure(Throwable throwable) {

            }

            @Override
            public void onSuccess(ArrayList<PropertyItem> propertyItems) {
                itemArrayList = propertyItems;
                Window.alert("read successful");
                }
            }
        };
        serverManagementSvc.getProperties(customerId, callback);
        return itemArrayList;
    }
}

当我运行我的应用程序时,我只看到列名称,单元格中没有数据(当然也没有单元格)。作为示例,我使用了这个:Ext GWT 2.2.6 Explorer(Grid)

我不明白什么可能导致这样的问题。这可能是什么原因?

1 个答案:

答案 0 :(得分:1)

因为正在异步填充propsList,所以在服务器调用返回之前呈现网格。

要解决此问题,首先要使商店成为网格

之类的私有财产

接下来,移动填充商店的代码:

    for (PropertyItem propertyItem: propslist){
        store.insert(propertyItem, 0);
    }

进入回调的onSuccess方法。

最后,您可能还需要致电: grid.reconfigure(grid.getStore(), grid.getColumnModel()); 只是让Grid再次渲染。