Nested DataTable within a UI:Repeat
fgonzalezaguirre Feb 19, 2009 9:13 PMIm having trouble with the following scenario:
A CustomerSegment can have many subsegments ,and a subsegment can have many filters. What I basically did, was create a ui:repeat that displays all of the subsegments of a given customer segment, and for each subsegment i display a dataTable with all the filters.
The code is as follows:
customersegment.xhtml (this is where the create and edit operations on a customer segment happen)
<ui:repeat value="#{msubsegments}" var="ss"> <rich:panel> <f:facet name="header">Subsegment</f:facet> <h:dataTable value="#{ss.filters}" var="f"> <h:column> <h:outputText value="#{f.variable.name}"/> </h:column> <h:column> <h:outputText value="#{f.value}"/> </h:column> <h:column> <h:commandLink action="#{customerSegmentManager.editFilter(ss,f)}" value="Edit" /> </h:column> <h:column> <h:commandLink action="#{customerSegmentManager.deleteFilter(ss,f)}" value="Delete" /> </h:column> </h:dataTable> <h:commandLink action="#{customerSegmentManager.addFilter}" value="Add Filter" immediate="true"/> </rich:panel> <br/> </ui:repeat>
addFilter.xhtml (when you want to add or edit a filter for a given subsegment, you get taken to this page)
<h:form> Variable: <h:selectOneMenu value="#{filter.variable}"> <s:selectItems value="#{variables}" var="v" label="#{v.name}" /> <s:convertEntity/> </h:selectOneMenu> Association: <h:selectOneMenu value="#{filter.association}"> </h:selectOneMenu> Filter: <h:inputText value="#{filter.value}"/> Value: <h:commandButton value="Save" action="#{customerSegmentManager.saveFilter}" /> </h:form>
and finally this is the customerSegmentManager, which manages all of the CRUD operations of a customerSegment:
@Name("customerSegmentManager") @Scope(ScopeType.CONVERSATION) public class CustomerSegmentManager { @In(create=true) @Out CustomerSegment customerSegment; @DataModel List<CustomerSegment> segments; @DataModel List<Subsegment> msubsegments; @DataModelSelection("msubsegments") @Out(required=false) Subsegment subs; @In(create=true) @Out Filter filter; @In EntityManager entityManager; @End public String save() { if(segments!=null) { segments.add(customerSegment); } entityManager.persist(customerSegment); return "/customersegments.xhtml"; } public String addFilter() { filter = new Filter(); return "/addFilter.xhtml"; } public String editFilter(Subsegment s,Filter f) { subs=s; filter = s.getFilters().get(s.getFilters().indexOf(f)); return "/addFilter.xhtml"; } public String deleteFilter(Subsegment s,Filter f) { s.getFilters().remove(f); return ""; } public String saveFilter() { if(subs.getFilters()==null) { subs.setFilters( new ArrayList<Filter>() ); } if(subs.getFilters().contains(filter)) { return "/customersegment.xhtml"; } else { filter.setSubsegment(subs); subs.getFilters().add(filter); } return "/customersegment.xhtml"; } public String addSubsegment() { if(customerSegment.getSubsegments()==null) { ArrayList<Subsegment> list = new ArrayList<Subsegment>(); customerSegment.setSubsegments(list); } Subsegment s = new Subsegment(); customerSegment.getSubsegments().add(s); s.setSegment(customerSegment); msubsegments = customerSegment.getSubsegments(); return ""; } @Begin(join=true) public String createSegment() { return "/customersegment.xhtml"; } @Factory("msubsegments") public void getSubsegments() { msubsegments = ( customerSegment.getSubsegments()==null ) ? new ArrayList<Subsegment>() : customerSegment.getSubsegments(); } @Factory("segments") public void getSegments() { segments = (List<CustomerSegment>) entityManager.createQuery("Select s from CustomerSegment s").getResultList(); } }
the add filter operation works as expected, the subs variable gets correctly filled with the selected subsegment, takes you to the addFilter page, and when you click save it correctly gets saved to the selected subsegment.
However, when I try to do the edit or delete operations for a filter, the ss variable is ALWAYS the last subsegment of the list. I don't really know why. I can't expose a datamodel for the nested dataTable(the filters one) since it depends on each subsegment. So i tried passing the paramaters edit(subsegment,filter), but subsegment always returns with the last subsegment of customersegment, without the parameters, the same thing happens, the commandLink sets subs to the last subsegment (I knew without parameters it wouldn't work, as your not clicking the ui:repeat, but the nested h:dataTable), however, i really need a way to know not only which filter was selected, but to which subsegment it belongs.
Please give me any ideas or solutions (changing the whole workflow, though not ideal, is acceptable)