Creating a DataScroller with Primefaces

In this tutorial you will learn how to use a Primefaces DataScroller to let your application load additional set of Data as you scroll down the Web page (on Demand scrolling). This component is available since Primefaces 5.

The structure of the component dataScroller is quite similar to a dataTable. The key attribute of it is the chunkSize by which you can control the amount of items that will be loaded and displayed at once. Here follows an example which displays the World’s country list in chunks:

<!DOCTYPE html>
<html xmlns=""
    <p:dataScroller value="#{dataScrollerView.countries}" var="c" chunkSize="10">
        <f:facet name="header">
            Data Scroller Demo
        <h:panelGrid columns="2" style="width:100%" columnClasses="logo,detail">
                <h:panelGrid columns="2" cellpadding="5">
                    <h:outputText value="Country code:" />
                    <h:outputText value="#{c.code}" style="font-weight: bold"/>
                    <h:outputText value="Name:" />
                    <h:outputText value="#{}" style="font-weight: bold"/>

In order to work you will need a DataScrollerView to hold the list of Country objects:

package com.mastertheboss.bean;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.mastertheboss.model.Country;
import com.mastertheboss.service.CountryService;
public class DataScrollerView implements Serializable {
    private List<Country> countries;
    private CountryService service;
    public void init() {
    	countries = service.createCountries();
    public List<Country> getCountries() {
        return countries;
    public void setService(CountryService service) {
        this.service = service;

The Country class is a simple Java Bean with some properties in it:

package com.mastertheboss.model;


public class Country implements Serializable {
	private String code;
	private String name;

	public String getCode() {
		return code;

	public void setCode(String code) {
		this.code = code;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public Country() {


Finally, the CountryService populates the List of Country using as source the JDK’s Locale getISOCountries:

package com.mastertheboss.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.mastertheboss.model.Country;
@ManagedBean(name = "countryService")
public class CountryService {

    public List<Country> createCountries() {
    	String[] locales = Locale.getISOCountries();
    	List<Country> list = new ArrayList<Country>();
    	for (String countryCode : locales) {

    		Locale obj = new Locale("", countryCode);
    		Country c = new Country();
    		c.setCode( obj.getCountry());

        return list;

Here is your DataScroller view in action:

primefaces datascroller

Looking for other ways to display your large set of data ? Then check this article where we show how to enable live scrolling on a dataTable in order to lazy load its content