【问题标题】:Vaadin Grid Table : How to disable Sort Function and set the color of one columnVaadin Grid Table:如何禁用排序功能并设置一列的颜色
【发布时间】:2016-02-24 12:36:07
【问题描述】:

我在 Vaadin 中使用 Grid 表来表示数据。 为此,我试图找出以下两个问题:

1.) 如何在每列的Header中禁用排序功能

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

【问题讨论】:

    标签: java sorting colors vaadin


    【解决方案1】:

    这里是对Morficanswer的一个小补充:

    1.) 如何在每列的Header中禁用排序功能

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

    container = new BeanContainer<String, Dive>(Dive.class) {
                @Override
                public Collection<?> getSortableContainerPropertyIds() {
                    LinkedList<Object> sortables = new LinkedList<Object>();
                    return sortables;
                }
            };
    

    【讨论】:

      【解决方案2】:

      首先,我发现Vaadin docs 是开始寻求帮助的好地方。对于练习的其余部分,假设我们有一个 Grid,其中包含 3 个简单的列 c1、c2 和 c3:

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



      1.) 如何在每列的Header中禁用排序功能

      遍历每一列并将其可排序属性设置为false

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

      或者只获取您想要的一列并设置其可排序属性:

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



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

      was done with the table 类似,您需要在theme 中定义您的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;
              }
          }
      });
      

      这应该会产生以下内容:

      【讨论】:

      • 这种方法不再有效,因为方法 setCellStyleGenerator() 已在 Grid 类中删除。相反,我认为可以使用以下内容,但我尚未对此进行验证:stackoverflow.com/questions/60464437/…
      猜你喜欢
      • 2021-09-03
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      相关资源
      最近更新 更多