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

Amira picture Amira · Sep 8, 2012 · Viewed 83.6k times · Source

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.....

Answer

Amira Manai picture Amira Manai · Sep 8, 2012

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}">