2017-07-31 107 views
1

我在我的网格中使用了IndexedContainer。更改列名 - Vaadin 7.8.4

Grid grid = new Grid(); 
IndexedContainer container = new IndexedContainer(); 
grid.setContainerDataSource(container); 
container.addContainerProperty("Something", String.class, ""); 

我需要更改容器属性的名称。在点击按钮之后,将“某物”属性改为“新物业”。有任何想法吗 ?非常感谢你 !

回答

3

注1:你从哪里得到vaadin 7.8.4?最新的7.x release我能看到的是7.7.10。在这个练习中,我假定这是一个错字和使用7.7.4 ...


注2:不知道你是否要更改只是列标题,或整个房地产ID ...如果它仅仅是标题,您可以使用:

grid.getColumn("Something").setHeaderCaption("Something else"); 

AFAIK这是不可能变化属性。但是,您可以解决此通过删除它,并加入一个新问题:

public class MyGridWithChangeableColumnHeader extends VerticalLayout { 
    public MyGridWithChangeableColumnHeader() { 
     // basic grid setup 
     Grid grid = new Grid(); 
     IndexedContainer container = new IndexedContainer(); 
     grid.setContainerDataSource(container); 
     container.addContainerProperty("P1", String.class, ""); 
     container.addContainerProperty("Something", String.class, ""); 
     container.addContainerProperty("P3", String.class, ""); 

     // button to toggle properties 
     Button button = new Button("Toggle properties", event -> { 
      String oldProperty, newProperty; 
      if (container.getContainerPropertyIds().contains("Something")) { 
       oldProperty = "Something"; 
       newProperty = "Something else"; 
      } else { 
       oldProperty = "Something else"; 
       newProperty = "Something"; 
      } 

      container.removeContainerProperty(oldProperty); 
      container.addContainerProperty(newProperty, String.class, ""); 
      grid.setColumnOrder("P1", newProperty, "P3"); 
     }); 

     addComponents(grid, button); 
    } 
} 

结果:

container-toggle-property

+0

请记住,如果你删除一个属性,你将失去存储的所有数据它,你将不得不再次填充它。 – Shirkam