Only values from last pe:fluidGrid iteration of p:dataList are updated in model

273 views Asked by At

In my bean I have a HashMap<PDIDefinition, List<FluidGridItem>> which i use in a <p:dataList> to dynamicaly generate a form.

First I generate the FluidGridItems and place them in the hashMap:

private HashMap<PDIDefinition, List<FluidGridItem>> formItems = new HashMap<>();

private void generateModel() {

    for (PDIDefinition pdid : pdiDefinitions) {
        Fragment f = new Fragment();
        f.setDefinition(pdid.getFragmentDefinition());
        fragments.add(f);
        List<FluidGridItem> items = new ArrayList<>();
        formItems.put(pdid, items);

        //((DynamicField)((FluidGridItem)formItems.get(pdid).get(0)).getData()).label;

        for (FragmentValueDefinition fvd : f.getDefinition().getFragmentValues()) {
            FragmentValue fv = new FragmentValue();
            fv.setFragmentValueDefinition(fvd);
            fv.setFragmentId(f.getInner());
            if (f.getValues() == null) {
                f.setValues(new ArrayList<>());
            }
            f.getValues().add(fv);
            fragmentValues.add(fv); //not required?
            DynamicField df = new DynamicField((fv), items.size());
            fields.add(df);

            if ("String".equals(fvd.getType())) {
                items.add(new FluidGridItem(df, "stringValue"));
            }
            if ("Integer".equals(fvd.getType())) {
                items.add(new FluidGridItem(df, "integerValue"));
            }
            if ("Date".equals(fvd.getType())) {
                items.add(new FluidGridItem(df, "dateValue"));
            }
        }

    }

}

Then I convert them as stated in Displaying Hashmap keys and values in a primefaces DataTable:

public List<Map.Entry<PDIDefinition,List<FluidGridItem>>> getFormItemsForDisplay(){
    Set <Map.Entry<PDIDefinition,List<FluidGridItem>>> s = formItems.entrySet();
    return new ArrayList<>(s);
} 

Finaly in my view I generate the form using dataList and fluidGrid:

<p:dataList var="pdi" value="#{FormGenerator.formItemsForDisplay}" id="all" varStatus="loop" type="none">
        <p:panel id="panel" header="#{pdi.key.getFragmentDefinition().getFragmentName()}" style="margin-bottom:1em; width:100%;">

            <pe:fluidGrid  id="fluidGrid" value="#{pdi.value}" var="data"
                           hGutter="20" vGutter="10" widgetVar="fgwv_#{loop.index}">

                <pe:fluidGridItem type="stringValue"  id="txt_">
                    <div class="dynaFormLabel">
                        <p:outputLabel for="txt" value="#{data.label}"/>
                    </div>
                    <p:inputText id="txt" value="#{data.value}"/>
                </pe:fluidGridItem>
                <pe:fluidGridItem type="integerValue" id="int_">
                    <div class="dynaFormLabel">
                        <p:outputLabel for="int" value="#{data.label}"/>
                    </div>
                    <p:spinner id="int" value="#{data.value}" />
                </pe:fluidGridItem>
                <pe:fluidGridItem type="dateValue" id="cal_">
                    <div class="dynaFormLabel">
                        <p:outputLabel for="cal" value="#{data.label}"/>
                    </div>
                    <p:calendar id="cal" value="#{data.value}" showOn="button"/>
                </pe:fluidGridItem>

            </pe:fluidGrid>

        </p:panel>
    </p:dataList>

This results in the form I wish.

enter image description here

My problem is, that only the values from the last iteration of the dataList are updated on the server (PDF in the image).

Thanks for your Help.

1

There are 1 answers

0
bwright On BEST ANSWER

Solved it by replacing

<p:dataList var="pdi" value="#{FormGenerator.formItemsForDisplay}" 
    id="all" varStatus="loop" type="none">

by

<c:forEach var="pdi" items="#{FormGenerator.formItemsForDisplay}"
    id="all" varStatus="loop">

Got the Idea from https://zenidas.wordpress.com/recipes/jsf-update-single-entry-in-a-datalist-like-component/