Hi everyone I am facing problems with my grid and paging toolbar. On every page I should show 9 items. When I have, for example 15 items, when loader is trying to load second page, first he gives me the second page but right after that he backs me to the first page. If I have 20 or more items, first and second pages are okay, but third is having problems like I wrote in last sentence(backs me to the first page). If I have 4 pages, first 3 are okay, but the last is problem So, every time LAST PAGE is a problem. This is my class with Grid and Paging Toolbar:
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();
}
This is MyGrid class:
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);
}
}
And this is 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());
}
}
I saw some similar questions but that solutions did not work in my case. So please, do not mark this question as duplicate. Thanks in advance.
This is my server method:
@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);
}
And this is where I call that query server method :
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);
}
};
}
EDIT: This is convertQuery method
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;
}