Search code examples
javavaadinvaadin6

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


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


Solution

  • 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