瓦丁网格 : Show all rows

标签 vaadin vaadin7 vaadin-grid

我如何制作新的 Grid Vaadin 7 中的小部件显示所有数据行(而不是滚动)?

最佳答案

高度模式

首先,您必须切换高度模式。您想要的是面向行的高度,而不是面向 CSS 的高度。

myGrid.setHeightMode( HeightMode.ROW );

然后您可以设置要显示的行数。您可以指定小数行,因为 number-of-rows 参数是 double 值。
this.setHeightByRows( myDouble );

避免零行

因此,要显示所有行,请传递一个包含支持网格的包含行数的 double 值。但检查零,因为网格不容忍没有行。如果容器中没有数据,请指定任意数量的空行。
int size = this.getContainerDataSource().size();
double rows = ( size > 0 ) ? size : myDefaultRowCount;

两排错误

在我自己的项目中,我在 Vaadin 7.4.2 中遇到了一个令人讨厌的错误,其中将行数设置为 2(范围为 2.0d 到 2.7d)会导致高 CPU 负载和几分钟的延迟,因为页面部分加载但似乎从未结尾。我无法在示例应用程序中重现,但无法确定我自己的应用程序中的任何其他原因。作为一种解决方法,我的代码仅使用 3.0d (或 2.8d)代替任何出现的 2.0d .
if ( rows == 2.0d ) {         rows = 2.8d;  // Workaround for weird bug.
}

示例子类

这是 Grid 的一个子类,它为行集中的任何更改添加了一个监听器。监听器重置 Grid 的高度以显示所有新数据行。
package com.example;

import com.vaadin.data.Container;
import com.vaadin.shared.ui.grid.HeightMode;

/**
 * Adds one feature to Grid: Automatically resize the height to show all
 * rows.
 *
 * @author Basil Bourque. 
 * Released under ISC License, http://opensource.org/licenses/ISC
 */
public class GridAllRowsTall extends Grid
{
    static double defaultRowsCount = 3.0d;

    // -----|  Constructors  |-------------------------
    public GridAllRowsTall ()
    {
        super();
        this.initialize();
    }

    public GridAllRowsTall ( Container.Indexed dataSource )
    {
        super( dataSource );
        this.initialize();
    }

    public GridAllRowsTall ( String caption )
    {
        super( caption );
        this.initialize();
    }

    public GridAllRowsTall ( String caption , Container.Indexed dataSource )
    {
        super( caption , dataSource );
        this.initialize();
    }

    // -----|  Init  |-------------------------
    @Override
    void initialize ()
    {
        // Add a listener so when the set of items changes, re-size the Grid to display all rows.
        if ( this.getContainerDataSource() instanceof Container.ItemSetChangeNotifier ) {
            Container.ItemSetChangeNotifier n = ( Container.ItemSetChangeNotifier ) this.getContainerDataSource();
            n.addItemSetChangeListener( ( Container.ItemSetChangeEvent event ) -> {
                this.showAllRows();
            } );
        }
    }

    // -----|  Features  |-------------------------
    public void showAllRows ()
    {
        this.setHeightMode( HeightMode.ROW );
        int size = this.getContainerDataSource().size();
        double rows = ( size > 0 ) ? size : GridAllRowsTall.defaultRowsCount; // Cannot set height to zero rows. So if no data, set height to some arbitrary number of (empty) rows.
        if ( rows == 2.0d ) {
            rows = 3.0d; // Workaround for weird bug where a value of "2 rows" ( 2.0d - 2.7d ) causes a huge slowdown and major CPU load, and page never finishes updating.
        }
        this.setHeightByRows( rows );
    }
}

关于瓦丁网格 : Show all rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29310266/

相关文章:

java - 如何创建安全的 vaadin 14 登录表单?

java - 通过单击 GWT 按钮更改连接器中 Vaadin 自定义小部件的状态并将其获取到服务器端

Vaadin Grid ItemClickListener 无法使用 ImageRenderer 注册对列的点击

java - 如何将 Vaadin 8 Grid 的高度设置为未定义?

spring - 集成spring和vaadin

java - 如何在 vaadin 中设置网格单元格样式

checkbox - 以编程方式取消选中 Vaadin 上下文菜单中的复选框

java - 保存时上传?

hover - 当用户将鼠标指针悬停在 Vaadin 7 中的网格或表格中的行上时,会发生 "mouse over"的事件吗?

java - 验证 vaadin 7 网格中的列值