Search code examples
javaspringjsfjsf-2primefaces

Saving datatable with a independent inputbox using PrimeFaces


I want to save my data table value with the inputtext value enter by the user. my problem is my save method is not working and i am not able to find any idea to save it.

my xhtml is:

<h:message for="message"/>  
        <h:panelGrid columns="2" cellspacing="4" columnClasses="control-label">

            <h:outputText
                    value="#{msg['elicense.examinationform.personal.proposeofexam']}" />
                <p:selectOneMenu id="propExam" value="#{issueAdmitCardBean.examination}" 
                    converter="omnifaces.SelectItemsConverter" editable="false"
                    required="true" label="Examination Applied For" styleClass="dropdownWidth">
                    <f:selectItem itemLabel="---Select One---" itemValue="" />
                    <f:selectItems value="#{issueAdmitCardBean.testExamNames}" var="test" itemLabel="#{test.name}" itemValue="#{test}" />

                </p:selectOneMenu>

                <h:outputText value="Select Degree Category :" />
                <p:selectOneMenu id="degreeList"
                    value="#{issueAdmitCardBean.degree}" editable="false"
                    converter="omnifaces.SelectItemsConverter" required="true"
                    label="Please Select degree" styleClass="dropdownWidth">
                    <f:selectItem itemValue="" itemLabel="---Select One---" />
                    <f:selectItems value="#{issueAdmitCardBean.degreeNames}"
                        var="degree" itemLabel="#{degree.name}" itemValue="#{degree}" />
                </p:selectOneMenu>


                <h:outputText value="Center of Examination :"/>
                <p:selectOneMenu id="centerId" value="#{issueAdmitCardBean.center}" editable="false" 
                converter="omnifaces.SelectItemsConverter" required="true" label="Please select Center"
                styleClass="dropdownWidth">
                <f:selectItem itemValue="" itemLabel="---Select One---"/>
                <f:selectItems value="#{issueAdmitCardBean.centerNames}" var="center" 
                itemLabel="#{center.name}" itemValue="#{center}"/>
                <p:ajax   listener="#{issueAdmitCardBean.readVenuefromCenter(issueAdmitCardBean.center.id)}" update="venueNames"  />
                </p:selectOneMenu>


                <h:outputText value="Total No. of Candidates Under this Category :"/>
                <!-- <h:outputText value="#{issueAdmitCardBean.getTotalNoofCandidates()}"/>  -->



                 </h:panelGrid> 


                <p:dataTable id="venueNames" var="test" value="#{issueAdmitCardBean.venueNames}" 
                paginator="false" rows="10">

                    <p:column headerText="Id">
                        <h:outputText value="#{test.id}" />
                    </p:column>

                    <p:column headerText="Name Of Venue">
                        <h:outputText value="#{test.name}" />
                    </p:column>

                    <p:column headerText="Maximum Capacity ">
                        <h:outputText value="#{test.capacity}" />
                    </p:column>

                     <p:column headerText="Allot No. of Candidates ">
                        <h:inputText value="issueAdmitCardBean.allotedCandidates" style="width:50px;"/>
                    </p:column>

                    <p:column headerText="Exam Date">
                       <p:calendar value="issueAdmitCardBean.examDate" pattern="dd-MM-yyyy" navigator="true" style="width:10px;"/>
                     </p:column>

               </p:dataTable>


            <p:commandButton  icon="Save" value="Submit" action="#{issueAdmitCardBean.issueRollNoForExamination()}"/>
            </p:panel>

    </div>

</h:form>

and my managed bean save method is:

public void issueRollNoForExamination() {


        for(Venue venue :venueNames) 
        {
            System.out.println("name of vanue"+venue.getName());
        }
        }

but my save method will never execute. please help me to save my datatable with the update value.


Solution

  • You need to make a few changes in order to be able to update the venues in your backing bean.

    1. Add a styleClass and rowIndexVar variable to your p:dataTable component

      <p:dataTable id="venueNames" var="test" value="#{issueAdmitCardBean.venueNames}" styleClass="dataTable"  rowIndexVar="index" paginator="false" rows="10">
      
    2. change your input text to

      <h:inputText value="#{issueAdmitCardBean.venueNames[index].allotedCandidates}" style="width:50px;"/>
      
    3. Add an update to your save button

      <p:commandButton  icon="Save" value="Submit" update="@(.dataTable)" action="# {issueAdmitCardBean.issueRollNoForExamination()}"/>
      

    This will fix your immediate problem of not being able to update the values of allotedCandiadates. I didn't understand well the part in your question when you say "my save method will never execute". But what I see in your code, the method should be triggered just fine, so if the issue is that you're not hitting the method in your backing bean, than the root cause is something else, that is not obvious from the snippet you've posted.