如何向 GXT 网格添加行

发布于 2024-12-26 10:20:16 字数 2290 浏览 2 评论 0原文

我正在尝试使用 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(),它没有任何可见的效果。

I'm trying to create a fairly simply GWT app using the GXT framework, and am doing this based on the eBook "Ext GWT 2.0 Beginners Guide". It details all the step for building an app using GXTs MVC model, and most of it is working as expected, however I can not - for the life of it - add new rows to my Grid.

The initial loading of the Grid data (through itemStore, loader, and an RPCProxy work correct).

However every time I fire the event that triggers adding a new row, the ListStore to which I want to add, is empty (not null! but empty) - even though my Grid is showing data after the initial load!

Here is some of the relevant code:

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() is being called by the owning component (a View to which this LayoutContainer has been added).

If I set a breakpoint inside the addMessage() method, my itemStore has size 0 (same goes for grid.getStore() for that matter).

I've tried various different changes to this code and I have yet to see a row being added.

On a similar note: if inside the addMessage() method I try to do a reload from the proxy - since I do update the server side data as well - ie. loader.load(), it also does not have any visible effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不爱素颜 2025-01-02 10:20:16

加载程序将是异步的 - 它需要等待服务器(MarketingService)返回值。您确定这返回一个非空列表吗?通过将代理中的回调替换为您自己的回调来验证这一点,该回调检查返回给客户端的内容(如果服务器抛出异常,它甚至可能根本不会被调用)。

如果在 onRender 或此后不久调用 addMessage,则可能为时过早,服务器可能还没有返回数据,因此存储可能仍为空。如果通过单击按钮或其他方式调用,这不会成为问题。

检查日志中是否有异常,您正在将处理项目列表的代理传递给需要 ListLoadResult 的加载器 - 这也可能是错误来源。 GXT 2 中令人困惑的泛型应该在目前处于测试阶段的 3 中得到解决。

The loader is going to be asynchronous - it needs to wait for the server (MarketingService) to return values. Are you certain that this is returning a non-empty list? Verify this by replacing the callback in your proxy with your own callback that checks the contents returned to the client (it may not even be called at all if the server threw an exception).

If addMessage is called onRender or shortly thereafter, it may be too early, and the server may not have returned data yet, so the store may still be empty. If called from a button click or something, this won't be an issue.

Check your log for exceptions, you are passing a Proxy that handles a List of items to a Loader that expects a ListLoadResult - this could also be a source of errors. The confusing generics in GXT 2 should mostly be resolved in 3, which is currently in beta.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文