Pagination component can't display correct page tabs

Hi Team:
I refer to the configuration of this document

But my pages always 1 pages. Are there any settings I am missing?

My screen.xml settings:

<data readOnly="true">
        <collection id="taskDc" class="com.company.model.TaskData"/>
</data>

<pagination id="pagination" itemsPerPageOptions="10,20,30" itemsPerPageVisible="true">
        <containerProvider dataContainer="taskDataDc"/>
</pagination>

UiController:
The taskDc has set items in onInit event.

@Install(to = "pagination", subject = "totalCountDelegate")
private Integer paginationTotalCountDelegate() {
        return (int) result.getTotal();    //  ===> There are 41 results
}

Regards.

Hello!

Pagination divides data into pages using loader because it provides setting first and maximum results. Pagination cannot detect “from” and “to” positions if container does not use loader and therefore shows data in one page.
I think in your case, the correct way is to specify loader with delegation. In this delegation, you should consider the first and maximum results while loading data:

<data readOnly="true">
    <collection id="customersDc"
                class="com.company.trpagination.entity.Customer">
        <loader id="customersDl"/>
    </collection>
</data>

...

<pagination id="pagination"
            itemsPerPageOptions="10,20,30"
            itemsPerPageVisible="true">
    <containerProvider dataContainer="customersDc"/>
</pagination>
@Install(to = "customersDl", target = Target.DATA_LOADER)
private List<Customer> customersDlLoadDelegate(LoadContext<Customer> loadContext) {
    int firstResult = loadContext.getQuery().getFirstResult();
    int maxResult = loadContext.getQuery().getMaxResults();
    
    return dataManager.load(Customer.class)
            .query("select e from tr_Customer e")
            .firstResult(firstResult)
            .maxResults(maxResult)
            .list();

    // or using loadContext
    // return dataManager.loadList(loadContext);
}

@Install(to = "pagination", subject = "totalCountDelegate")
private Integer paginationTotalCountDelegate() {
    return Math.toIntExact(dataManager.getCount(new LoadContext<>(customersDc.getEntityMetaClass())));
}
1 Like

Thanks. This is exactly what i want.

Hi everyone,

I am experiencing an issue when using filters.

    @Install(to = "customersDl", target = Target.DATA_LOADER)
    private List<PartyHierarchyGroup> customersDlLoadDelegate(LoadContext<Customer> loadContext) {
        Object searchString = Objects.requireNonNull(loadContext.getQuery()).getParameters().get("component_searchStringField");
        if(searchString != null) {
            loadContext.getQuery()
                    .setParameter("component_searchStringField", createSearchQuery(searchString));
        }
        return dataManager.loadList(loadContext);
    }

I am also using the pagination load delegate:

    @Install(to = "pagination", subject = "totalCountDelegate")
    private Integer paginationTotalCountDelegate() {
        return dataManager.loadValue("select count(e) from db_Customer e", Integer.class)
                .one();
    }

The issue is when filtering the customers by name for example, the pagination settings doesn’t update. I still have 10 pages displayed, despite the number of filtered customers is just 5.

Any idea how to solve this?

Thanks

Hello!

You should consider your query condition also in total count query. After you apply a condition, totalCountDelegate still counts the whole customer table.

You can change the query or use LoadContext with query parameters from loader:

@Install(to = "pagination", subject = "totalCountDelegate")
private Integer paginationTotalCountDelegate() {
    return Math.toIntExact(dataManager.getCount(customersDl.createLoadContext()));
}

That works, however when clearing the filters the pagination is not updated. With filtering value, the number of pages is reduced. But clearing the filters doesn’t update the number of pages back.

Probably, the query still contains condition with filter parameter. Do you use Filter component or use some field to filter items? How do you define condition in the query?

Could you share screen XML descriptor and controller code?

No I am not using the Filter. I am following the example: https://demo.jmix.io/sampler/#main/2/sample?id=custom-filter

I just added pagination component to it.

Unfortunately, I cannot reproduce the issue with example from above link.

I guess the parameter still presents in the query, but cannot I cannot check it. Could you please share some small demo project where the problem is reproduced?

Also, you can set a break point to paginationTotalCountDelegate() and check DataContext/Query whether they contain a parameter with value.