1

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?

Georgian
  • 8,795
  • 8
  • 46
  • 87

2 Answers2

1

I've implemented a DataScroller backed by a live database.

I'm not sure what happens in postEJB, so I can't be sure, but the call you make to it looks peculiar to me. Try using a debugger (or adding some logger messages) to see if load() is being called when you scroll down, and to see exactly what postEJB.loadLatestPosts() is returning. My suspicion -- and forgive me if I'm being presumptive -- is that the EJB call should actually be something like postEJB.loadLatestPosts(first, pageSize), and that startingFrom and CHUNK_SIZE can be ditched.

You may also want to set the row count of the data model; i.e., lazyModel.setRowCount(postCount);

I'm not sure if the DataScroller actually uses it, but it could not hurt. :)

Nick
  • 2,827
  • 4
  • 29
  • 39
  • I doesn't work for me **UNLESS** I set rowCount by calling *LazyDataModel.setRowCount()* with the correct number **outside** the *load()* method. – jahroy Feb 16 '21 at 16:50
1

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.