Autoincrement column in Grid

Hi,

I’ve a grid with a column S.No ( Sequence No). I update this column by using

static AtomicInteger at = new AtomicInteger(0);

    public static int getNextCountValue() {
        return at.incrementAndGet();
    }

It works fine when there is no filtering. If I add a filter, this column value gets incremented again which I do not want to. Is there a way to prevent incrementing the value when filtering?

Hi Srinivasa

It’s unclear to me what this column is for. What you describe sounds a little like an [index column]
(https://stackoverflow.com/a/62061436/3441504). Or do you mean that when you filter and/or sort, the initially assigned S.No-value should stay with the row?

It also sounds like you could do this by having an actual sequence number value saved in the DB for each of your items, and add a simple property-column to your grid.

grid.addColumn(Foo::getSequenceNumber);

Thanks for looking and replying a solution. I;m reading messages from a queue so I’ve to create a sequence number on the fly. The solution provided here https://stackoverflow.com/questions/62061435/vaadin-flow-grid-with-row-index/62061436#62061436 works ok but when filteringthe grid sequence number is not retained. It switches again 1,2…

When you create the sequence number on the fly, you can either store this value in your item and have a @Transient getSequenceNumber method to use for grid.addColumn(Foo::getSequenceNumber);.

Or if you don’t want to modify your item class, you can build a Map<Foo, Integer> (or whatever type your seq.No is) and create your column like this:

Map<Foo, Integer> itemSeqNoMap = new HashMap<>();
// TODO: create sequence numbers for each item and store them in this map

grid.addColumn(foo -> itemSeqNoMap.get(foo).toString()); // TODO: add null check for map.get(foo)

This trick worked. Thanks a lot.