Vaadin网格表:如何禁用排序function并设置一列的颜色

我在Vaadin中使用Grid表进行数据表示。 为此,我试图弄清楚以下两个问题:

1.)如何在每列的标题中禁用排序function

2.)如何设置Grid表中一列的颜色

首先,我发现Vaadin文档是一个开始寻求帮助的好地方。 对于剩下的练习,假设我们有一个包含3个简单列c1,c2和c3的Grid

 Grid grid = new Grid(); grid.addColumn("c1", String.class); grid.addColumn("c2", String.class); grid.addColumn("c3", String.class); 

1.)如何在每列的标题中禁用排序function

遍历每一列并将其sortable属性设置为false

 for (Grid.Column column : grid.getColumns()) { column.setSortable(false); } 

或者只需获取所需的一列并设置其可排序属性:

 grid.getColumn("c1").setSortable(false); 

2.)如何设置网格表中一列的颜色

与表格的完成方式类似,您需要在主题中定义CSS样式:

 @import "../valo/valo.scss"; @mixin mytheme { @include valo; // Insert your own theme rules here .v-grid-cell.green { background: #33BB00; } } 

并使用CellStyleGenerator将样式应用于所需的列:

 grid.setCellStyleGenerator(new Grid.CellStyleGenerator() { @Override public String getStyle(Grid.CellReference cellReference) { if ("c1".equals(cellReference.getPropertyId())) { return "green"; } else { return null; } } }); 

哪个应该生成以下内容:

在此处输入图像描述

以下是Morfic的一个小补充答案 :

1.)如何在每列的标题中禁用排序function

如果列是可排序的,Grid会询问Container(请参阅Grid中的appendColumn-method)。 如果要禁用所有列的排序,可以覆盖Container中的getSortableContainerPropertyIds并返回空集合:

 container = new BeanContainer(Dive.class) { @Override public Collection getSortableContainerPropertyIds() { LinkedList sortables = new LinkedList(); return sortables; } };