JSF PrimeFaces DataScroller with Lazy Loading and no pagination (infinite scroll)

datascrollerjsf-2lazy-loadingprimefacesprimefaces-extensions

Since it's a new component, it might not be as stable.

Has anyone managed to implement this data scroller with lazy loading using an actual data source (e.g. DB)?


index.xhtml

<p:dataScroller value="#{postLoader.lazyModel}" var="post" lazy="true" chunkSize="5">

    <article>
        <h:outputText value="#{post.title}" />
        <br />
        <p:graphicImage value="/files/#{post.blobPath}" />
    </article>

    <hr />
</p:dataScroller>

PostLoaderBacking

@ManagedBean(name = "postLoader")
@ViewScoped
public class PostLoaderBacking implements Serializable
{

    // ==================== 1. Static Fields ==============================

    private static final long serialVersionUID = 2732106678777694908L;

    private static final int CHUNK_SIZE = 5;


    // ==================== 2. Instance Fields ============================

    @EJB
    private PostEJB postEJB;

    private int postCount;

    private LazyDataModel<PostEntity> lazyModel;


    // ==================== 4. Constructors ===============================

    @PostConstruct
    public void init()
    {
        postCount = postEJB.getPostCount();

        lazyModel = new LazyDataModel<PostEntity>() {

            private static final long serialVersionUID = -4742720028771554420L;

            @Override public List<PostEntity> load(final int first, final int pageSize,
                    final String sortField, final SortOrder sortOrder,
                    final Map<String, Object> filters) { 


                final int startingFrom = postCount - first; 

                return postEJB.loadLatestPosts(startingFrom, CHUNK_SIZE);
            }
        };
    }


    // ==================== 7. Getters & Setters ======================

    public LazyDataModel<PostEntity> getLazyModel()
    {
        return lazyModel;
    }

}

My solution works fine up to a point. It only loads two chunks. For example, if my chunk size is 2 (no pun intended), then it will load 4 posts in total, although I have much more where those came from.

Why does it stop loading at some point? I scroll down the page and nothing happens. On which criteria does the primefaces implementation decide if it should load?

Best Answer

I had the same problem.

My problem was that I used setRowCount(currentNumberOfRows) in the LazyDataModel. This resulted in loading content exactly twice - as your problem also was.

Try setting the rowCount to the total number of rows that are available in the database.

Related Topic