我对Vaadin有点陌生,并且已经读过他们的书。我的问题是关于一个可编辑的表,以及如何在更改表时获取其值。

所以基本上我有一个简单的表,并用一个HashMap填充它。因此,编辑按钮使字段可编辑,添加行按钮添加行。实现很简单。但是我没有管理的是更新按钮,因为当我添加一行或编辑某个字段时,我想将所有更改的键和值以成对的形式再次存储到我的哈希图中,然后进行处理。

我知道Property.ValueChangeListener为您提供了所有已更改的单元格,但是我需要的是有关整行的信息-而不是单个单元格。例如,当val2更改为val22时,我想根据它更新哈希映射。因此,我还需要获取key2。简而言之,当某件事发生变化时,我想获得整条记录。有什么想法吗?

我的UI如下所示,我正在使用Vaadin 6:

最佳答案

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

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


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

关于java - Vaadin将表格内容放入 map ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16722298/

10-13 04:20