使用primefaces过滤器后获取空行键

时间:2017-07-03 14:10:55

标签: jsf primefaces jsf-2 datatable

enter image description here当我没有在primefaces数据表中使用过滤器并尝试选择行然后按下编辑它运行良好并采取所选行。但是当我使用primefaces过滤器然后选择过滤行然后编辑 当选择启用异常时,我得到了org.primefaces.model.SelectableDataModel。

我知道这意味着我有一个空行键但我不知道为什么。我正在使用有效行键(id)数据表的主键,当我使用调试时我发现它得到了id在失败情况下两次,第一次获得正确的过滤ID,第二次获得null id。 我的问题为什么它获取rowkey id两次,第二次得到null并导致异常

    <h:body>
    <h:form prependId="false" id="growlForm">
        <p:growl id="growl" showDetail="false" />
    </h:form>
    <h:form id="dataForm">
    <p:panel id="ingerdientsTable">
    <f:facet name="header">
          <h:outputText value="Standard Food List" />
        </f:facet>  
         <p:dataTable id="ingedientsTable" widgetVar="ingedientsTable" var="ingerdients" resizableColumns="true"
                    selectionMode="single" selection="#{mealBean.selectedStandardIngerdients}"
                     rowKey="#{ingerdients.getId()}" value="#{mealBean.allIngerdients}" rowsPerPageTemplate="5,10,25,50" rows="20"
                     paginator="true" style="padding-top:10px;padding-bottom:10px" tableStyle="table-layout: auto"
                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown} ">

                    <p:column headerText="Food Type"  filterBy="#{ingerdients.name}"><h:outputText value="#{ingerdients.name}" /></p:column>
                    <p:column headerText="Protein(gm)"  filterBy="#{ingerdients.containedProtiens}"><h:outputText value="#{ingerdients.containedProtiens}" /></p:column>
                    <p:column headerText="Carbs(gm)"  filterBy="#{ingerdients.containedCarbs}"><h:outputText value="#{ingerdients.containedCarbs}" /></p:column>
                    <p:column headerText="Fats(gm)"  filterBy="#{ingerdients.containedFats}"><h:outputText value="#{ingerdients.containedFats}" /></p:column>
                    <p:column headerText="Total Calories" filterBy="#{ingerdients.totalCalories}"><h:outputText value="#{ingerdients.totalCalories}" /></p:column>
                    <p:column styleClass="action-column">
                        <f:facet name="header">
                            <h:outputText value="Actions" />
                        </f:facet>
                        <p:commandButton id="addToMeal" value="Add To Meal" icon="ui-icon-create" update="addToMealDialog"
                            action="#{mealBean.showIngerdientsToMealDialog(ingerdients)}" immediate="true"
                            title="Add To meal" ajax="true">
                        </p:commandButton>
                        <p:tooltip for="addToMeal" value="Add To Meal"
                            showEffect="fade" hideEffect="fade" />
                            </p:column>

                    </p:dataTable>
        </p:panel>




public class StandardIngerdients{
    @Id
    @Column(name="Id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    @ManyToOne
    @JoinColumn(name="FolderPathId",referencedColumnName="Id",nullable=true)
    private FolderPath folderPath;

    @Column(name="Name")
    private String name;
    @Column(name="ContainedProteins")
    @NotNull
    private Double containedProtiens;
    @Column(name="ContainedCarbs")
    @NotNull
    private Double containedCarbs;
    @Column(name="ContainedFats")
    @NotNull
    private Double containedFats;
    @Column(name="TotalCalories")
    @NotNull
    private Double totalCalories;
    @Column(name="ImageName")
    private String imageName;
    public Integer getId() {
        return id;
    }
    public void setId(Integer Id) {
        this.id = Id;
    }
    public FolderPath getFolderPath() {
        return folderPath;
    }
    public void setFolderPath(FolderPath folderPath) {
        this.folderPath = folderPath;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Double getContainedProtiens() {
        return containedProtiens;
    }
    public void setContainedProtiens(Double containedProtiens) {
        this.containedProtiens = containedProtiens;
    }
    public Double getContainedCarbs() {
        return containedCarbs;
    }
    public void setContainedCarbs(Double containedCarbs) {
        this.containedCarbs = containedCarbs;
    }
    public Double getContainedFats() {
        return containedFats;
    }
    public void setContainedFats(Double containedFats) {
        this.containedFats = containedFats;
    }
    public Double getTotalCalories() {
        return totalCalories;
    }
    public void setTotalCalories(Double totalCalories) {
        this.totalCalories = totalCalories;
    }
    public String getImageName() {
        return imageName;
    }
    public void setImageName(String imageName) {
        this.imageName = imageName;
    }


}

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。问题是我让我的班级实施Serializable,以保证&#34;完整性&#34; (可以这么说)数据是为了不消失......所以你的课程就是这样:

 public class StandardIngerdients implements Serializable {

答案 1 :(得分:0)

你可以尝试下面的代码,我认为解决它

rowKey="#{ingerdients.id}"