Auto refresh of Serial Numbers in Data Source Table

Hi,

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.

Thanks,
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

Thanks,
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;

    @Transient
    @MetaProperty
    protected Integer lineNum;
...

Then add a column to the screen table:

<groupTable id="customersTable"
            width="100%">
...
    <columns>
        <column id="lineNum"/>
...

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

package com.company.sample.web.customer;

import com.company.sample.entity.Customer;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.GroupDatasource;
import com.haulmont.cuba.gui.data.impl.DatasourceImplementation;

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

public class CustomerBrowse extends AbstractLookup {

    @Inject
    private GroupDatasource<Customer, UUID> customersDs;

    @Override
    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()) {
                customer.setLineNum(++counter);
            }
            // 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: https://github.com/cuba-labs/large-dataset