0

我有这种情况,每行数据表都有复选框。如果选中复选框,您可以删除多个数据并仅编辑一个数据。

我成功地实现了删除,但我被编辑卡住了,因为 checkID 将返回 0 的大小。

对于我遵循这个例子的记录

以下是我的代码:

列表.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 ---> transportcontroller.java transportcontroller 是一个 sessionscoped

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

实体类--->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" 和 selections 但我不使用它。我只想使用 primefaces 进行分页。

4

0 回答 0