24

I was trying to implement one DataTable Editable with RowSelection enabled.

But it is throwing out an exception:

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

I implemented the DataModel in both the object class and the managed bean class but the error is the same. Now only a blank table appears for me. Please help.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
user1281029
  • 1,513
  • 8
  • 22
  • 32
  • Possible duplicate http://stackoverflow.com/questions/12333764/datamodel-must-implement-org-primefaces-model-selectabledatamodel-when-selection.. – Wanna Coffee Feb 14 '14 at 07:40

6 Answers6

41

There are two solutions for this problem:

  1. Adding rowKey, selection and selectionMode attributes to dataTable
  2. Implementing SelectableDataModel interface and extending a DataModel like ListDataModel for filling the dataTable

First one is Simpler. Adding rowKey="#{myEntity.carID}" to your p:dataTable should solve your problem

Apostolos
  • 10,033
  • 5
  • 24
  • 39
rags
  • 2,580
  • 19
  • 37
  • Hi,I seperated entity from model class.I had almost completed the second point but there is one error coming selection="#{editBean.selectedSubStudyPlan}": Property 'selectedSubStudyPlan' not readable on typecom.buddhiedge.server.entity.StudyplanCategory Please check the XHTML part also Here selection="#{editBean.selectedSubStudyPlan}"and selectedSubStudyPlan is of type StudyplanCategorey which is entity class – user1281029 May 09 '12 at 16:34
  • 2
    did you try giving rowKey to the dataTable ? This is simpler. – rags May 10 '12 at 07:40
  • @rags upvoted you for providing an alternate solution which is insightful. I haven't read the docs pertaining to PriceFaces data table, but is this where you have found the solution? – Oh Chin Boon Jan 06 '13 at 00:32
22

You can get this error if you try to add a new item to the underlying list and forget to assign a value to the new item's rowKey (the rowKey is null).

Nublodeveloper
  • 1,301
  • 13
  • 20
3

In addition to the Solutions given by rags, I would like to mention that if the row key is "NULL" or if your entire List is "NULL" you may get the same error, even if you have completed all the above mentioned steps. If you want to show 0 row, return a list with 0 items. Don't return null for the list.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
RajdeepS
  • 31
  • 2
2

The error message indicates that your DataModel does not implement the SelectableDataModel interfaces. It needs to be a separate class. This is an example from the PF showcase how the data table definition needs to be done:

import org.primefaces.model.SelectableDataModel;  

public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {    
   ...
}
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • My entity class is StudyPlanCategorey.java which implements SelectableDataModel.In addition to this I have a ManagedBean class also, here also i tried to implement it but did not work. – user1281029 May 09 '12 at 10:26
  • I don't know if it is possible to use an entity class as DataModel. Maybe it is an option to separate entity class from data model. – Matt Handy May 09 '12 at 10:28
0

Don't forget to surround the rowKey value within EL syntax.

rowKey="row.id" 

will fail but

rowKey="#{row.id}" 

will succeed.

Jan B.
  • 6,030
  • 5
  • 32
  • 53
J Slick
  • 929
  • 11
  • 17
0

Possibly the error is because the row.id is empty or null in my case the solution be change this:

rowKey="row.id" 

to this:

rowKey="row" 

And the Object of datatable, for example

List<Row> collectionOfDataTable = new ArrayList<>();

Row (Object) implements Serializable:

public class Row implements Serializable{...}