【问题标题】:Vaadin get the table contents into a MapVaadin 将表格内容放入 Map
【发布时间】:2013-05-23 19:28:26
【问题描述】:

我对 Vaadin 有点陌生,并且已经阅读了他们的书。我的问题是关于一个可编辑的表格以及如何在它被改变时从中获取值。

所以基本上我有一个简单的表,我用一个 HashMap 填充它。所以编辑按钮使字段可编辑,添加行按钮添加一行。实施很简单。但是我没有管理的是更新按钮,因为当我添加一行或编辑某些字段时,我想将所有更改的键和值 成对 再次放入我的 hashmap 并做某事。

我知道Property.ValueChangeListener 为您提供了所有更改的单元格,但我需要的是有关整行的信息,而不是单个单元格。比如val2改成val22,我想根据它来更新我的hashmap。因此我也需要得到key2。简单地说,我想在发生某些变化时得到整条线路。有什么想法吗?

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

【问题讨论】:

  • 您可以查看Map Container,这是我编写的一个插件,用作HashMap 和Vaadin 的Container 之间的适配器。它远非完美,但它至少应该让你开始。如有任何问题,请随时提出。
  • 谢谢,我去看看。

标签: java vaadin


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-10-19
    • 2012-04-30
    • 2014-10-22
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多