0

我正在开发一个使用分页工具栏的 GWT 应用程序。当网格中有超过 10 个组时,用户可以使用分页工具栏转到第二页。但是当我按下按钮转到第二页时,它会转到第二页,显示加载但随后工具栏将返回到第一页。10 项。这是第一页: 在此处输入图像描述

当我按下第二页的按钮时,我得到了这个加载:

在此处输入图像描述

但是在那个工具栏之后,我回到了第一页。这是我的分页工具栏类:

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());
    }
}

这是我使用 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);

我的错误是什么?

编辑:这是我的服务器方法:

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);
    }
4

1 回答 1

0

(我不是在较早的版本中回答这个问题吗?请不要在得到答案后删除问题,否则人们将不再回答您的问题。)

如果服务器收到第二页的请求(偏移量为 10),但PagingLoadResult无论如何都为第一页返回 a,这就是您将看到的。确保您的服务器实际上正在发回第二页 - 不仅如此,它还必须在响应对象中发送它实际用于下一页的偏移量(在您的示例中为 10),否则分页工具栏将不知道用户实际在哪个页面上。

确保服务器将请求偏移量考虑在内,并将正确使用的参数返回给客户端。如果这看起来是正确的,请将服务器方法添加到您的问题中,并在客户端和服务器上添加日志记录以验证请求的内容与返回的内容。


在 Java 中跳过项目非常简单,但不能很好地扩展大型列表。

简而言之,offset循环时只需跳过第一项。

首先,免费的代码审查 - 这是非常低效的代码 -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());
                    }
            }

它可以改为:

            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());
                }
            }

或者,它们可能只是一个循环。

现在我们再次对其进行修改,以处理offsetand limit

            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());
                }
            }

请注意我们如何offset避免项目,直到我们到达新页面所需的项目,并且我们limit最多只发送那么多时间。

最后,除非您groupQuery已经内置了限制(在这种情况下,您也应该将偏移量放在那里......),if (groups.getSize() >= loadConfig.getLimit()) {否则代码块可能根本不需要,因为您已经加载了所有项目。如果有必要因为有限制,那么您的页面将不会一直正确加载到最后。无论哪种方式,调查此代码,并可能对其进行进一步审查,那里看起来很不对劲。

于 2017-10-31T12:11:51.097 回答