是否右对齐Vaadin网格中的列内容?

是否右对齐Vaadin网格中的列内容?,vaadin,vaadin7,vaadin-grid,Vaadin,Vaadin7,Vaadin Grid,在新的Vaadin小部件(Vinerable的替代品)中,如何右对齐列中的数字或其他内容?我能想到的最简单的方法是定义自己的CSS类和样式生成器,这与我在处理表时所做的非常类似 @主题(“mytheme”) @Widgetset(“com.matritza.MyAppWidgetset”) 公共类MyUI扩展了MyUI{ @WebServlet(urlPatterns=“/*”,name=“MyUIServlet”,asyncSupported=true) @VaadinServletConfi

在新的Vaadin小部件(Vinerable的替代品)中,如何右对齐列中的数字或其他内容?

我能想到的最简单的方法是定义自己的CSS类和样式生成器,这与我在处理表时所做的非常类似

@主题(“mytheme”)
@Widgetset(“com.matritza.MyAppWidgetset”)
公共类MyUI扩展了MyUI{
@WebServlet(urlPatterns=“/*”,name=“MyUIServlet”,asyncSupported=true)
@VaadinServletConfiguration(ui=MyUI.class,productionMode=false)
公共静态类MyUIServlet扩展了VaadinServlet{
//嗯,默认的东西
}
@凌驾
受保护的void init(VaadinRequest VaadinRequest){
最终垂直布局=新建垂直布局();
布局。设置页边距(真);
设置内容(布局);
//创建网格
网格=新网格(“网格测试”);
//为网格创建一个特定的容器来容纳我们的人员
BeanItemContainer=新的BeanItemContainer(Person.class);
grid.setContainerDataSource(容器);
//定义我们自己的样式生成器
grid.setCellStyleGenerator(新grid.CellStyleGenerator(){
@凌驾
公共字符串getStyle(Grid.CellReference-CellReference){
if(“age”.equals(cellReference.getPropertyId())){
//当当前单元格为数字(如年龄)时,将文本向右对齐
返回“右对齐”;
}否则{
//否则,请将文本向左对齐
返回“左对齐”;
}
}
});
//生成一些虚拟数据
对于(int i=0;i<10;i++){
container.addItem(新人员(“姓名”+i,“姓氏”+i,i));
}
布局。添加组件(网格);
}
//基本类以快速简单的方式填充网格
公共阶层人士{
私有字符串名称;
私家姓;
私人互联网;
私人(字符串名称、字符串姓氏、整数年龄){
this.name=名称;
this.姓氏=姓氏;
这个。年龄=年龄;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getNames(){
返回姓氏;
}
public void setSurname(字符串姓氏){
this.姓氏=姓氏;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
}
}
还有基本的CSS样式

@mixin mytheme {
    @include valo;

    // Insert your own theme rules here
    .leftAligned {
      text-align: left;
    }

    .rightAligned {
      text-align: right;
    }
}
你应该看到类似的东西

顺便说一下,在Java 8及更高版本中,该样式生成器的新Lambda语法是:

grid.setCellStyleGenerator((grid.CellReference-CellReference)->{
if(“age”.equals(cellReference.getPropertyId())){
//当当前单元格为数字(如年龄)时,将文本向右对齐
返回“右对齐”;
}否则{
//否则,请将文本向左对齐
返回“左对齐”;
}
});

也可以使用现有样式,如v-align-right、v-align-middle等。只需查看Valo等主题已经包含了哪些内容,并仅在需要时扩展现有主题即可

下面是一个简单的示例,说明如何使用regexp实现cell生成器(根据字段名称匹配一个或多个字段)

由于这只是部分有用,因为大多数网格都有多个场,所以复合生成器可能很方便

public class CompositeCellStyleGenerator implements CellStyleGenerator {

List<CellStyleGenerator> generators = new ArrayList<>();

public CompositeCellStyleGenerator() {}

public void addCellStyleGenerator(CellStyleGenerator generator) {
    generators.add(generator);
}

@Override
public String getStyle(CellReference cellReference) {
    List<String> styles = new ArrayList<>();
    for (CellStyleGenerator generator : generators) {
        String style = generator.getStyle(cellReference);
        if (style != null) {
            styles.add(style);
        }
    }
    if (!styles.isEmpty()) {
        return styles.stream().collect(Collectors.joining(" "));
    }
    return null;
}
注意,复合生成器可以使用通用生成器,比如带有regexp定义的生成器和更复杂的特定于用例的生成器


希望这能帮助那些试图找到简单方法来设计细胞的人。愉快的实验。

为什么不干脆
grid.setCellStyleGenerator(cell->“age”).equals(cell.getPropertyId())?“v-align-right”:“v-align-left”)?@BrunoJCM Jukka Nikki的回答已经涵盖了这一点,但还是要感谢使用默认Valo样式的建议
public class CompositeCellStyleGenerator implements CellStyleGenerator {

List<CellStyleGenerator> generators = new ArrayList<>();

public CompositeCellStyleGenerator() {}

public void addCellStyleGenerator(CellStyleGenerator generator) {
    generators.add(generator);
}

@Override
public String getStyle(CellReference cellReference) {
    List<String> styles = new ArrayList<>();
    for (CellStyleGenerator generator : generators) {
        String style = generator.getStyle(cellReference);
        if (style != null) {
            styles.add(style);
        }
    }
    if (!styles.isEmpty()) {
        return styles.stream().collect(Collectors.joining(" "));
    }
    return null;
}
    RegexpCellStyleGenerator yearGenerator = new RegexpCellStyleGenerator("yearOfFoundation", "v-align-right");
    RegexpCellStyleGenerator nameGenerator = new RegexpCellStyleGenerator("name", "v-align-center");
    RegexpCellStyleGenerator nameGenerator2 = new RegexpCellStyleGenerator("name", "v-label-huge");

    CompositeCellStyleGenerator compositeGenerator = new CompositeCellStyleGenerator();

    compositeGenerator.addCellStyleGenerator(yearGenerator);
    compositeGenerator.addCellStyleGenerator(nameGenerator);
    compositeGenerator.addCellStyleGenerator(nameGenerator2);

    grid.setCellStyleGenerator(compositeGenerator);