我有一个数据表,我想添加一个新行。这样做时,我收到以下错误 javax.servlet.ServletException:启用选择时,DataModel必须实现org.primefaces.model.SelectableDataModel。
如何解决我的问题?
这是我的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" />
这是我的bean代码
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;
}
答案 0 :(得分:5)
好的,发现了问题。添加新项时,其ID默认为null,id用作rowkey。所以我在添加一个新的时候将id初始化为-1并且它可以工作。
答案 1 :(得分:1)
无需将id设置为-1,只需编写如下函数:
public int getRowKey(Integer id) {
return id!=null?id:-1;
}
并通过@rowKey中的el表达式调用它