Vaadin Grid: Show all rows

How to make a new Grid widget Vaadin 7 show all rows of data (rather than scrolling)?

+5
source share
1 answer

Height mode

First you need to switch the height mode. Instead of having CSS-oriented height, you need line-oriented height.

 myGrid.setHeightMode( HeightMode.ROW ); 

Then you can set the number of rows to display. You can specify fractional strings because the argument number-of-rows is double.

 this.setHeightByRows( myDouble ); 

Avoid null lines

So, to show that all rows pass a double number with the number of rows in your database containing Grid support. But check for zero, as the grid does not tolerate any rows. If you do not have data in your container, specify an arbitrary number of empty lines.

 int size = this.getContainerDataSource().size(); double rows = ( size > 0 ) ? size : myDefaultRowCount; 

Two line error

In my own project, I came across an unpleasant error in Vaadin 7.4.2, where setting the number of lines from two (range from 2.0d to 2.7d) leads to high processor load and delays in minutes, because the page partially loads, but never ends. I cannot reproduce in the sample application, but I cannot determine any other reason in my own application. As a workaround, my code just uses 3.0d (or 2.8d ) instead of any 2.0d .

 if ( rows == 2.0d ) { rows = 2.8d; // Workaround for weird bug. } 

Class subclass

Here is a subclass of Grid that adds a listener for any change in the rowset. The listener resets the grid height to display all rows of fresh data.

 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 ); } } 
+9
source

Source: https://habr.com/ru/post/1216321/


All Articles