36

我正在尝试创建具有多行选择的 DataTable,但出现错误,这是教程http://www.primefaces.org/showcase/ui/datatableRowSelectionMultiple.jsf的链接:

这是我的xhtml:

    <p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}" 
                     var="userObj"
                     selection="#  
         {projectAdminisrationMB.selectedUsers}"
 selectionMode="multiple" rowIndexVar="rowIndex"binding="#{table2}">
<p:column id="column3">
<f:facet name="header">
<h:outputText value=" user "></h:outputText>
</f:facet>

      <h:outputText value="#{userObj.name}"/>  

                            /
  <h:outputText value="#{userObj.lastName}"></h:outputText>

  &nbsp;
   <h:outputText value="#{userObj.firstName}"></h:outputText>
  </p:column>

    <f:facet name="footer">  
     <p:commandButton id="addProjectUser" value=" Add " onclick="dlg1.show()" />  
      <p:commandButton id="deleteProjectUser" value=" Delete " />  

    </f:facet> 

</p:dataTable>

托管豆:

 @ManagedBean
 @SessionScoped
 public class ProjectAdminisrationMB implements Serializable {

private static final long serialVersionUID = 1L;

private String projectName;
private List <User> projectUsersList;
private List<User> projectNoUsersList;
private List<User> selectedUsers;

private String projectAdmin;


public ProjectAdminisrationMB() {
    super();
    AdministrationProjectFinal administrationProjectFinal =new    
             AdministrationProjectFinal();
    this.projectUsersList=administrationProjectFinal.getUserList();
    this.projectNoUsersList=administrationProjectFinal.getNotUserList();
}



public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}



public List<User> getProjectUsersList() {
    return projectUsersList;
}



public void setProjectUsersList(List<User> projectUsersList) {
    this.projectUsersList = projectUsersList;
}



public String getProjectAdmin() {
    return projectAdmin;
}

public void setProjectAdmin(String projectAdmin) {
    this.projectAdmin = projectAdmin;
}

public List<User> getProjectNoUsersList() {
    return projectNoUsersList;
}



public void setProjectNoUsersList(List<User> projectNoUsersList) {
    this.projectNoUsersList = projectNoUsersList;
}



public List<User> getSelectedUsers() {
    return selectedUsers;
}



public void setSelectedUsers(List<User> selectedUsers) {
    this.selectedUsers = selectedUsers;
}




 }

我收到此错误:

  javax.faces.FacesException: DataModel must implement     
  org.primefaces.model.SelectableDataModel when selection is enabled.....
4

4 回答 4

86

只需将此属性 rowKey 添加到数据表标签:

<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}" 
 var="userObj"
 rowKey="#{userObj.name}"selection="#{projectAdminisrationMB.selectedUsers}"
 selectionMode="multiple" rowIndexVar="rowIndex"
 binding="#{table2}">
于 2012-09-08T19:42:26.433 回答
19

如果您尝试将新项目添加到基础列表并忘记为该新项目的 rowKey 分配值,则可能会出现此错误。

于 2013-03-21T14:28:32.897 回答
5

或者,rowKey您可以将数据包装在真正实现的自定义模型中org.primefaces.model.SelectableDataModel。这很有帮助,如果

  • 您所有的类都具有相同类型的@Id(例如 a long)并且可以实现相同的接口(例如EjbWithId
  • 您想为您的数据添加额外的功能,这些功能不是特定于域且不属于例如User.

界面可能是这样的:

public interface EjbWithId
{
  public long getId();
  public void setId(long id);
}

SelectableDataModel然后可以使用所有类的通用实现:

public class PrimefacesEjbIdDataModel <T extends EjbWithId>
       extends ListDataModel<T> implements SelectableDataModel<T>
{    
  public PrimefacesEjbIdDataModel(List<T> data)
  {  
    super(data);
  }  

  @Override public T getRowData(String rowKey)
  {  
    List<T> list = (List<T>) getWrappedData();  

    for(T ejb : list)
    {  
      if(ejb.getId()==(new Integer(rowKey))){return ejb;}  
    }
    return null;  
  }  

  @Override public Object getRowKey(T item) {return item.getId();}
}

在你的@ManagedBean

private PrimefacesEjbIdDataModel<User> dmUser; //+getter
dmUser = new PrimefacesEjbIdDataModel<User>(administrationProjectFinal.getUserList());
于 2012-09-09T07:27:26.033 回答
2

首先检查您是否添加了 rowKey="#{userObj.id}"

那么您需要在 xhtml 中的数据表的filteredValue属性中设置数据表 List ,而不是value

于 2019-01-06T15:10:13.953 回答