How do I get multiple selected rows in a table or indexed container?

I have a table for which DataSource is set to IndexedContainer. I also have several options included in my spreadsheet. 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);

    }

      

I end up trying to get an array of Analyte objects

+3


source to share


4 answers


Why would you want to use IndexContainer? Why don't you use a BeanItemCotainer? Please find the code snippet 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 if this does not solve the problem.

+4


source


Vaadin objects that support MultiSelect return a set of selected items.

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



The disadvantage of this is that if you want the selected items in a "real" order (as shown on the screen) you have to find them from Set to Container

+1


source


Just add your object as Item-ID as luuksen already requested. Just change the initialization 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


source


table.getValue()

is what you are looking for. This method gives you Object

(if the table is a single selection) or Set<Object>

(if using a multi-segment) the IDs (s) of the selected item (s). The runtime type depends on the type of the runtime identifier, but if you don't need the value, you can work around it 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();

      

Please note that this approach works with all the standard containers you can use in Vaadin. Hello!

EDIT: Actually what .getValue () returns depends on the container used. In most cases, this is an identifier.

0


source







All Articles