Straight scroll compatible with Lazy loading

I have a datatable where I need to display huge data arrays. So I decided to go with direct straight scrolls. I also found out that to improve data performance with huge data sets, we need to implement lazy loading. With reference to an example of a storefront here , that I noticed that there is a statement

LazyDataModel must be implemented to query the data source when pagination, sorting, filtering, or direct scrolling occurs

direct scrolling is shown, but I do not see the code to handle scrolling. It only has code to handle sorting, filtering, line counting and pagination. Someone, please clarify my confusion if both live scolling and lazy datamodel implementations are compatible with each other or I have to add code to handle live scrolling.

+4
source share
2 answers

It works exactly as if you had pagination.

Here is a small example:

XHTML

<p:dataTable var="user"
        value="#{bean.lazyDataModel}"
        scrollRows="20"
        liveScroll="true"
        scrollHeight="500"
        lazy="true"
        scrollable="true">

        <p:column headerText="name">
           <h:outputText value="#{user.name}" />
        </p:column>

</p:dataTable>

Bean

@ManagedBean
@ViewScoped
public class Bean {

    private LazyDataModel<User> lazyDataModel;

    @EJB
    UserEJB userEJB;

    @PostConstruct
    public void init() {
       lazyDataModel = new LazyUserModel(userEJB);
    }

    public LazyDataModel<User> getLazyDataModel() {
        return lazyDataModel;
    }

    public void setLazyDataModel(LazyDataModel<User> lazyDataModel) {
       this.lazyDataModel = lazyDataModel;
    }

   //setters and getters for userEJB
}

LazyUserModel

public class LazyUserModel extends LazyDataModel<User> {
   private Integer findAllCount;

   @EJB
   private UserEJB userEJB;

   public LazyUserModel(UserEJB userEJB) {
       this.userEJB = userEJB;
   }


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

       List<User> data = new ArrayList<User>();
       // pageSize is scrollRows="20" in the datatable
       data = userEJB.findAll(first, pageSize); 
       // findAll is using query.setFirstResult(first).setMaxResults(pageSize).getResultList()

      // rowCount
       if (findAllCount == null) {
           findAllCount = userEJB.findAllCount();
           this.setRowCount(findAllCount);
       }

       return data;
   }

}

Hope this helps.

+6
source

It looks like the new attribute for it is in 6.1, liveScroll = "true" https://github.com/primefaces/primefaces/issues/2105

0

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


All Articles