Is primefaces live scrolling compatible with Lazy loading

[亡魂溺海] 提交于 2019-12-30 03:32:07

问题


I have a datatable where huge data sets need to be displayed.So i decided to go with primefaces live scrolling.I also got to know that to improve datatable performance with huge data sets we need to implement lazy loading.With reference to the primefaces showcase example here,what i observed that there in the statement

, a LazyDataModel needs to be implemented to query the datasource when pagination, sorting, filtering or live scrolling happens

live scrolling is mentioned but i cannot see the code to handle scrolling.It only has the code to handle sorting,filtering,row count and pagination.Can someone please clarify my confusion if both live scolling and lazy datamodel implementation are compatible with each other or should i add some code to handle live scrolling also.


回答1:


It works exactly the same as if you were having a pagination.

Here's 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.




回答2:


Looks like there's a new attribute for it in 6.1, liveScroll="true" https://github.com/primefaces/primefaces/issues/2105



来源:https://stackoverflow.com/questions/22194987/is-primefaces-live-scrolling-compatible-with-lazy-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!