当实现延迟加载时,行选择在primefaces(5.3)数据表中不起作用,但它在没有延迟加载的情况下工作

时间:2016-01-29 16:14:40

标签: jsf primefaces lazy-loading

这是我的数据表:

<h:form id="eventListForm">
                <p:contextMenu id="eventDataTableContextMenu" for="datalist">  
                <p:menuitem value="#{myBundle.Create}" onclick="document.getElementById('eventListForm:createButton').click();" icon="ui-icon-plus"/>
                <p:menuitem value="#{myBundle.View}" onclick="document.getElementById('eventListForm:viewButton').click();" icon="ui-icon-search"/>
                <p:menuitem value="#{myBundle.Edit}" onclick="document.getElementById('eventListForm:editButton').click();" icon="ui-icon-pencil"/>
                <p:menuitem value="#{myBundle.Delete}" onclick="document.getElementById('eventListForm:deleteButton').click();" icon="ui-icon-trash"/>
            </p:contextMenu>
            <p:dataTable id="datalist"
                         value="#{eventController.lazyModel}"
                         var="item"
                         rowKey="#{item.eventId}"
                         paginator="true"
                         paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                         rows="10"
                         rowsPerPageTemplate="10,20,30,40,50"
                         selectionMode="single"
                         selection="#{eventController.selected}"
                         lazy="true">

                <p:ajax event="rowSelect" update="@form:createButton,@form:viewButton,@form:editButton,@form:deleteButton,@form:eventDataTableContextMenu"/>
                <p:ajax event="rowUnselect" update="@form:createButton,@form:viewButton,@form:editButton,@form:deleteButton,@form:eventDataTableContextMenu"/>
                <p:ajax event="contextMenu" update="@form:createButton @form:viewButton @form:editButton @form:deleteButton"/>
                <p:ajax event="rowDblselect" onsuccess="document.getElementById('eventListForm:viewButton').click();"/>

这是控制器:

 public class EventController {

        IEventService eventService;
        List<EventDTO> items;
        private EventDTO selected;
        private Integer priorityIdSelected;

        private LazyDataModel<EventDTO> lazyModel;

        public void init() {
            //items = eventService.getAll();
            lazyModel = new EventLazyLoader(eventService);
        }


public void onRowSelect(SelectEvent event) {
    FacesMessage msg = new FacesMessage("Event Selected", ((EventDTO) event.getObject()).getEventId().toString());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

这是延迟加载器实现:

public class EventLazyLoader extends LazyDataModel<EventDTO> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private List<EventDTO> datasource;

    public EventLazyLoader(IEventService eventServiceImpl) {
        this.datasource = eventServiceImpl.getAll();
    }

    @Override
    public EventDTO getRowData(String rowKey) {
        for(EventDTO event : datasource) {
            if(event.getEventId().equals(rowKey))
                return event;
        }
        return null;
    }

    @Override
    public Object getRowKey(EventDTO event) {
        return event.getEventId();
    }

    @Override
    public List<EventDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
        List<EventDTO> data = new ArrayList<EventDTO>();
          //filter
        for(EventDTO event : datasource) {
            boolean match = true;
            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        String filterProperty = it.next();
                        Object filterValue = filters.get(filterProperty);
                        String fieldValue = String.valueOf(event.getClass().getField(filterProperty).get(event));
                        if(filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                            match = true;
                        } else {
                            match = false;
                            break;
                        }
                    } catch(Exception ex) {
                        match = false;
                    }
                }
            }
            if(match) {
                data.add(event);
            }
        }

        //sort
        if(sortField != null) {
            Collections.sort(data, new EventLazySorter(sortField, sortOrder));
        }

        //rowCount
        int dataSize = data.size();
        this.setRowCount(dataSize);

        //paginate
        if(dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        }
        else {
            return data;
        }
    }
}

控制器还有一个项目列表(java.util.List)如果我使用此列表作为数据列表,行选择工作没有任何问题,似乎问题是懒惰的装载,但我不知道为什么会发生这种情况。

顺便说一句,非常感谢有人能给我的所有提示和帮助。

1 个答案:

答案 0 :(得分:2)

最后我解决了这个问题,问题是LazyDataModel实现中的逻辑错误,特别是在&#34; getRowData(String rowKey)&#34;方法。 在评论&#34;错误在这里&#34; 问题是在primefaces展示中,该示例使用equals()方法进行比较,我的问题是我试图比较相等整数(我的dto的id)与从行选择中获得的字符串值,所以我解析该字符串,最后比较整数值(如果你考虑它就是一个愚蠢的错误)

public class EventLazyLoader extends LazyDataModel<EventDTO> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static final Logger logger = Logger.getLogger(EventLazyLoader.class);

    private List<EventDTO> datasource;

    public EventLazyLoader(IEventService eventServiceImpl) {
        this.datasource = eventServiceImpl.getAll();
    }

    @Override
    public EventDTO getRowData(String rowKey) {
        int intRowKey = Integer.parseInt(rowKey);
        for(EventDTO event : datasource) {
            if(event.getEventId() == intRowKey) {//Error here
                return event;
            }
        }
        return null;
    }

    @Override
    public Object getRowKey(EventDTO event) {
        return event.getEventId();
    }

    @Override
    public List<EventDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
        List<EventDTO> data = new ArrayList<EventDTO>();
          //filter
        for(EventDTO event : datasource) {
            boolean match = true;
            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        String filterProperty = it.next();
                        Object filterValue = filters.get(filterProperty);
                        String fieldValue = String.valueOf(event.getClass().getField(filterProperty).get(event));
                        if(filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                            match = true;
                        } else {
                            match = false;
                            break;
                        }
                    } catch(Exception ex) {
                        match = false;
                    }
                }
            }
            if(match) {
                data.add(event);
            }
        }

        //sort
        if(sortField != null) {
            Collections.sort(data, new EventLazySorter(sortField, sortOrder));
        }

        //rowCount
        int dataSize = data.size();
        this.setRowCount(dataSize);

        //paginate
        if(dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        }
        else {
            return data;
        }
    }
}