Vaadin - 在调整大小时更新单个标题列的文本

时间:2016-03-15 17:55:25

标签: vaadin

我正在尝试调整列的大小并向其添加resizeEventListener。这是在Vaadin网格中。 调整列的大小时,我希望应该使用我提供的新文本更新已调整大小的列。但是,就我而言,网格正在更新网格的所有列。其他栏目不应该被触及。

我正在使用Vaadin 7.6.4作为一个简单的日食项目。 代码如下: -

    grid.addColumnResizeListener(new ColumnResizeListener(){

        private static final long serialVersionUID = -4280346740116267368L;

        @Override
        public void columnResize(ColumnResizeEvent event) {
            String columnName = (String) event.getColumn().getPropertyId();
            TableColumnData tblData = getColumnData(tblDataLst,columnName);

            double colWidth = event.getColumn().getWidth();
            double colMinWidth = event.getColumn().getWidth() / 2 ;



               grid.getDefaultHeaderRow().getCell(columnName).setText(tblData.getShort_name());
               logger.info("call made to resize column="+columnName);





        }

    });
    grid.setImmediate(true);

1 个答案:

答案 0 :(得分:2)

简介:我刚刚使用带有未定义列宽的简单网格对7.6.4进行了测试,如果在监听器中添加日志消息,您将看到第一次触发resize事件,所有列都会调整为自动计算的大小。但是,后续事件只会针对相应的列触发,在我的情况下为Image

<强>来源:

public class MyGridComponent extends VerticalLayout {

    private static final Logger log = LoggerFactory.getLogger(MyGridComponent.class);

    public MyGridComponent() {
        BeanItemContainer<MyBean> dataSource = new BeanItemContainer<>(MyBean.class);
        Grid grid = new Grid(dataSource);
        addComponent(grid);


        grid.getColumn("image").setRenderer(new ImageRenderer());
        grid.setColumnOrder("image", "name", "surname");

        dataSource.addItem(new MyBean(null, "Agent", "Smith"));
        dataSource.addItem(new MyBean(null, "There is", "No spoon"));
        dataSource.addItem(new MyBean(null, "The", "One"));

        for (Grid.Column column : grid.getColumns()) {
            log.debug("Column [{}] has width [{}]", column.getHeaderCaption(), column.getWidth());
        }

        grid.addColumnResizeListener(event -> {
            Grid.Column column = event.getColumn();
            log.debug("Column resize event for [{}] was triggered by [{}] with new width [{}]", column.getHeaderCaption(), event.isUserOriginated() ? "user" : "vaadin", column.getWidth());
        });
    }

    public static class MyBean {
        private ExternalResource image;
        private String name, surname;

        public MyBean(ExternalResource image, String name, String surname) {
            this.image = image;
            this.name = name;
            this.surname = surname;
        }

        public ExternalResource getImage() {
            return image;
        }

        public String getName() {
            return name;
        }

        public String getSurname() {
            return surname;
        }
    }
}

生成日志

 com.example.grid.MyGridComponent: Column [Image] has width [-1.0]
 com.example.grid.MyGridComponent: Column [Name] has width [-1.0]
 com.example.grid.MyGridComponent: Column [Surname] has width [-1.0]
 com.example.grid.MyGridComponent: Column resize event for [Image] was triggered by [user] with new width [149.171875]
 com.example.grid.MyGridComponent: Column resize event for [Name] was triggered by [user] with new width [168.703125]
 com.example.grid.MyGridComponent: Column resize event for [Surname] was triggered by [user] with new width [182.09375]
 com.example.grid.MyGridComponent: Column resize event for [Image] was triggered by [user] with new width [195.171875]
 com.example.grid.MyGridComponent: Column resize event for [Image] was triggered by [user] with new width [334.171875]
 com.example.grid.MyGridComponent: Column resize event for [Image] was triggered by [user] with new width [175.171875]

根据original enhancement/feature ticket中的描述判断,可能也可能不是错误:

  

调整列的大小不应影响其他列的大小,并且应在用户调整某些列的大小后禁用列的自动调整大小(如表中所示)

它可能是一个错误,因为它会触发所有列的调整大小,但同时它可能不是一个错误,因为所需的行为是从自动大小(-1)切换到计算或用户定义的大小确实发生了。

解决方法:快速&amp;肮脏的解决方法是在构建网格时手动定义列的大小,这与下面的内容类似(请注意,只有在定义了大小后才应添加侦听器,否则每个{{}都会触发它。 1}}来电):

setWidth

这似乎产生了预期的行为:

    for (Grid.Column column : grid.getColumns()) {
        column.setWidth(100);
        log.debug("Column [{}] has width [{}]", column.getHeaderCaption(), column.getWidth());
    }

从我所做的各种测试中,这似乎只有在用户触发调整大小时才会发生。例如,如果使用com.example.grid.MyGridComponent: Column [Image] has width [100.0] com.example.grid.MyGridComponent: Column [Name] has width [100.0] com.example.grid.MyGridComponent: Column [Surname] has width [100.0] com.example.grid.MyGridComponent: Column resize event for [Image] was triggered by [user] with new width [133.0] ,则其他2列将保留未定义(-1)宽度,并且在第一次用户调整大小时将再次发生此问题。此外,只要至少有一列具有未定义的大小,这似乎就会发生,这意味着您必须手动将大小设置为所有列,以使此变通方法生效。

<小时/> Outro :我非常有兴趣听到关于此问题的其他更多专家意见,以及这个初始事件是否应该传播给听众......