本文介绍了为什么我的网格不能在分页工具栏(GWT 2.4)的第二页上移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在开发一个使用分页工具栏的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(); .bpend(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()); $ / code $ / pre 这是我使用MyPagingToolbar的类: p> public abstract class EntityGrid< M extends GwtEntityModel>扩展ContentPanel { private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class); private static final int ENTITY_PAGE_SIZE = 10; 保护GwtSession currentSession; private AbstractEntityView< M> parentEntityView; 私人EntityCRUDToolbar< M> entityCRUDToolbar; 保护KapuaGrid< M> entityGrid; protected BasePagingLoader< PagingLoadResult< M>> entityLoader; 受保护的ListStore< M> entityStore; protected PagingToolBar entityPagingToolbar; 受保护的EntityFilterPanel< M> filterPanel; $ b $ protected EntityGrid(AbstractEntityView< M> entityView,GwtSession currentSession){ super(new FitLayout()); // //设置其他属性 this.parentEntityView = entityView; this.currentSession = currentSession; // //容器边界 setBorders(false); setBodyBorder(true); setHeaderVisible(false); // // CRUD工具栏 entityCRUDToolbar = getToolbar(); if(entityCRUDToolbar!= null){ setTopComponent(entityCRUDToolbar); } // //分页工具栏 entityPagingToolbar = getPagingToolbar(); if(entityPagingToolbar!= null){ setBottomComponent(entityPagingToolbar); $ b @Override protected void onRender(Element target,int index){ super.onRender(target,index); // //配置实体网格 //数据代理 RpcProxy< PagingLoadResult< M>> dataProxy = getDataProxy(); // Data Loader entityLoader = new BasePagingLoader< PagingLoadResult< M>>(dataProxy); // Data Store entityStore = new ListStore< M>(entityLoader); // //网格数据加载监听器 entityLoader.addLoadListener(new EntityGridLoadListener< M>(this,entityStore)); $ b $ // //绑定实体分页工具栏 if(entityPagingToolbar!= null){ entityPagingToolbar.bind(entityLoader); } // //配置列 ColumnModel columnModel = new ColumnModel(getColumns()); // //设置网格 entityGrid = new KapuaGrid< M>(entityStore,columnModel); add(entityGrid); // //将网格绑定到CRUD工具栏 entityCRUDToolbar.setEntityGrid(this); // //网格选择模式 GridSelectionModel< M> selectionModel = entityGrid.getSelectionModel(); selectionModel.setSelectionMode(SelectionMode.SINGLE); selectionModel.addSelectionChangedListener(new SelectionChangedListener< M>(){ @Override public void selectionChanged(SelectionChangedEvent< M> se){ selectionChangedEvent(se.getSelectedItem ()); } }); // //网格视图选项 GridView gridView = entityGrid.getView(); gridView.setEmptyText(MSGS.gridEmptyResult()); // //先加载 refresh(); } 保护EntityCRUDToolbar< M> getToolbar(){返回新的EntityCRUDToolbar< M>(currentSession); } protected abstract RpcProxy< PagingLoadResult< M>> getDataProxy(); protected PagingToolBar getPagingToolbar(){ return new MyPagingToolBar(ENTITY_PAGE_SIZE); } 受保护的摘要列表< 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 GwtQuery getFilterQuery(); protected abstract setFilterQuery(GwtQuery filterQuery); 我的错误是什么? 编辑:这是我的服务器方法: int totalLength = 0; 列表< GwtGroup> gwtGroupList = new ArrayList< GwtGroup>(); 尝试{ 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(); (Group g:groups.getItems()){ gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g))的} ; (gwtGroup gwtGroup:gwtGroupList){ User $ userService.find(g.getScopeId(),g.getCreatedBy()); if(user!= null){ gwtGroup.setUserName(user.getDisplayName()); $ b catch(Exception e){ KapuaExceptionHandler.handle(e); } 返回新的BasePagingLoadResult< GwtGroup>(gwtGroupList,loadConfig.getOffset(), totalLength); 解决方案回答此问题的早期版本?请在回答问题后不要删除问题,否则人们将不再回答您的问题。) 如果服务器接收到第二页(偏移量为10)的请求,但是无论如何都会为第一页返回一个 PagingLoadResult ,这是您将看到的内容。确保你的服务器实际上是发回第二页 - 不仅如此,它还必须在响应对象中发送实际用于下一页(在你的例子中为10)的偏移量,否则分页工具栏将不知道 确保服务器考虑了请求偏移量,并将正确使用的参数返回给客户端。如果这看起来是正确的,请将服务器方法添加到您的问题中,并在客户端和服务器上添加日志记录以验证所请求的内容与正在返回的内容。 在Java中跳过项目非常简单,但对于巨大的列表不能很好地扩展。 简而言之,在循环时只需跳过第一个偏移项。 首先,免费代码审查 - 这是非常低效的代码 - 您每次添加内容时都会重写 gwtGroupList 中的每个项目: $ (Group g:groups.getItems()){ gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g)); b $ b (gwtGroup gwtGroup:gwtGroupList){ User $ userService.find(g.getScopeId(),g.getCreatedBy()); if(user!= null){ gwtGroup.setUserName(user.getDisplayName()); $ / code $ / pre 可以改为:$ b (Group g:groups.getItems()){ gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g)); $ b (gwtGroup gwtGroup:gwtGroupList){ User user = userService.find(g.getScopeId(),g.getCreatedBy()); if(user!= null){ gwtGroup.setUserName(user.getDisplayName()); } } 或者,它们可能只是一个循环。现在我们再次修改它,以处理 offset 和 limit : int itemsLeftToSkip = offset; for(Group g:groups.getItems()){ if(itemsLeftToSkip> 0){ itemsLeftToSkip--; continue; //如果(gwtGroupList.size()> = limit){ break; //我们跳过这个项目,现在计数减少一个} 我们已经足够了,退出循环} gwtGroupList.add(KapuaGwtAuthorizationModelConverter.convertGroup(g)); (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()){代码块可能根本不需要,因为你已经加载了所有的项目。如果因为存在限制而有必要,那么您的页面将无法正确加载到最后。无论哪种方式,调查此代码,并可能进一步审查,在那里看起来非常错误。 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. 这篇关于为什么我的网格不能在分页工具栏(GWT 2.4)的第二页上移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 03:35