0

I am kinda new to Vaadin, and already went through their book. My question is about an editable table and how to get the values out of it when it is changed.

So basically what I have is a simple table and I fill this with a HashMap. So edit buttons make the fields editable, and add line button adds a line. The implementation is straightforward. But what I did not manage is the update button, because when I add a line or edit some field I want to get all the changed keys and values as pairs into my hashmap again and do sth with that.

I know that Property.ValueChangeListener gives you all the cells that changed, but what I need is information about the whole row - not a single cell. For example when val2 is changed to val22, I want to update my hashmap according to it. Therefore I need to get key2 as well. Simply, I want to get the whole line when something is changed on that. Any ideas on that?

And my UI looks as following and I am using Vaadin 6: enter image description here

4

1 回答 1

3

对于那些可能需要这种东西的人,我找到了一种解决方法:

// I needed to fill the table with an IndexedContainer
final IndexedContainer container = new IndexedContainer();
// table headers
container.addContainerProperty("Metadata Key", String.class, null);
container.addContainerProperty("Metadata Value", String.class, null);

// Now fill the container with my hashmap (objectMetadatas) and at the end we will add the container to the table
int i = 0;
for (String k : objectMetadatas.keySet()) {
    Integer rowId = new Integer(i);
    container.addItem(rowId);
    container.getContainerProperty(rowId, "Metadata Key").setValue(k);
    container.getContainerProperty(rowId, "Metadata Value").setValue(objectMetadatas.get(k));
    i++;
}

// then added a ValueChangeListener to the container
container.addListener(new Property.ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
         Property p = event.getProperty(); // not necessary
             System.out.println(p);        // not necessary
    }
});

// add the the button to update the table and get the changed values into your hashmap
buttonUpdate.addListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        Map<String, String> map = new HashMap<String,String>();
        Collection i = tableMetadata.getContainerDataSource().getItemIds();
        for (int x = 0; x < i.size(); x++) {
            // Items in Vaadin represent rows, since I have two columns
            // i have only two values in my row as following: "row1 row2"
            // if you have four columns on your table 
            // your item will look like this: "row1 row2 row3 row4"
            Item it=myTable.getContainerDataSource().getItem(x);
            // thats why I am splitting it
            String[] row= it.toString().split(" ");
            map.put(row[0], row[1]);
        }
        // Now all changed values are in your map! 
        // Do sth with that map
    }
});

// Finally do not forget to add that container to your table
tableMetadata.setContainerDataSource(container);

就这样!希望它对某人有所帮助!如果您找到更好的方法,请发布。

于 2013-05-27T12:26:37.453 回答