8 Replies Latest reply: May 21, 2013 8:30 AM by shreevat RSS

    Question on populating a field based on 2 other fields

    shreevat
      Hi,

      I am looking for a solution wherein I need to populate a field (say no of days) based on the other two fields selected by the user (say date from and date to). As soon as user selects the value on "date to" field, "no of days" field should be auto-populated as the value from "date to - date from". I have tried using setter and getter methods on all three field, set valuechangelistener on date to field, set auto submit to true and in the valuechangelistener event code, I am doing the calculations as mentioned in the below snippet.

      public void onChangeDateTo(ValueChangeEvent evt){
      this.dtDateTo= (java.util.Date)evt.getNewValue();
      this.dtDateFrom = (java.util.Date)getDateFrom();//This is the getter method for date from field
      System.out.println("onChangeDateTo "+ this.dtDateFrom+":"+this.dtDateTo);
      if (evt.getNewValue() != null){
      int intCalcNoOfDays = DateHelper.getBusinessDaysCount(this.getDateFrom(),this.getDateTo()) + 1;// my custom class which does the calculation
      String strCalcNoOfDays = String.valueOf(intCalcNoOfDays);
      System.out.println("strCalcNoOfDays "+strCalcNoOfDays);
      this.setNoOfDays(strCalcNoOfDays);
      }

      I am getting NullPointerException while getting the value for date from (this.dtDateFrom). I have tried other approaches like attaching a binding to date from field, passing attributes etc but no luck. Can someone assist me in achieving this? Basically I need to populate one field automatically based on values entered on two other fields whenever tab event/user selects value on second field AND it shouldn't fire any other validations on the form (basically partial trigger).

      Thanks very much for the help in advance.

      Thanks
      Shree
        • 1. Re: Question on populating a field based on 2 other fields
          Shay Shmeltzer-Oracle
          Is your backend based on ADF BC? Which JDeveloper version is this?
          • 2. Re: Question on populating a field based on 2 other fields
            Shay Shmeltzer-Oracle
            See this "no code" approach to getting it to work:
            https://blogs.oracle.com/shay/entry/simpler_partial_page_refresh_ppr
            • 3. Re: Question on populating a field based on 2 other fields
              LillyD
              Hi,

              Can you please share your JSFF code snippet.

              The solution is simple:
              1. Bind the fromValue attribute correctly to the backing bean.
              2. Have an autosubmit=true on it
              3. In the value change listener you should get the value of fromDate from binding and toDate from newValue.
              4. Perform your calculations and set this to the third value
              5. Programmatically refresh the page using this code snippet AdfFacesContext.getCurrentInstance().addPartialTarget(<<the third value attribute>>);

              Once I have a look at your JSFF, would be able to be in a better position to comment.
              Regards,
              Lilly
              • 4. Re: Question on populating a field based on 2 other fields
                shreevat
                I am using JDeveloper 11g. I am not using ADF BC

                Thanks
                Shree
                • 5. Re: Question on populating a field based on 2 other fields
                  shreevat
                  Thanks Lilly.

                  I don't have a backing bean associated with the page. I have managed bean instead. JSF source code snippet below

                  <?xml version='1.0' encoding='UTF-8'?>
                  <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
                  xmlns:af="http://xmlns.oracle.com/adf/faces/rich" xmlns:h="http://java.sun.com/jsf/html">
                  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
                  <f:view>
                  <af:document title="LMS" id="d1">
                  <af:messages id="m1"/>
                  <af:form id="f1">
                  <af:panelStretchLayout id="psl1" startWidth="160px">
                  <f:facet name="top">
                  <af:panelGroupLayout id="pgl1" layout="vertical" halign="center">
                  <af:panelFormLayout id="pfl1" rows="1" maxColumns="1">
                  <af:outputText value="LMS" id="ot1"
                  inlineStyle="color:Blue; font-family:'Times New Roman', 'Arial Black', times, Serif; font-size:175%; font-weight:bold; text-align:center;"/>
                  </af:panelFormLayout>
                  </af:panelGroupLayout>

                  </f:facet>
                  <f:facet name="bottom">
                  <af:panelGroupLayout id="pgl2"/>
                  </f:facet>
                  <f:facet name="center">
                  <af:panelGroupLayout layout="scroll" id="pgl4">
                  <af:outputText value="Dashboard" id="ot6"
                  inlineStyle="color:Green; font-size:150%; font-family:'Times New Roman', 'Arial Black', times, Serif; font-weight:bold; text-align:center;"
                  visible='#{clickActionHandlerBean.clickLink== "HOME"}'/>
                  <af:spacer width="10" height="10" id="s1"
                  visible='#{clickActionHandlerBean.clickLink== "HOME"}'/>
                  <af:spacer width="10" height="25" id="s5"
                  visible='#{clickActionHandlerBean.clickLink=="APPLY"}'/>
                  <af:panelFormLayout id="pfl2" rows="1" maxColumns="1"
                  visible='#{clickActionHandlerBean.clickLink=="APPLY"}'>
                  <af:selectOneChoice label="Type" id="soc1" showRequired="true"
                  immediate="true" value="#{bean1.type}">
                  <af:selectItem label="X1" value="X1" id="si1"/>
                  <af:selectItem label="X2" value="X2 id="si2"/>
                  <af:selectItem label="X3" value="X3" id="si3"/>
                  <af:selectItem label="X4 value="X4" id="si4"/>
                  </af:selectOneChoice>
                  </af:panelFormLayout>
                  <af:panelFormLayout id="pfl3" rows="1" maxColumns="3"
                  visible='#{clickActionHandlerBean.clickLink=="APPLY"}'>
                  <af:inputDate label=" Date From" id="id1"
                  rendered='true'
                  showRequired="true" inlineStyle="vertical-align:baseline;"
                  value="#{bean1.dateFrom}">
                  <af:convertDateTime pattern="dd-MMM-yyyy" secondaryPattern="MM/dd/yyyy"/>
                  </af:inputDate>
                  <af:inputDate label="Date To" id="id2"
                  rendered='true'
                  showRequired="true" value="#{bean1.dateTo}" partialTriggers="it1"
                  valueChangeListener="#{bean1.onChangeDateTo}"
                  autoSubmit="true">
                  <af:convertDateTime pattern="dd-MMM-yyyy" secondaryPattern="MM/dd/yyyy"/>
                  </af:inputDate>
                  <af:inputText label="No of Days" id="it1" columns="7" showRequired="true"
                  rendered="true" value="#{bean1.noOfDays}" maximumLength="2">
                  <af:validateRegExp pattern="[0-9]{1,10}"
                  messageDetailNoMatch="No of days can have only numbers"/>
                  </af:inputText>
                  </af:panelFormLayout>
                  <af:spacer width="10" height="50" id="s6"/>
                  <af:group id="g1">
                  <af:spacer width="250" height="10" id="s7"
                  visible='#{clickActionHandlerBean.clickLink=="APPLY"}'/>
                  <af:commandButton text="Save" id="cb1" partialSubmit="true" blocking="false"
                  immediate="false"
                  visible='#{clickActionHandlerBean.clickLink=="APPLY"}'
                  actionListener="#{bean1.ApplyButtonClicked}">
                  </af:commandButton>
                  <af:spacer width="20" height="10" id="s8"/>
                  <af:commandButton text="Cancel" id="cb2"
                  visible='#{clickActionHandlerBean.clickLink=="APPLY"}'/>
                  </af:group>
                  </af:panelGroupLayout>
                  </f:facet>
                  <f:facet name="start">
                  <af:panelGroupLayout id="pgl3" halign="left">
                  <p>
                  <af:outputText value="#{userinfo.userInfo.firstName}" id="otWel"
                  inlineStyle="color:Maroon;" rendered="false"/>
                  </p>
                  <af:spacer width="1" height="50" id="s2"/>
                  <p>
                  <af:commandLink text="Home" id="glHome" action="#{clickActionHandlerBean.showHomeRN}"/>
                  </p>
                  <p>
                  <af:commandLink text="Apply" id="glApply"
                  action="#{clickActionHandlerBean.showApplyRN}"/>
                  </p>
                  </af:panelGroupLayout>
                  </f:facet>
                  </af:panelStretchLayout>
                  </af:form>
                  </af:document>
                  </f:view>
                  </jsp:root>


                  Thanks
                  Shree
                  • 6. Re: Question on populating a field based on 2 other fields
                    shreevat
                    Thanks Shay/ In my case, there is no ADF BC. Consider an example of applying leave wherein the date is not derived from any datasource or VO. User selects the fields manually (from date and to date) and we need to calculate the no of days automatically.

                    Thanks
                    Shree
                    • 7. Re: Question on populating a field based on 2 other fields
                      Shay Shmeltzer-Oracle
                      What is the scope of the bean that has your data/method?
                      • 8. Re: Question on populating a field based on 2 other fields
                        shreevat
                        bean has request scope

                        Thanks
                        Shree