我正在尝试使用 GXT 框架创建一个相当简单的 GWT 应用程序,并且是基于电子书“Ext GWT 2.0 初学者指南”来完成此操作。它详细介绍了使用 GXT MVC 模型构建应用程序的所有步骤,其中大部分都按预期工作,但是我无法(在其生命周期中)向我的网格添加新行。
网格数据的初始加载(通过 itemStore、加载器和 RPCProxy 工作正常)。
但是,每次我触发触发添加新行的事件时,我要添加的 ListStore 都是空的(不是空!而是空) - 即使我的网格在初始加载后显示数据!
以下是一些相关代码:
public class MessageGrid extends LayoutContainer {
private final Grid<ModelData> grid;
private final ListLoader<ListLoadResult<MarketingMessage>> loader;
private final ListStore<ModelData> itemStore;
public MessageGrid() {
setLayout(new FitLayout());
final MarketingServiceAsync marketingService = Registry.get(MarketingUIConstants.MARKETING_SERVICE);
RpcProxy<List<MarketingMessage>> proxy = new RpcProxy<List<MarketingMessage>>() {
@Override
protected void load(Object loadConfig, final AsyncCallback<List<MarketingMessage>> callback) {
marketingService.getMessages(callback);
}
};
final List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
columns.add(new ColumnConfig("col1", "Name", 100));
columns.add(new ColumnConfig("col2", "Phone", 100));
final ColumnModel columnModel = new ColumnModel(columns);
loader = new BaseListLoader<ListLoadResult<MarketingMessage>>(proxy);
itemStore = new ListStore<ModelData>(loader);
grid = new Grid<ModelData>(itemStore, columnModel);
grid.setBorders(true);
grid.setAutoExpandColumn("sentTime");
grid.setAutoHeight(false);
grid.setHeight("100%");
}
@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
loader.load();
add(grid);
}
public void addMessage(MarketingMessage marketingMessage) {
itemStore.add(new MarketingMessage("test", "test")));
}
}
addMessage() 正在由所属组件(已添加此 LayoutContainer 的 View )调用。
如果我在 addMessage() 方法中设置断点,我的 itemStore 的大小为 0(对于 grid.getStore() 来说也是如此)。
我已尝试对此代码进行各种不同的更改,但尚未看到添加行。
类似的说明:如果在 addMessage() 方法内,我尝试从代理重新加载 - 因为我也更新服务器端数据 - 即。 loader.load(),它也没有任何可见的效果。
最佳答案
加载程序将是异步的 - 它需要等待服务器 (MarketingService) 返回值。您确定这返回一个非空列表吗?通过将代理中的回调替换为您自己的回调来验证这一点,该回调检查返回给客户端的内容(如果服务器抛出异常,它甚至可能根本不会被调用)。
如果在 onRender 或此后不久调用 addMessage,则可能为时过早,服务器可能还没有返回数据,因此存储可能仍为空。如果通过单击按钮或其他方式调用,这不会成为问题。
检查日志中是否有异常,您正在将处理项目列表的代理传递给需要 ListLoadResult 的加载器 - 这也可能是错误来源。 GXT 2 中令人困惑的泛型应该在目前处于测试阶段的 3 中得到解决。
关于java - 如何向 GXT 网格添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8842590/