我有这种情况,每行数据表都有复选框。 如果选中复选框,则可以删除多个数据并仅编辑一个数据。
我成功地能够实现删除但是我遇到了编辑原因,checkID将返回0的大小。
我正在关注此example
的记录下面是我的代码:
list.xhtml
<h:form>
<h:commandButton value="Edit" action="#{transportationController.prepareEdit()}"/>
<h:commandButton value="Delete" action="#{transportationController.delete()}">
<p:dataTable value="#{transportationController.items}" var="item" <p:column>
<f:facet name="header">
</f:facet>
<h:selectBooleanCheckbox value="#{transportationTypeController.checkedID[item.id]}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Remark"/>
</f:facet>
<h:outputText value="#{item.remark}"/>
</p:column>
</p:dataTable>
</h:form>
managedbean ---&gt; transportationcontroller.java transportcontroller是一个会话
private Map<Integer, Boolean> checkedID = new HashMap<Integer, Boolean>();
private List<Transportation> transportation = new ArrayList<Transportation>();
//setters and getters.
public List<Transportation> getCheckedTransportation() {
List<Transportation> checkedItems = new ArrayList<Transportation>();
for (Transportation trans : transportation) {
if (checkedID.get(trans.getId())) {
checkedItems.add(trans);
}
}
checkedID.clear();
return checkedItems;
}
//delete function
public String delete() {
List<Transportation> list = getCheckedTransportation();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
ejbFacade.remove(list.get(i));
}
return "List";
} else {
FacesMessage msg = new FacesMessage("Please select data", "Please select data");
JsfUtil.getCurrent().addMessage("warning-message", msg);
return null;
}
}
//edit function
public void prepareEdit() {
List<Transportation> list = getCheckedTransportation();
if (list.size() == 1) {
current = list.get(0);
} else {
FacesMessage msg = new FacesMessage("Please select at least one data", "Please select at least one data");
JsfUtil.getCurrent().addMessage("warning-message", msg);
}
}
public Transportation getSelected() {
if (current == null) {
current = new Transportation();
selectedItemIndex = -1;
}
return current;
}
实体类---&gt; Transportation.java
public class Transportation implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
//etc..
}
在调试时,Map checkedID的大小始终为0,但是对于删除,它的工作正常。我现在已经被困在这几个小时但是我无法确定问题,因为为什么checkID总是大小为0,只有当它正常工作才能进行编辑。任何帮助,将不胜感激。感谢。
注意:我知道primefaces p:datatable有selectionMode =“multiple” 和选择,但我不使用它。我只想使用primefaces 因为它的分页。