0

I am migrating my project from vaadin 7 to vaadin 8. As Table is removed. so I am replacing it with grid. previously I was fetching the row ids for multiple selection like this:

  Set<Object> itemIds = table.getValue();
  for(Object lItem : itemIds){
      Integer lId = Integer.parseInt(lItem.toString());
  }

But in vaadin 8 grid there is a itemclick listener that provide rowindex only if we click on any item and on click of any checkbox for selection it does not return anything, as checkboxes only works with selection listener. see code below:

  lGrd.addItemClickListener(new ItemClickListener<Employee>() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void itemClick(ItemClick<Employee> event) {

            if(lGrd.getSelectionModel().isSelected(event.getItem())){
                if(!lSelection.contains(event.getRowIndex())){
                    lSelection.add(event.getRowIndex());
                }
            }else if(lSelection.contains(event.getRowIndex())){
                lSelection.remove(event.getRowIndex());
            }

            Notification.show(lSelection.toString() + " Selected Employees Row Id");
        }
    });

Also using selection listener it does not return any row index like as in itemclicklistener

  lGrd.addSelectionListener(new SelectionListener<Employee>() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void selectionChange(SelectionEvent<Employee> event) {
            Set<Employee> lSet = event.getAllSelectedItems();
            for(Employee emp : lSet){
                //how to fetch row id here
            }
        });

It provides selected items but no row index. How to fetch Employee Row index here. Also if I want any column data. How to fetch it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
tushar sharma
  • 105
  • 2
  • 10

1 Answers1

2

Since Framework version 8.4.0 (release notes) the Grid.ItemClick event (Grid.ItemClick doc) also contains the row index information of the clicked item.

before that we had a ugly workaround :(

we hat

private List items; private Grid grid; as a Class Fields.

in the initialisation method we filled the List with items and had

grid.setITems(items);

onclick we allways had the item from click Event earlier something ugly like (Item) event.getSource().getValue() i think with modern Vaadin the Event is parametrised, so we simply get event.getValue()

then we used items.indexOF(event .. get Value ..);

so i am VERY thankfull, that since Vaadin 8.4 we can refactor this :) ! ! !

  • hi thank you for your time, actually i want row index at row selection. How can i do that? – tushar sharma May 12 '18 at 09:25
  • Grid client side knows the row indices of the rows which it is currently showing. Thus it was no brainer to implement this in ItemClickEvent. The selection is totally different case, since it can be also programmatic, and then you have the case that you select a row that is not shown. Then we do not know the index. The implementation covering all the use cases would require Grid to fetch the whole data and calculate the index. That is why we leave this for the developer, so that it is well understood the difference of O(1) vs. O(N) operation. – Tatu Lund May 12 '18 at 12:55
  • @Vilius Kukanauskas Thanks for your feedback. – Tatu Lund May 12 '18 at 12:57