Vaadin 瓦丁网格:显示所有行
如何使Vaadin 7中的新小部件显示所有数据行(而不是滚动)?高度模式 首先,您必须切换高度模式。与CSS导向的高度不同,您需要的是行导向的高度Vaadin 瓦丁网格:显示所有行,vaadin,vaadin7,vaadin-grid,Vaadin,Vaadin7,Vaadin Grid,如何使Vaadin 7中的新小部件显示所有数据行(而不是滚动)?高度模式 首先,您必须切换高度模式。与CSS导向的高度不同,您需要的是行导向的高度 myGrid.setHeightMode( HeightMode.ROW ); 然后可以设置要显示的行数。您可以指定分数行,因为“行数”参数是双精度的 this.setHeightByRows( myDouble ); 避免零行 因此,要显示所有行,请使用网格中的行数传递一个double。但是检查零,因为网格不允许任何行。如果容器中没有数据,请指
myGrid.setHeightMode( HeightMode.ROW );
然后可以设置要显示的行数。您可以指定分数行,因为“行数”参数是双精度的
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的一个子类,它为行集合中的任何更改添加了侦听器。侦听器重置网格的高度以显示所有新数据行
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 );
}
}