简体   繁体   English

primefaces动态嵌套数据表

[英]primefaces dynamic nested datatable

With primefaces I made a multicheckbox with several divisions to choose. 使用primefaces,我创建了一个带有多个分区的multicheckbox。 After clicking the submit Button the chosen divisions should be listed in a datatable with the associated items in a subtable. 单击提交按钮后,所选分区应在数据表中列出,而相关项目在子表中。 There could be 0 - n items which should be shown in the subtable. 子表中可能显示0-n个项目。

division1 -> item1, item2 division2 -> item3, item4, item5 部门1->项目1,项目2部门2->项目3,项目4,项目5

The first impression of the webpage looks fine. 网页的第一印象看起来不错。 When I only choose one division and press submit the fitting items will be shown. 当我仅选择一个部门并按提交时,将显示合适的项目。 But when I want to display other items they get overwritten. 但是当我要显示其他项目时,它们会被覆盖。 eg when I choose division2 it will display the items item1, item2 and item 5. 例如,当我选择Division2时,它将显示项目item1,item2和item 5。

How can I make it so that the items will be loaded correctly every time? 我该如何做才能使每次正确装入物品? I also changed the subtable to a nested datatable, but the behaviour was the same. 我也将子表更改为嵌套数据表,但是行为是相同的。

I've primefaces version 4.0 我有primefaces版本4.0

Below is my code: 下面是我的代码:

division_list.xhtml: division_list.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Gruppenverwaltung</title>
</h:head>
<h:body>
    <f:view>
        <h:form id="myForm">
            <h2>
                <h:outputText value="Division List" />
            </h2>
            <h:outputText value="Grid: " />
            <p:selectOneRadio id="grid" value="#{divisionController.selectedDivisions}" layout="grid" columns="3"
                converter="DivisionConverter">
                <f:selectItems value="#{divisionController.divisions}" />
            </p:selectOneRadio>
            <p:commandButton value="Submit" update=":myForm" />
            <p:dataTable id="dtDivisions" value="#{divisionController.selectedDivisions}" var="division">
                <p:subTable id="stItems" var="item" value="#{division.items}">><f:facet name="header">
                        <h:outputText value="#{division.name}" />
                    </f:facet>
                    <p:column>
                        <h:inputText value="#{item.name}" />
                    </p:column>
                    <p:column>
                        <p:commandButton icon="ui-icon-disk" action="#{divisionController.doUpdateItem(item)}" />
                        <p:commandButton icon="ui-icon-trash" oncomplete="confirm.show()">
                            <f:setPropertyActionListener target="#{divisionController.selectedItem}" value="#{item}" />
                        </p:commandButton>
                    </p:column>
                </p:subTable>
            </p:dataTable>
        </h:form>
        <p:confirmDialog message="Gewählter Eintrag löschen?" widgetVar="confirm"> --><h:form
                id="formDialog">
                <p:commandButton value="Yes" action="#{divisionController.doDeleteItem}"
                    styleClass="ui-confirmdialog-yes" icon="ui-icon-check" oncomplete="confirmation.hide()" />
                <p:commandButton value="No" type="button" onclick="confirm.hide()" styleClass="ui-confirmdialog-no"
                    icon="ui-icon-close" />
            </h:form>
        </p:confirmDialog>
    </f:view>
</h:body>
</html>

DivisonController.java: DivisonController.java:

...
@ManagedBean
@ViewScoped
public class DivisionController implements Serializable {
...
   private List<DivisionDto>    divisions;

   private List<DivisionDto>    selectedDivisions;
...
   @PostConstruct
   public void init() throws MappingTOException {

   ....
      if (divisions == null) {
         divisions = club.getDivisions();
      }
   }
...

DivisionDto.java DivisionDto.java

...
public class DivisionDto {

   ....
   private List<ItemDto> items;
   private String        name;
...

ItemDto.java ... public class ItemDto { ... private DivisionDto division; ItemDto.java ...公共类ItemDto {...私有DivisionDto部门;

private String name; 私有字符串名称; ... ...

DivisionConverter.java DivisionConverter.java

@FacesConverter(value = "DivisionConverter")
public class DivisionConverter implements Converter {

   private static Map<String, DivisionDto> divisionCache = new HashMap<String, DivisionDto>();

   @Override
   public Object getAsObject(FacesContext context, UIComponent component, String value) {

      DivisionDto val = divisionCache.get(value);
      if (val == null) {
         val = new DivisionDto();
         val.setName(value);
      }
      return val;
   }

   @Override
   public String getAsString(FacesContext context, UIComponent component, Object value) {

      if (value instanceof DivisionDto) {
         DivisionDto division = (DivisionDto) value;
         divisionCache.put(division.getName(), division);
         return division.getName();
      } else {
         return "";
      }
   }

}

I would be really grateful for any help. 我将非常感谢您的帮助。

I solved the issue wie a datatable in a datatable and put a filter on the first datatable. 我解决了将数据表放入数据表的问题,并在第一个数据表上放置了过滤器。 Now it works as expected. 现在它可以按预期工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM