Enabling pagination to datagrid

HI ,

How to enable pagingation to datagrid with valueCollectionDatasource and pass the pager params to custom service via cuba studio .

Thank you .


Suppose you have a service loading the list of user logins and names and you want to show it in DataGrid. Of course you could do it with regular User entity and an appropriate view, but we will use KeyValueEntity and ValueCollectionDatasource for the sake of example.

The service interface:

package com.company.sample.service;

import com.haulmont.cuba.core.entity.KeyValueEntity;

import java.util.List;

public interface UserLoadingService {
    String NAME = "sample_UserLoadingService";

    List<KeyValueEntity> loadUsers(int firstResult, int maxResults);

    long getUsersCount();

And implementation:

package com.company.sample.service;

import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.Transaction;
import com.haulmont.cuba.core.TypedQuery;
import com.haulmont.cuba.core.entity.KeyValueEntity;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;

public class UserLoadingServiceBean implements UserLoadingService {

    private Persistence persistence;

    public List<KeyValueEntity> loadUsers(int firstResult, int maxResults) {
        try (Transaction tx = persistence.createTransaction()) {
            TypedQuery<Object[]> query = persistence.getEntityManager().createQuery(
                    "select u.login, u.name from sec$User u order by u.login", Object[].class);
            return query.getResultList().stream()
                    .map(fields -> {
                        KeyValueEntity entity = new KeyValueEntity();
                        entity.setValue("login", fields[0]);
                        entity.setValue("name", fields[1]);
                        return entity;

    public long getUsersCount() {
        return persistence.callInTransaction(em ->
                (long) em.createQuery("select count(u) from sec$User u").getSingleResult());


In the screen, we define a datasource of a custom class and set maxResults attribute for paging. The dataGrid component has rowsCount element which shows the paging controls:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        <valueCollectionDatasource id="usersDs"
                <property datatype="string"
                <property datatype="string"
        <dataGrid datasource="usersDs"
                <column property="login"/>
                <column property="name"/>

The datasource just delegates to our service. Overriding of the getCount() method is required for correct work of paging controls:

package com.company.sample.web.screens;

import com.company.sample.service.UserLoadingService;
import com.haulmont.cuba.core.entity.KeyValueEntity;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.gui.data.impl.CustomValueCollectionDatasource;

import java.util.Collection;
import java.util.Map;

public class UserValuesDatasource extends CustomValueCollectionDatasource {

    private final UserLoadingService service = AppBeans.get(UserLoadingService.class);

    protected Collection<KeyValueEntity> getEntities(Map<String, Object> params) {
        return service.loadUsers(firstResult, maxResults);

    public int getCount() {
        return (int) service.getUsersCount();

The whole project is attached:
sample.zip (89.9 KB)

Please note that the custom service is not strictly required here, we could use DataManager’s loadValues() and getCount() methods.