How to get multiple selected rows in a table or indexedcontainer?

6.8k views Asked by At

I have a Table whose DataSource is set to a IndexedContainer. I also have multiple selection enabled on my Table. The Question is, how do I get all the selected values.. as an array perhaps?

My IndexedContainer:

private void populateAnalyteTable () {

        Analyte[] analytes = Analyte.getAnalytes();

        for (Analyte analyte : analytes) {

            Object id = ic_analytes.addItem();
            ic_analytes.getContainerProperty(id, "ID").setValue(analyte.getId());
            ic_analytes.getContainerProperty(id, "Analyte Name").setValue(analyte.getAnalyteName());

        }

        // Bind indexed container to table
        tbl_analytes.setContainerDataSource(ic_analytes);

    }

What I'm eventually trying to get is an array of Analyte objects

4

There are 4 answers

3
Patton On BEST ANSWER

Why do you want to use IndexContainer? Why don't you use BeanItemCotainer? Please find the snippet of code below

table.setMultiSelect(true);
BeanItemContainer<Analyte> container = new BeanItemContainer<Analyte>(Analyte.class);
container.addAll(Arrays.asList(Analyte.getAnalytes()));
table.setContainerDatasource(container);
// Add some Properties of Analyte class that you want to be shown to user
table.setVisibleColumns(new Object[]{"ID","Analyte Name"});


//User selects Multiple Values, mind you this is an Unmodifiable Collection
Set<Analyte> selectedValues = (Set<Analyte>)table.getValue();

Please let me know in case it doesn't solve the issue

5
André Schild On

The vaadin objects supporting MultiSelect all return a set of the selected items.

https://www.vaadin.com/api/com/vaadin/ui/AbstractSelect.html#getValue%28%29

The drawback of this, if you need the selected items in "real" order (as displayed onscreen) you will then have to find them from the Set to the Container

0
riddy On

Just add your object as the Item-ID, like luuksen already propesed. Just change the initialisation of yout IndexedContainer to:

    for (Analyte analyte : analytes) {

        Object id = ic_analytes.addItem(analyte);
        ic_analytes.getContainerProperty(id, "ID").setValue(analyte.getId());
        ic_analytes.getContainerProperty(id, "Analyte Name").setValue(analyte.getAnalyteName());

    }
0
MarcelloGarini On

table.getValue() is what you are looking for. This method gives you an Object (if table is single select) or a Set<Object> (if multiselect) of the ID(s) of selected item(s). Runtime type depends on runtime id type, but if you do not need the value you can go around with Object . If you are looking for Analytes as an array you can do

@SuppressWarnings("unchecked")
Set<Object> selectedIds = (Set<Object>) tbl_analytes.getValue();
List<Analyte> listAnalytes = new ArrayList<Analyte>(); 
for (Object id : selectedIds) {
    listAnalytes.get(tbl_analytes.getItem(id));
}
listAnalytes.toArray();

Note that this approach works with every standard container you may use in Vaadin. Regards!

EDIT: actually what .getValue() returns depends on the used container. In most of the cases it's the ID.