Dynamic 2D arrayList table not working after refreshing in primefaces?

118 views Asked by At

I am using prime-faces 5 and jsf, I've created dynamic 2D arraylist table based on Tester and Date, first time its showing values correctly with date. But next time onwards its showing different tester name in single row.

enter image description here

The above showing first row correctly showing tester name but 2nd and 3rd showing different values. But i want to display same tester name in each row level.

MY XHTML:

<div align="left" class="width100">
            <div class="DTHeader">
                <h:form id="frmres">
                    <h:panelGrid columns="2"
                        style="padding-top:35px;padding-left:30px;">

                        <h:outputText class="lighttxt1" value="Schedule Date" />
                        <p:calendar id="button" value="#{schedulerbean.sch.scheDate}"
                            styleClass="cal schdate" label="Schedule Date" showOn="button"
                            pattern="dd/MM/yyyy HH:mm" showButtonPanel="true"
                            required="true">

                        </p:calendar>


                        <h:outputText value="TCU Goal" class="lighttxt1" />
                        <h:panelGrid columns="1" >

                        <p:inputText id="tcu" label="The Value given in TCU is"
                            style="border: 1px solid #A8A8A8 !important;background: transparent !important;"
                            styleClass="txtbig" value="#{schedulerbean.sch.tcu}"
                            keypadOnly="true" required="#{tcselectionbean.mancnt} != 0}" />


                    <h:outputText value="(Total Tcu:#{schedulerbean.tottalTCU})" class="lighttxt1" />

                        </h:panelGrid>  



                        <h:outputText value="Select Squad" styleClass="txtblack14" />


                        <p:selectOneMenu value="#{schedulerbean.sch.squadparam}"
                            panelStyleClass="panel" styleClass="DTDD ddwidth1">

                            <f:selectItem itemLabel="All Tester" itemValue=""
                                styleClass="txtblack14" />
                            <f:selectItems value="#{schedulerbean.squadLst}" var="squadval"
                                itemLabel="#{squadval}" itemValue="#{squadval}"
                                styleClass="txtblack14" />
                        </p:selectOneMenu>
                    </h:panelGrid>

                   <h:panelGrid columns="1">
                    <p:commandButton
                        actionListener="#{schedulerbean.resourcePlanWithPossibleEnddate}"
                        value="Calculate" styleClass="blubtn" 
                        update=":frmres:reservtable" />
                    <h:panelGroup id="reservtable">
                        <table class="bor bortd" style="margin-left:32px;">
                            <thead>
                                <tr>
                                    <th>Tester Name / Dates</th>

                                    <c:forEach var="resdate" items="#{schedulerbean.resDateList}">
                                        <th>#{resdate}</th>
                                    </c:forEach>
                                </tr>
                            </thead>

                            <c:forEach var="reserv" items="#{schedulerbean.resList}">
                                <tr>


                                    <td>                

                        <h:selectBooleanCheckbox value="#{schedulerbean.testerCheckboxmap[reserv.testerName]}"
                            styleClass="lighttxt1" />#{reserv.testerName}</td>

                                    <c:forEach var="resdate1" items="#{schedulerbean.resDateList}">
                                        <td class='c#{reserv.reserveType.get(resdate1)}'>
                                    #{reserv.testerName}        #{reserv.tcuMap.get(resdate1)}</td>
                                    </c:forEach>
                                </tr>

                            </c:forEach>
                        </table>
                    </h:panelGroup></h:panelGrid>




                        <p:commandLink styleClass="bluelinknew"
                            action="#{schedulerbean.setSchedulestep('step3')}"
                            update=":schmenufrm" value="Next" style="float:right;"
                            onclick="javascript:changets('schedule');" />





                </h:form>


            </div>
        </div>

How can i achieve this?

1

There are 1 answers

0
DavidS On

You are misusing the forEach JSTL tag. The tag is only in effect when the component tree is built, and the component tree is not re-built for post-backs and AJAX requests.

bwright's suggestion to use a <p:dataTable> is good, but you could also use <ui:repeat> if you don't want to render an HTML table.

https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

The most important thing to understand about the JSTL tags in JSF is that they do not represent components and never become a part of the component tree once the view has been built. Rather, they are tags which are actually responsible for building the tree in the first place. Once they have done their job they expire, are no more, cease to be, etc etc.

...

When is the view built?

Now that you understand that tag handlers are only effective when the tree is built, the next logical question should be well, when is tree built?

The short answer is that a new view is built for every request which is not a postback. During a postback, the view is reconstructed from saved state. Quite confusing, and not very obvious I know, but there you have it.

...

My list doesn't change size after deleting or adding an item

When your view was built you only had, say, 5 items. If you post back to this view and add or delete an item, your view still has 5 h:outputText components in it since it was restored from saved state. In this simple case, you should use ui:repeat and your tree will always contain one h:ouputText component which is iterated over with differing values of ${item}.

If you rely on using c:forEach to dynamically include different form components you could run into difficulty. Always try to think about what the resulting tree looks like and remember it doesn't change on a postback.

See also