簡體   English   中英

PrimeFaces DataTable延遲加載兩次

[英]PrimeFaces DataTable lazy loading is loaded twice

問題是每次我嘗試過濾表時,我的LazyDataModel實現中的重寫方法load()都會被調用兩次

我的LazyDataModel實現:

public class LazyPostDataModel extends LazyDataModel<Post> {

    private List<Post> data;
    private final PostService postService;

    public LazyPostDataModel(PostService postService) {
        this.postService = postService;
        data = new ArrayList<>();
    }

    @Override
    public Post getRowData(String rowKey) {
        Long id = Long.valueOf(rowKey);
        for (Post p : data) {
            if (p.getId().equals(id))
                return p;
        }
        return null;
    }

    @Override
    public Object getRowKey(Post post) {
        return post.getId();
    }

    @Override
    public List<Post> load(int first, int pageSize, String sortField,      SortOrder sortOrder, Map<String,Object> filters) {
        PostFilter filter = new PostFilter(filters, first, pageSize);
        FilteredDataModel<Post> postDataModel =    postService.findFilteredList(filter);
        data = postDataModel.getData();
        this.setRowCount(postDataModel.getCount().intValue());
        return data;
    }
}

我的xhtml視圖:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:composition>
        <p:panel header="Posts to Review">
            <ui:include src="ModerationPostContextMenu.xhtml" />

            <p:dataTable id="ModerationPostTable" value="# {moderationPostController.posts}" var="post" lazy="true" rows="25"  widgetVar="ModerationPostTable"
                     paginator="true" rowKey="#{post.id}"  selectionMode="single" selection="#{moderationPostController.selected}"
                     paginatorTemplate="{FirstPageLink} {PreviousPageLink}  {CurrentPageReport} {NextPageLink} {LastPageLink}" >

                <p:ajax event="rowSelect"   update="@form:contextMenu" />
                <p:ajax event="rowUnselect" update="@form:contextMenu" />

                <p:column headerText="Id">
                    <h:outputText value="#{post.id}"/>
                </p:column>
                <p:column headerText="Creation Date">
                    <h:outputText value="#{post.creationDate}"/>
                </p:column>
                <p:column headerText="Author Id">
                    <h:outputText value="#{post.author.id}"/>
                </p:column>
                <p:column headerText="Author Nickname">
                    <h:outputText value="#{post.author.nickname}"/>
                </p:column>
                <p:column headerText="Text">
                    <h:outputText value="#{post.text}" />
                </p:column>
                <p:column headerText="Media" width="200">
                    <p:graphicImage value="#{mediaController.buildUrl(post.media)}" 
                                width="200" height="200"/>
                </p:column>

                <p:column headerText="Status" filterBy="#{post.moderationApproved}">
                    <f:facet name="filter">
                        <p:selectOneButton onchange="PF('ModerationPostTable').filter()">
                            <f:converter converterId="javax.faces.Boolean" />   
                            <f:selectItem itemLabel="Approved" itemValue="true" />
                            <f:selectItem itemLabel="Rejected" itemValue="false" />
                            <f:selectItem itemLabel="Review" itemValue="" />
                        </p:selectOneButton>
                    </f:facet>
                    <h:outputText value="#{post.moderationApproved}" />
                </p:column>

            </p:dataTable>
        </p:panel>
    </ui:composition>

</html>

和控制器

@Named("moderationPostController")
@SessionScoped
@Transactional(Transactional.TxType.REQUIRED)
public class ModeratioPostController extends BaseController implements Serializable {

    @Inject PostService postService;
    private LazyPostDataModel posts;

    // Selection
    private Post selected;

    @Override
    protected void initSpecific() {
        posts = new LazyPostDataModel(postService);
    }

    // Actions
    public void approve() {
        if (selected == null) return;

        Post post = postService.find(selected.getId());
        post.setModerationApproved(Boolean.TRUE);
        postService.edit(post);
    }

    public void reject() {
        if (selected == null) return;        

        Post post = postService.find(selected.getId());
        post.setModerationApproved(Boolean.FALSE);
        postService.edit(post);
    }

    public LazyPostDataModel getPosts() {
        return posts;
    }

    public void setPosts(LazyPostDataModel posts) {
        this.posts = posts;
    }

    public Post getSelected() {
        return selected;
    }

    public void setSelected(Post selected) {
        this.selected = selected;
    }

}

這是因為你有

filterBy="#{post.moderationApproved}"

<p:selectOneButton onchange="PF('ModerationPostTable').filter()">

這兩個方法都會調用一次過濾器,並導致兩次調用load方法。 僅使用其中之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM