Vaadin将表格内容放入地图中

时间:2013-05-23 19:28:27

标签: java vaadin

我是Vaadin的新手,已经完成了他们的书。我的问题是关于一个可编辑的表以及如何在更改时从中获取值。

所以基本上我拥有的是一个简单的表,我用HashMap填充它。因此,编辑按钮可使字段可编辑,添加行按钮可添加一行。实施很简单。但是我没有管理的是更新按钮,因为当我添加一行或编辑某些字段时,我希望将所有已更改的键和值作为对再次添加到我的hashmap中并执行该操作。

我知道Property.ValueChangeListener会给你所有改变的细胞,但我需要的是关于整行的信息 - 而不是单个细胞。例如,当val2更改为val22时,我想根据它更新我的hashmap。因此我也需要key2。简单地说,我希望在改变某些内容时获得整条线。有什么想法?

我的UI看起来如下,我正在使用Vaadin 6: enter image description here

1 个答案:

答案 0 :(得分: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);

这就是全部!希望它对某人有所帮助!如果你找到更好的方法,请发表它。

相关问题