DataTable - Scrolling
DataTable supports client and live scrolling. In client mode, whole data is rendered, by enabling liveScroll option data is loaded during scrolling.
Source
<h:form>
<h3>Y Scrolling</h3>
<p:dataTable var="car" value="#{tableBean.cars}" scrollable="true" scrollHeight="150" id="dataTable1">
<p:column headerText="Model" footerText="Model">
<h:outputText value="#{car.model}" />
</p:column>
<p:column headerText="Year" footerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer" footerText="Manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color" footerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
<h3>X Scrolling</h3>
<p:dataTable var="car" value="#{tableBean.carsSmall}" scrollable="true" scrollWidth="400" id="dataTable2">
<p:column headerText="Model" footerText="Model">
<h:outputText value="#{car.model}" />
</p:column>
<p:column headerText="Year" footerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer" footerText="Manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color" footerText="Year">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
<h3>XY Scrolling</h3>
<p:dataTable var="car" value="#{tableBean.cars}" scrollable="true" scrollWidth="400" scrollHeight="150" id="dataTable3">
<p:column headerText="Model" footerText="Model">
<h:outputText value="#{car.model}" />
</p:column>
<p:column headerText="Year" footerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer" footerText="Manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color" footerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
<h3>Live Scrolling</h3>
<p:dataTable var="car" value="#{tableBean.carsLarge}" scrollRows="20"
scrollable="true" liveScroll="true" scrollHeight="150" id="dataTable4" >
<p:column headerText="Model">
<h:outputText value="#{car.model}" />
</p:column>
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
</h:form>
package org.primefaces.examples.view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.primefaces.examples.domain.Car;
public class TableBean implements Serializable {
static {
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";
manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}
private final static String[] colors;
private final static String[] manufacturers;
private List<Car> cars;
private List<Car> carsLarge;
public TableBean() {
cars = new ArrayList<Car>();
carsLarge = new ArrayList<Car>();
populateRandomCars(cars, 50);
populateRandomCars(carsLarge, 200);
}
private void populateRandomCars(List<Car> list, int size) {
for(int i = 0 ; i < size ; i++)
list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
}
public List<Car> getCars() {
return cars;
}
public List<Car> getCarsLarge() {
return cars;
}
private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private String getRandomManufacturer() {
return manufacturers[(int) (Math.random() * 10)];
}
private String getRandomModel() {
return UUID.randomUUID().toString().substring(0, 8);
}
}
