Vaadin - 添加项目可选(和可编辑)

时间:2017-01-16 13:53:28

标签: java vaadin

我想通过单击添加按钮添加新添加的表项(也称为行)。我的主要目的是使它既可选择又可编辑(但只有那一行,现在是整个表格)。

这是表格代码:

            public Table createTable(BeanContainer<Long, PS_SECTION> beans) {       
            table.setImmediate(true);
            table.setContainerDataSource(beans);
            table.setColumnHeader("PS_SECTION", "ID");
            table.setColumnHeader("NAME", "SECTION");
            table.setColumnHeader("VORDER","ORDER");

            table.addGeneratedColumn("ACTIVE", new Table.ColumnGenerator() {

                @Override
                public Object generateCell(final Table source, final Object itemId,
                        Object columnId) {              

            table.setTableFieldFactory(new DefaultFieldFactory() {

                @Override
                public Field<?> createField(Container container, final Object itemId, Object propertyId, Component uiContext) {

                        if (!itemId.equals(table.getValue())) {
                               return null;
                        }
                               Field<?> field = super.createField(container, itemId, propertyId, uiContext);
                               field.setBuffered(true);

                    return field;
                }
            });


            table.addValueChangeListener(new Property.ValueChangeListener() {           
                @Override
                public void valueChange(Property.ValueChangeEvent event) {              
                        if (table.getValue() != null) {                     
                            editButton.setVisible(true);
                        } 
                        else {
                            editButton.setVisible(false);
                        }           
                }
            });

            table.addItemClickListener(new ItemClickEvent.ItemClickListener() {
                  @Override
                  public void itemClick(ItemClickEvent itemClickEvent) {
                        if (itemClickEvent.isDoubleClick() && !table.isEditable()) {
                          table.setValue(itemClickEvent.getItemId());
                          configureEditable(true);  
                        // THE FUNCTION THAT MAKES THE SELECTED ROW EDITABLE
                          editButton.setVisible(false);
                          addButton.setVisible(false);
                        }               
                  }
                });


      // ....SOME MORE TABLE ATTRIBUTES HERE....

            return table;
        }

上面的代码适用于所选行,但是如何直接选择和编辑新添加的行?

addButton.addClickListener(new ClickListener() {            
        @Override
            public void buttonClick(ClickEvent event) {                                     
                addList();  //HERE THE ROW (AND THE BEAN) IS ADDED                  

            /**
             * What to put here?
             *
             */
                configureEditable(true);

            }
    });

1 个答案:

答案 0 :(得分:1)

Object itemId = createNewBean(); //创建一个新bean,返回一个可以设置为新项ID的唯一ID。

table.addItem(itemId); //add this newly created item to the table
//set the column values here for this particular item
table.getItem(itemId).getProperty("column_name").setValue("column_value");
//similarly set values of other columns for this particular item

table.setValue(itemId); //this selects the just added row

我希望它有所帮助。