大家好,我的网格和分页工具栏遇到问题。在每一页上,我应该显示 9 个项目。例如,当我有 15 个项目时,当加载程序尝试加载第二页时,首先他给了我第二页,但之后他又让我回到第一页。如果我有 20 个或更多项目,第一页和第二页还可以,但第三页有我在最后一句中写的问题(让我回到第一页)。如果我有 4 页,前 3 页还可以,但最后是问题 所以,每次 LAST PAGE 都是问题。这是我的网格和分页工具栏类:
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 MyGrid<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));
entityLoader.setRemoteSort(true);
//
// Bind Entity Paging Toolbar
if (entityPagingToolbar != null) {
entityPagingToolbar.bind(entityLoader);
}
//
// Configure columns
ColumnModel columnModel = new ColumnModel(getColumns());
//
// Set grid
entityGrid = new MyGrid<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();
}
这是 MyGrid 类:
public class MyGrid<M extends KapuaBaseModel> extends Grid<M> {
public MyGridGrid(ListStore<M> store, ColumnModel cm) {
super(store, cm);
//
// Grid properties
setBorders(false);
setStateful(false);
setLoadMask(true);
setStripeRows(true);
setTrackMouseOver(false);
disableTextSelection(false);
//
// Grid view options
GridView gridView = getView();
gridView.setAutoFill(true);
gridView.setForceFit(true);
gridView.setSortingEnabled(false);
}
}
这是 MyPagingToolbar:
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());
}
}
我看到了一些类似的问题,但这些解决方案在我的情况下不起作用。所以请不要将此问题标记为重复。提前致谢。
这是我的服务器方法:
@Override
public PagingLoadResult<GwtTag> query(PagingLoadConfig loadConfig,
GwtTagQuery gwtTagQuery) throws GwtKapuaException {
int totalLength = 0;
List<GwtTag> gwtTagList = new ArrayList<GwtTag>();
try {
KapuaLocator locator = KapuaLocator.getInstance();
TagService tagService = locator.getService(TagService.class);
TagQuery tagQuery = GwtKapuaTagModelConverter.convertTagQuery(loadConfig, gwtTagQuery);
UserService userService = locator.getService(UserService.class);
TagListResult tags = tagService.query(tagQuery);
if (!tags.isEmpty()) {
if (tags.getSize() >= loadConfig.getLimit()) {
totalLength = Long.valueOf(tagService.count(tagQuery)).intValue();
} else {
totalLength = tags.getSize();
}
for (Tag g : tags.getItems()) {
gwtTagList.add(KapuaGwtTagModelConverter.convertTag(g));
for (GwtTag gwtTag : gwtTagList) {
User user = userService.find(g.getScopeId(), g.getCreatedBy());
if (user != null) {
gwtTag.setUserName(user.getDisplayName());
}
}
}
}
} catch (Exception e) {
KapuaExceptionHandler.handle(e);
}
return new BasePagingLoadResult<GwtTag>(gwtTagList, loadConfig.getOffset(),
totalLength);
}
这就是我称之为查询服务器方法的地方:
public class TagGrid extends EntityGrid<GwtTag> {
private static final GwtTagServiceAsync GWT_TAG_SERVICE = GWT.create(GwtTagService.class);
private static final ConsoleTagMessages MSGS = GWT.create(ConsoleTagMessages.class);
private GwtTagQuery query;
protected TagGrid(AbstractEntityView<GwtTag> entityView, GwtSession currentSession) {
super(entityView, currentSession);
query = new GwtTagQuery();
query.setScopeId(currentSession.getSelectedAccountId());
}
@Override
protected RpcProxy<PagingLoadResult<GwtTag>> getDataProxy() {
return new RpcProxy<PagingLoadResult<GwtTag>>() {
@Override
protected void load(Object loadConfig,
AsyncCallback<PagingLoadResult<GwtTag>> callback) {
GWT_TAG_SERVICE.query((PagingLoadConfig) loadConfig, query, callback);
}
};
}
编辑:这是 convertQuery 方法
public static TagQuery convertTagQuery(PagingLoadConfig loadConfig, GwtTagQuery gwtTagQuery) {
KapuaLocator locator = KapuaLocator.getInstance();
TagFactory tagFactory = locator.getFactory(TagFactory.class);
TagQuery tagQuery = tagFactory.newQuery(GwtKapuaCommonsModelConverter.convertKapuaId(gwtTagQuery.getScopeId()));
if (gwtTagQuery.getName() != null && !gwtTagQuery.getName().isEmpty()) {
tagQuery.setPredicate(new AttributePredicate<String>(TagPredicates.NAME, gwtTagQuery.getName(), Operator.LIKE));
}
tagQuery.setOffset(loadConfig.getOffset());
tagQuery.setLimit(loadConfig.getLimit());
return tagQuery;
}