3

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 Answers4

4

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

Patton
  • 2,002
  • 3
  • 25
  • 36
  • table.getValue() return the ID used within the table, not the real items. From Vaadin getValue() Javadoc: "Gets the selected item id or in multiselect mode a set of selected ids." Apart that, the suggestion of using a BeanItemContainer is good, even if it depends on OP needs. – MarcelloGarini Nov 21 '14 at 07:42
  • No it actually returns the Bean that was selected in your case Ananlyte. Please do a POC, if you want, I will share mine :) The itemID is nothing but the Bean in case of BeanItemContainer. – Patton Nov 21 '14 at 14:34
  • Well, I was pretty shure but I checked it out and you are right. Now totally deserved an up-vote! – MarcelloGarini Nov 21 '14 at 16:17
1

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

André Schild
  • 4,592
  • 5
  • 28
  • 42
  • Danke Andre, although I'm still struggling to get the multiple selections. Is it possible to provide a code example? What I'm trying to do is get all the id values into an Integer array. By 'id' I mean the selected values of 'getContainerProperty(id, "ID")'. –  Nov 17 '14 at 11:51
  • @Relborg in your question you wanted an array of selected Analyte objects. getValue() on a multiselect table will do exactly this, only it gives you a Set instead of an Array. You don't have to work around with property id's or something, just work with the objects themselves. – luuksen Nov 17 '14 at 15:09
  • @luuksen apparently it doesn't give a Set of Analyte objects but 'a set of selected ids.' Once I have a set of selected ids, I don't know how to iterate over it and get my Analyte objects :( –  Nov 18 '14 at 04:36
  • @Relborg how do you know that you get id's? Do not get confused by the javadoc, itemId is how Vaadin calls the objects. Simply cast the contained Objects of the set to Analyte and you should have it. – luuksen Nov 18 '14 at 11:45
  • @luuksen the reason is that he says "addItem()" which generates a default id – riddy Nov 20 '14 at 12:51
0

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());

    }
riddy
  • 501
  • 1
  • 4
  • 17
0

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.

MarcelloGarini
  • 599
  • 2
  • 13