简体   繁体   English

Vaadin-动态生成表

[英]Vaadin - generate table dynamically

I have a Vaadin application. 我有一个Vaadin应用程序。 One of it's components is a table. 它的组成部分之一是一张桌子。 I need a possibility to add as many columns as the user wants (well, let's say max. 20 at the moment) to that table. 我需要一种可能,可以向该表添加用户想要的任意多的列(目前最多可以添加20个列)。 At the beginning, there are 5 colums, so in fact the user can add column 6 - 20. However, after doing "layout.addComponent(...)", this table is not editable any more. 开始时有5个列,因此实际上用户可以添加第6列至第20列。但是,在执行“ layout.addComponent(...)”之后,该表将不再可编辑。

I tried 2 things: 我尝试了两件事:

  1. There is a variable which stores the current number of rows and is increased by 1 for every click on an "add" button. 有一个变量存储当前的行数,并在每次单击“添加”按钮时将其增加1。 With every click, 1 column is added to the table. 每次单击都会在表中添加1列。

  2. Another idea was to hold the columns numbers in a variable (like idea 1) and the value of the cells somewhere in a Collection (whatever). 另一个想法是将列号保存在变量中(如想法1),将单元格的值保留在Collection中的某个位置(无论如何)。 After click on the "add" button, the whole table is removed, then all columns are added (all that have been there + a new one) and it is added to the layout. 单击“添加”按钮后,将删除整个表,然后添加所有列(所有已存在的列+一个新列)并将其添加到布局中。

However, none of these ideas works. 但是,这些想法都不起作用。

Any suggestions? 有什么建议么?

I think it should be enough to invoke addContainerProperty on the Container that is linked to your Table . 我认为在链接到TableContainer上调用addContainerProperty应该足够了。 Doing that should fire a PropertySetChangeEvent to the Table. 这样做应将PropertySetChangeEvent触发到表。

Unless, of course, if you have limited the visible columns using Table.setVisibleColumns . 当然,除非您使用Table.setVisibleColumns限制了可见列。

If this doesn't help you, a piece of code could help solving the problem. 如果这对您没有帮助,那么一段代码可以帮助解决问题。

If MyContainer extends IndexedContainer, you have full freedom; 如果MyContainer扩展IndexedContainer,则您具有完全的自由; just as a sample, I have Statistics which is simply (dynamic) Map serialized in ZooKeeper, and MyContainer listens to ZooKeeper changes: 就像一个示例一样,我具有在ZooKeeper中序列化的,简单的(动态)Map统计信息,并且MyContainer侦听ZooKeeper的更改:

public class MyContainer extends IndexedContainer {
    ...
    public void process(final ModelChangeEvent event) {
        ...
        for (Map.Entry<String, Long> entry : newTask.getCounters().entrySet()) {
            addContainerProperty(entry.getKey(), Long.class, 0L);
            Property property = getContainerProperty(taskName, entry.getKey());
            property.setValue(entry.getValue());
        }
        fireContainerPropertySetChange();
        ...
    }

When we add new (key, value) pair to Map, we will get new column automatically, where "key" is column title, and "value" is cell value. 当我们向Map添加新的(键,值)对时,我们将自动获得新列,其中“键”是列标题,“值”是单元格值。 No needs to call "fire"; 无需称“火”; it will be called automatically by IndexedContainer implementation (check sources). IndexedContainer实现会自动调用它(检查源)。

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

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