简体   繁体   English

为什么我的网格在带有分页工具栏(GWT 2.4)的第二页上不移动?

[英]Why my grid does not move on second page with paging toolbar(GWT 2.4)?

I am developing a GWT app where I am using paging toolbar. 我正在开发使用分页工具栏的GWT应用程序。 When I have more than 10 groups in grid, user can go to second page with paging toolbar. 当我在网格中有10个以上的组时,用户可以使用分页工具栏转到第二页。 But when I press button to go to the second page, it goes to that second, loading is shown but then toolbar is back to the first page with those first. 但是,当我按按钮转到第二页时,它转到第二页,显示了加载,但工具栏又回到第一页。 10 items. 10项。 This is first page: 这是第一页: 在此处输入图片说明

And when I press button for second page I get this loading: 当我按下第二页按钮时,我得到了这个加载:

在此处输入图片说明

But then after that toolbar backs me to the first page. 但是之后,该工具栏将我带回到第一页。 This is my class for paging toolbar: 这是我的分页工具栏类:

public class MyPagingToolBar extends PagingToolBar {

    private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);

    public MyPagingToolBar(int pageSize) {
        super(pageSize);

        PagingToolBarMessages pagingToolbarMessages = getMessages();
        pagingToolbarMessages.setBeforePageText(MSGS.pagingToolbarPage());
        pagingToolbarMessages.setAfterPageText(MSGS.pagingToolbarOf().concat("{0}"));

        StringBuilder sb = new StringBuilder();
        sb.append(MSGS.pagingToolbarShowingPre())
                .append(" {0} - {1} ")
                .append(MSGS.pagingToolbarShowingMid())
                .append(" {2} ")
                .append(MSGS.pagingToolbarShowingPost());
        pagingToolbarMessages.setDisplayMsg(sb.toString());

        pagingToolbarMessages.setEmptyMsg(MSGS.pagingToolbarNoResult());

        pagingToolbarMessages.setFirstText(MSGS.pagingToolbarFirstPage());
        pagingToolbarMessages.setPrevText(MSGS.pagingToolbarPrevPage());
        pagingToolbarMessages.setNextText(MSGS.pagingToolbarNextPage());
        pagingToolbarMessages.setLastText(MSGS.pagingToolbarLastPage());
        pagingToolbarMessages.setRefreshText(MSGS.pagingToolbarRefresh());
    }
}

And this is class where I using MyPagingToolbar: 这是我使用MyPagingToolbar的类:

public abstract class EntityGrid<M extends GwtEntityModel> extends ContentPanel {

    private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);

    private static final int ENTITY_PAGE_SIZE = 10;

    protected GwtSession currentSession;
    private AbstractEntityView<M> parentEntityView;

    private EntityCRUDToolbar<M> entityCRUDToolbar;
    protected KapuaGrid<M> entityGrid;
    protected BasePagingLoader<PagingLoadResult<M>> entityLoader;
    protected ListStore<M> entityStore;
    protected PagingToolBar entityPagingToolbar;
    protected EntityFilterPanel<M> filterPanel;

    protected EntityGrid(AbstractEntityView<M> entityView, GwtSession currentSession) {
        super(new FitLayout());
        //
        // Set other properties
        this.parentEntityView = entityView;
        this.currentSession = currentSession;

        //
        // Container borders
        setBorders(false);
        setBodyBorder(true);
        setHeaderVisible(false);

        //
        // CRUD toolbar
        entityCRUDToolbar = getToolbar();
        if (entityCRUDToolbar != null) {
            setTopComponent(entityCRUDToolbar);
        }
        //
        // Paging toolbar
        entityPagingToolbar = getPagingToolbar();
        if (entityPagingToolbar != null) {
            setBottomComponent(entityPagingToolbar);
        }
    }

    @Override
    protected void onRender(Element target, int index) {
        super.onRender(target, index);

        //
        // Configure Entity Grid

        // Data Proxy
        RpcProxy<PagingLoadResult<M>> dataProxy = getDataProxy();

        // Data Loader
        entityLoader = new BasePagingLoader<PagingLoadResult<M>>(dataProxy);

        // Data Store
        entityStore = new ListStore<M>(entityLoader);

        //
        // Grid Data Load Listener
        entityLoader.addLoadListener(new EntityGridLoadListener<M>(this, entityStore));

        //
        // Bind Entity Paging Toolbar
        if (entityPagingToolbar != null) {
            entityPagingToolbar.bind(entityLoader);
        }

        //
        // Configure columns
        ColumnModel columnModel = new ColumnModel(getColumns());

        //
        // Set grid
        entityGrid = new KapuaGrid<M>(entityStore, columnModel);
        add(entityGrid);

        //
        // Bind the grid to CRUD toolbar
        entityCRUDToolbar.setEntityGrid(this);

        //
        // Grid selection mode
        GridSelectionModel<M> selectionModel = entityGrid.getSelectionModel();
        selectionModel.setSelectionMode(SelectionMode.SINGLE);
        selectionModel.addSelectionChangedListener(new SelectionChangedListener<M>() {

            @Override
            public void selectionChanged(SelectionChangedEvent<M> se) {
                selectionChangedEvent(se.getSelectedItem());
            }
        });

        //
        // Grid view options
        GridView gridView = entityGrid.getView();
        gridView.setEmptyText(MSGS.gridEmptyResult());

        //
        // Do first load
        refresh();
    }

    protected EntityCRUDToolbar<M> getToolbar() {
        return new EntityCRUDToolbar<M>(currentSession);
    }

    protected abstract RpcProxy<PagingLoadResult<M>> getDataProxy();

    protected PagingToolBar getPagingToolbar() {
        return new MyPagingToolBar(ENTITY_PAGE_SIZE);
    }

    protected abstract List<ColumnConfig> getColumns();

    public void refresh() {
        entityLoader.load();
        entityPagingToolbar.enable();
    }

    public void refresh(GwtQuery query) {
        // m_filterPredicates = predicates;
        setFilterQuery(query);
        entityLoader.load();
        entityPagingToolbar.enable();
    }

    public void setFilterPanel(EntityFilterPanel<M> filterPanel) {
        this.filterPanel = filterPanel;
        entityCRUDToolbar.setFilterPanel(filterPanel);
    }

    protected void selectionChangedEvent(M selectedItem) {
        if (parentEntityView != null) {
            parentEntityView.setSelectedEntity(selectedItem);
        }
    }

    public void setPagingToolbar(PagingToolBar entityPagingToolbar) {
        this.entityPagingToolbar = entityPagingToolbar;
    }

    public GridSelectionModel<M> getSelectionModel() {
        return entityGrid.getSelectionModel();
    }

    protected abstract GwtQuery getFilterQuery();

    protected abstract void setFilterQuery(GwtQuery filterQuery);

What is my mistake? 我怎么了

EDIT: This is my server method: 编辑:这是我的服务器方法:

int totalLength = 0;
        List<GwtGroup> gwtGroupList = new ArrayList<GwtGroup>();
        try {
            KapuaLocator locator = KapuaLocator.getInstance();
            GroupService groupService = locator.getService(GroupService.class);
            UserService userService = locator.getService(UserService.class);
            GroupQuery groupQuery = GwtKapuaAuthorizationModelConverter.convertGroupQuery(loadConfig,
                    gwtGroupQuery);
            GroupListResult groups = groupService.query(groupQuery);
            if (!groups.isEmpty()) {
                if (groups.getSize() >= loadConfig.getLimit()) {
                    totalLength = Long.valueOf(groupService.count(groupQuery)).intValue();

                } else {
                    totalLength = groups.getSize();
                }
                for (Group g : groups.getItems()) {
                    gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g));
                    for (GwtGroup gwtGroup : gwtGroupList) {
                        User user = userService.find(g.getScopeId(), g.getCreatedBy());
                        if (user != null) {
                            gwtGroup.setUserName(user.getDisplayName());
                        }
                }
            }
            }
        } catch (Exception e) {
            KapuaExceptionHandler.handle(e);
        }
        return new BasePagingLoadResult<GwtGroup>(gwtGroupList, loadConfig.getOffset(),
                totalLength);
    }

(Didn't I just answer this an earlier version of this? Please don't delete questions after you get an answer to them, or people won't answer your questions at all any more.) (我不只是回答这个问题的早期版本吗?请在得到答案后不要删除问题,否则人们将不再回答您的问题。)

If the server is given a request for the second page (offset of 10), but returns a PagingLoadResult for the first page anyway, that is what you will see. 如果服务器收到第二页的请求(偏移量为10),但是无论如何PagingLoadResult为第一页返回PagingLoadResult ,这就是您所看到的。 Make sure your server is actually sending back the second page - not only that, but it must send in the response object the offset that it actually used for the next page (in your example, 10), or else the paging toolbar will not know which page the user is actually on. 确保服务器实际上正在发送第二页-不仅如此,而且它必须在响应对象中发送其实际用于下一页的偏移量(在您的示例中为10),否则分页工具栏将不知道用户实际在哪个页面上。

Make sure the server is taking the request offset into account, and returning the parameters it used correctly to the client. 确保服务器考虑了请求偏移量,并将正确使用的参数返回给客户端。 If that appears to be correct, please add the server method to your question, and add logging on the client and server to verify what is being requested, vs what is being returned. 如果这似乎是正确的,请在您的问题中添加服务器方法,并在客户端和服务器上添加日志以验证请求的内容和返回的内容。


Skipping items in Java is pretty straightforward, but will not scale very well for huge lists. 跳过Java中的项目非常简单,但是对于庞大的列表而言,伸缩性不是很好。

In short, just skip the first offset items when looping. 简而言之,循环时只需跳过第一个offset项即可。

First though, a free code review - this is very inefficient code - you are rewriting every item in gwtGroupList every time you add something: 但是,首先要进行免费的代码审查-这是效率很低的代码-每次添加内容时,您都要重写gwtGroupList每个项目:

            for (Group g : groups.getItems()) {
                gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g));
                for (GwtGroup gwtGroup : gwtGroupList) {
                    User user = userService.find(g.getScopeId(), g.getCreatedBy());
                    if (user != null) {
                        gwtGroup.setUserName(user.getDisplayName());
                    }
            }

It could instead read: 它可以改为:

            for (Group g : groups.getItems()) {
                gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g));
            }
            for (GwtGroup gwtGroup : gwtGroupList) {
                User user = userService.find(g.getScopeId(), g.getCreatedBy());
                if (user != null) {
                    gwtGroup.setUserName(user.getDisplayName());
                }
            }

Alternatively, they could be just one loop. 另外,它们可能只是一个循环。

Now we modify it again, to handle offset and limit : 现在我们再次修改它,以处理offsetlimit

            int itemsLeftToSkip = offset;
            for (Group g : groups.getItems()) {
                if (itemsLeftToSkip > 0) {
                    itemsLeftToSkip--;
                    continue;//we skipped this item, and now the count is one less
                }
                if (gwtGroupList.size() >= limit) {
                    break;//we've got enough already, quit the loop
                }
                gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g));
            }
            for (GwtGroup gwtGroup : gwtGroupList) {
                User user = userService.find(g.getScopeId(), g.getCreatedBy());
                if (user != null) {
                    gwtGroup.setUserName(user.getDisplayName());
                }
            }

Notice how we use offset to avoid items until we get to the ones that are needed for the new page, and we use limit to only send that many time, at a maximum. 请注意,在到达新页面所需的项目之前,我们如何使用offset来避免项目,并且我们使用limit最多只发送那么多次。

Finally, unless your groupQuery already has a limit built in (in which case, you should put the offset there too...), the if (groups.getSize() >= loadConfig.getLimit()) { block of code is likely not necessary at all, since you've already loaded all items. 最后,除非您的groupQuery已有内置限制(在这种情况下,您也应该在其中放置偏移量...), if (groups.getSize() >= loadConfig.getLimit()) {代码块可能完全没有必要,因为您已经加载了所有项目。 If it is necessary because there is a limit, then your pages will not correctly load all the way to the end. 如果由于限制而有必要,则页面将无法正确加载到最后。 Either way, investigate this code, and possibly get it reviewed further, something looks very wrong there. 无论哪种方式,请研究此代码,并可能对其进行进一步检查,那里似乎有些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM