简体   繁体   English

Vaadin将表格内容放入地图

[英]Vaadin get the table contents into a Map

I am kinda new to Vaadin, and already went through their book. 我对Vaadin有点陌生,并且已经读过他们的书。 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. 所以基本上我有一个简单的表,并用一个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. 我知道Property.ValueChangeListener为您提供了所有已更改的单元格,但是我需要的是有关整行的信息-而不是单个单元格。 For example when val2 is changed to val22 , I want to update my hashmap according to it. 例如,当val2更改为val22 ,我想根据它更新哈希表。 Therefore I need to get key2 as well. 因此,我还需要获取key2 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: 而且我的UI如下所示,我正在使用Vaadin 6: 在此处输入图片说明

For those who might need such a thing I have found a workaround: 对于那些可能需要这种东西的人,我找到了一种解决方法:

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

That's all! 就这样! Hope it will help to someone! 希望对别人有帮助! And please if you find a better way please post it. 并且,如果您找到更好的方法,请发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM