Auto refresh of Serial Numbers in Data Source Table


In my application i have added the serial numbers for the data in the database and auto incremented it. Now when I see it in my application it showing correctly as in order. But when I search them using filters the sno is populating as showing in the second image. i wanted the Sno from Serial num 1. Can you please suggest me how to refresh them. I have added the images in the below.

Dhanush Chikoti.12

Hi Dhanush,

Do you want row numbers always started from one regardless of filter and sorting?

Hey Konstantin,

Yes, Even when the user filter the table the row numbers should start from the beginning.
Can you please help me out. @knstvk

Dhanush Chikoti

Hi Dhanush,

First, add a non-persistent attribute to your entity, for example:

@Table(name = "SAMPLE_CUSTOMER")
@Entity(name = "sample$Customer")
public class Customer extends StandardEntity {
    private static final long serialVersionUID = -6269895397249464673L;

    protected Integer lineNum;

Then add a column to the screen table:

<groupTable id="customersTable"
        <column id="lineNum"/>

In the browser controller, create a listener that will fill in the attribute on each datasource refresh:


import com.haulmont.cuba.gui.components.AbstractLookup;

import javax.inject.Inject;
import java.util.Map;
import java.util.UUID;

public class CustomerBrowse extends AbstractLookup {

    private GroupDatasource<Customer, UUID> customersDs;

    public void init(Map<String, Object> params) {
        // add listener on datasource content change
        customersDs.addCollectionChangeListener(e -> {
            // find page start
            int counter = ((CollectionDatasource.SupportsPaging) customersDs).getFirstResult();
            // for each entity in the datasource, set its sequence number
            for (Customer customer : customersDs.getItems()) {
            // make the datasource unmodified to prevent from asking to save changes on screen close
            ((DatasourceImplementation) customersDs).setModified(false);

You can find the complete source code here: