简体   繁体   中英

DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

I'm trying to create a DataTable with Multiple Row Selection but i'm getting an error here's the link of the tutorial http://www.primefaces.org/showcase/ui/datatableRowSelectionMultiple.jsf :

Here's my 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>

Managed Bean :

 @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;
}




 }

i'm getting this error:

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

just add this attribute rowKey to the datatable tag :

<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}" 
 var="userObj"
 rowKey="#{userObj.name}"selection="#{projectAdminisrationMB.selectedUsers}"
 selectionMode="multiple" rowIndexVar="rowIndex"
 binding="#{table2}">

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

Alternatively to rowKey you can wrap your data in a custom model which really implements org.primefaces.model.SelectableDataModel . This is helpful if

  • all of your your classes have the same kind of @Id (eg a long ) and can implement the same interface (eg EjbWithId )
  • you want to add additional functionalities to your data which are not domain specific and don't belong eg User .

The interface may be something like this:

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

Then a generic implementation of SelectableDataModel for all your classes can be used:

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();}
}

In your @ManagedBean :

private PrimefacesEjbIdDataModel<User> dmUser; //+getter
dmUser = new PrimefacesEjbIdDataModel<User>(administrationProjectFinal.getUserList());

first check whether you've added rowKey="#{userObj.id}"

then you need to have the data table List set in filteredValue attribute of your data table in xhtml, instead of value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM