CommandLink not Working on a Lazy Loaded Primefaces Datascroller

后端 未结 3 1315
感情败类
感情败类 2021-02-10 05:04

I\'m having an issue with lazy loading a Primefaces Datascroller component.

I have a jsf page that should display 10 events on page load. If the user wants to see more h

3条回答
  •  灰色年华
    2021-02-10 05:36

    I finally had time to spend on this issue and I found a workaround. It's a hack so maybe the proper solution would be to use a different component or create my own.

    It seems like Primefaces DataScroller limitation that occurs when using the DataScroller with a LazyDataModel. It would seem that the component was not designed to do this.

    To avoid this issue, I implemented my own lazy loading where the same list instance is returned in addition to the newly added elements.

    Here is my previous example modified to implement this new lazy loading pattern:

    The html page:

    
        
            #{event.name}
            
    
            
        
    
        
    
    

    The DataScroller no longer has lazy="true", chunkSize="10", uses a list called events as the value and declares an empty loader facet (to avoid auto-load more when the bottom of the list is reached). I used a commandLink that calls backing.loadMore() and updates the form to replace the loader facet.

    The backing bean:

    @Named("backing")
    @ViewScoped
    public class DataScrollerBacking implements Serializable {
    
    private static final long serialVersionUID = 4012320411042043677L;
    
    private static final Logger LOGGER = Logger.getLogger(DataScrollerBacking.class);
    
    private static final Integer CHUNK_SIZE = 10;
    
    @DataSource
    @Inject
    private String dataSource;
    
    private WebEventDAO webEventDAO;
    
    private List events;
    private Integer totalCount;
    private Date startDate;
    private Date endDate;
    
    @PostConstruct
    public void init() {
        webEventDAO = CommonDAOFactory.getInstance(dataSource).getWebEventDAO();
        search();
    }
    
    public void search() {
        DateTime start = new DateTime(2014, 1, 1, 0, 0 ,0);
        startDate = start.toDate();
        endDate = start.plus(Years.ONE.toPeriod()).minus(Seconds.ONE.toPeriod()).toDate();
    
        try {
            totalCount = webEventDAO.getSearchByPeriodRaceTypeAndRaceStatusForCompanyCount(Collections.singletonList(1), startDate, endDate, null, null);
            events = new ArrayList(totalCount);
            loadMore();
        } catch (DAOException e) {
            LOGGER.error("An error occurred while retrieving events.", e);
        }
    }
    
    public void loadMore() {
        List newEvents = new ArrayList(CHUNK_SIZE);
        try {
            newEvents = webEventDAO.searchByPeriodRaceTypeAndRaceStatusForCompany(Collections.singletonList(1), startDate, endDate, null, null, true, events.size(), CHUNK_SIZE);
            events.addAll(newEvents);
        } catch (DAOException e) {
            LOGGER.error("An error occurred while retrieving events.", e);
        }
    
    }
    
    public void initViewEventDetails(Integer index, Event event){
        LOGGER.info("index=" + index + " eventname=" + event.getName());
    }
    
    public String getDataSource() {
        return dataSource;
    }
    
    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }
    
    public List getEvents() {
        return events;
    }
    
    public void setEvents(List events) {
        this.events = events;
    }
    
    public Integer getTotalCount() {
        return totalCount;
    }
    
    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }}
    

    In the backing bean, the search method counts the total number of events, saves that information and calls loadMore() to load the first 10 events in the events list.

    When the more button is clicked, loadMore() is called again and the next 10 events are appended at the end of events list.

    Now when I click on newly loaded elements, the commandLink invokes the backing bean with the correct value.

提交回复
热议问题