2
votes

I have a datatable where I want to add a new row. When doing so I get the following error javax.servlet.ServletException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

How can I solve my problem?

This is in my xhtml

<h:commandLink  action="#{workOrderDetail.addOrderItem}" >
        <img src="${path.staticRootUrl}images/add.png" border="0" alt="${msg.workorderdetail_neworderitem}" title="${msg.workorderdetail_neworderitem}"/>
     </h:commandLink>
<p:dataTable styleClass="ptable100" id="orderItems" var="orderItem" value="#{workOrderDetail.orderItems}" width="100%" height="200" widgetVar="results"
     emptyMessage="#{msg.all_lists_no_records_found}" selection="#{workOrderDetail.selectedOrderItem}" selectionMode="single" onRowSelectUpdate=":detail:sub"
     rowKey="#{orderItem.id}"
<p:ajax event="rowSelect"  update=":detail:sub" />

and this is my bean code

List<IMWSOrderItem> orderList = null;
public void addOrderItem() throws MWSException {
  IMWSOrderItem newOrderItem = getWorkOrder().getMWSOrder().getMWSOrderItem_Set().getNewMWSOrderItem();      
  getWorkOrder().getMWSOrder().getMWSOrderItem_Set().addMWSOrderItem(newOrderItem);
  orderList = null;   
}
public List<IMWSOrderItem> getOrderItems() throws MWSException {
  if (orderList == null) {
     orderList = new ArrayList(Arrays.asList(getWorkOrder().getMWSOrder().getMWSOrderItem_Set().getMWSOrderItems()));
  }
  return orderList;
}
public IMWSOrderItem getSelectedOrderItem() {
  return selectedOrderItem;
}
public void setSelectedOrderItem(IMWSOrderItem newSelectedOrderItem) {
  this.selectedOrderItem = newSelectedOrderItem;
}
2
Which PF version? Setting the rowKey attribute to point to the unique identifier of the var object should solve it, but you already have set it. - BalusC
PrimeFaces 3.2. I know, I had to add it because at first the row selection didn't work. But that is working fine now. Displaying the details of every row in a separate section on the page. But when adding a new row it fails. strange. - roel
Ok, found the problem. When adding a new item, its ID is default to null and the id is used as rowkey. So I initialized the id to -1 when adding a new one and it works. - roel

2 Answers

5
votes

Ok, found the problem. When adding a new item, its ID is default to null and the id is used as rowkey. So I initialized the id to -1 when adding a new one and it works.

1
votes

No need to set the id to -1, just write a function like:

public int getRowKey(Integer id) { return id!=null?id:-1; }

and call it from an el expression in @rowKey