简体   繁体   中英

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. When I have more than 10 groups in grid, user can go to second page with paging toolbar. 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. 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:

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. 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.

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.

In short, just skip the first offset items when looping.

First though, a free code review - this is very inefficient code - you are rewriting every item in gwtGroupList every time you add something:

            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 :

            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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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