Reputation: 105
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?
Upvotes: 0
Views: 660
Reputation: 112
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 :) ! ! !
Upvotes: 2