+ Start a Discussion
lovetolearnlovetolearn 

Help with reRendering

Hi, 

 

I am trying to display different outputText depending on the value selected in a picklist. I also want my picklist to load the last value that the user entered. If I use the default getter and setter method, I can't display the last value entered by the user. But if I write my own getter and setter, the outputText does not refresh. I think I am writing the setter method wrong. Here is my code: 

<apex:page controller="testEvents">
    <apex:form id="eventsForm">
        <apex:pageBlock id="eventPageBlock">
            <apex:pageBlockSection id="eventPageBlockSection">
                <apex:selectList label="Events:" id="status" size="1" multiselect="false" value="{!events}">
                    <apex:selectOption itemLabel="" itemValue=""/>
                    <apex:selectOption itemLabel="Morning Only" itemValue="Morning Only"/>
                    <apex:selectOption itemLabel="Evening Only" itemValue="Evening Only"/>
                    <apex:selectOption itemLabel="All Day" itemValue="All Day"/>
                    <apex:actionSupport event="onchange" reRender="Display" status="testStatus"/>
                </apex:selectList><br/>
                <apex:commandButton value="GOGOGOGO!!" action="{!createEventObj}"/>
            </apex:pageBlockSection><br/><br/>
            <apex:outputPanel id="Display">
                <apex:outputText value="This is what displays when you enter All Day Event" rendered="{!IF(events = 'All Day', true, false)}"/>
                <apex:outputText value="This is what displays when you enter anything else but All Day Events" rendered="{!IF(events != 'All Day', true, false)}"/>
            </apex:outputPanel><br/>
            <apex:actionStatus startText="Rerendering..." stopText="(All Set!)" id="testStatus"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

public class testEvents{
    public string events;
    LIST<Event_Obj__c> eventlist  = [SELECT ID, Title__c, Type_of_Event__c FROM Event_Obj__c WHERE ID = 'a0qR0000000U10w'];
    
    public void setevents(String events){
        this.events = events;
    }
    
     public String getevents(){
          return eventList[0].Type_of_Event__c;
     }
    
    public void createEventObj(){
        if(eventlist.size()==0){
            Event_Obj__c eobj = new Event_Obj__c (Number__c = '500R000000354zt', Type_of_Event__c = events);
            insert eobj;
        }else if(eventlist.size() >= 1){
            Event_Obj__c updateobj = [SELECT Type_of_Event__c FROM Event_Obj__c WHERE Number__c = '500R000000354zt' LIMIT 1];
            updateobj.Type_of_Event__c = events;
            update updateobj;
        }
    }
}

 Please help. Thank you. 

bob_buzzardbob_buzzard

I think what is happening here is that eventlist isn't ever changed.  Thus even though you insert a record, the list contains the original view from when the controller was constructed, i.e. that there are no records.

 

Try requerying at the end of your action method:

 

    public void createEventObj(){
        if(eventlist.size()==0){
            Event_Obj__c eobj = new Event_Obj__c (Number__c = '500R000000354zt', Type_of_Event__c = events);
            insert eobj;
        }else if(eventlist.size() >= 1){
            Event_Obj__c updateobj = [SELECT Type_of_Event__c FROM Event_Obj__c WHERE Number__c = '500R000000354zt' LIMIT 1];
            updateobj.Type_of_Event__c = events;
            update updateobj;
        }

        eventlist  = [SELECT ID, Title__c, Type_of_Event__c FROM Event_Obj__c WHERE ID = 'a0qR0000000U10w'];
    }

 

 

asish1989asish1989

Hi Love

   

     I did not understand what is your exaxt requirement.still I have posted some code which can help ful to full fill your requirement....

 I have posted code which helps you

  1- to display user entered value in a picklist

  2-to display Apex output text based on picklist value

  3-how to referesh outputpanel

  is this your requiirement......?

So before going to my code please make a custom object DynamicPicklist__c.....and also a field Picklistvalue__c

  this is my page code..

 

   <apex:page controller="dynamicpicklist" sidebar="false" >
<apex:form >
<apex:sectionHeader title="Dynamic Picklist" subtitle="Reusable code"/>
<apex:pageblock >
<apex:pageBlockSection title="Dynamic picklist" columns="1">
    
      <apex:pageblocksectionItem >
          <apex:outputlabel value="City" for="values" />
          <apex:selectList value="{!city}" size="1" id="values">
              <apex:actionSupport event="onchange" reRender="newvalue" />
              <apex:selectOptions value="{!citynames}"/>
          </apex:selectList>
      </apex:pageblocksectionItem>                                        
          <apex:outputpanel id="newvalue">
             <!--<apex:outputpanel rendered="{!city == '--Other--'}">-->
             <div style="position:relative;left:75px;">             
                  <apex:outputlabel value="New value" for="newval" />
                  <apex:inputText value="{!newpicklistvalue}" id="newval"/>
                  <apex:outputText value="This is what displays when you enter All Day Event" rendered="{!IF(city = 'All Day', true, false)}"/>
                  <apex:outputText value="This is what displays when you enter anything else but All Day Events" rendered="{!IF(city != 'All Day', true, false)}"/>
                  <apex:commandbutton action="{!saverec}" value="Add!"/>
             </div>
             <!--</apex:outputpanel>-->
          </apex:outputpanel>             
  </apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>

 

my controller is.............

    public class dynamicpicklist
{
public String city{get; set;}
public DynamicPicklist__c dp{get; set;}
public List<SelectOption> getcitynames()
{
  List<SelectOption> options = new List<SelectOption>();
  List<DynamicPicklist__c> citylist = new List<DynamicPicklist__c>();
  citylist = [Select Id, PicklistValue__c FROM DynamicPicklist__c ];
  //options.add(new SelectOption('--None--','--None--'));
 
  for (Integer j=0;j<citylist.size();j++)
  {
      options.add(new SelectOption(citylist[j].PicklistValue__c,citylist[j].PicklistValue__c));
  }
  return options;
}
public String newpicklistvalue{get; set;}

public void saverec()
{
  DynamicPicklist__c newrec = new DynamicPicklist__c(PicklistValue__c=newpicklistvalue);
  insert newrec;
  newpicklistvalue=NULL;
}

}

 

Did this post solve your problem..if so please mark it solved for reference otherwise let me know about your problem..

 

Thanks

asish

lovetolearnlovetolearn

bob_buzzard wrote:

I think what is happening here is that eventlist isn't ever changed.  Thus even though you insert a record, the list contains the original view from when the controller was constructed, i.e. that there are no records.

 

Try requerying at the end of your action method:

 

    public void createEventObj(){
        if(eventlist.size()==0){
            Event_Obj__c eobj = new Event_Obj__c (Number__c = '500R000000354zt', Type_of_Event__c = events);
            insert eobj;
        }else if(eventlist.size() >= 1){
            Event_Obj__c updateobj = [SELECT Type_of_Event__c FROM Event_Obj__c WHERE Number__c = '500R000000354zt' LIMIT 1];
            updateobj.Type_of_Event__c = events;
            update updateobj;
        }

        eventlist  = [SELECT ID, Title__c, Type_of_Event__c FROM Event_Obj__c WHERE ID = 'a0qR0000000U10w'];
    }

 

 


Yes, I see what you mean. How do I fix this?

 

Thanks for your help. 

lovetolearnlovetolearn

asish1989 wrote:

Hi Love

   

     I did not understand what is your exaxt requirement.still I have posted some code which can help ful to full fill your requirement....

 I have posted code which helps you

  1- to display user entered value in a picklist

  2-to display Apex output text based on picklist value

  3-how to referesh outputpanel

  is this your requiirement......?

So before going to my code please make a custom object DynamicPicklist__c.....and also a field Picklistvalue__c

  this is my page code..

 

   <apex:page controller="dynamicpicklist" sidebar="false" >
<apex:form >
<apex:sectionHeader title="Dynamic Picklist" subtitle="Reusable code"/>
<apex:pageblock >
<apex:pageBlockSection title="Dynamic picklist" columns="1">
    
      <apex:pageblocksectionItem >
          <apex:outputlabel value="City" for="values" />
          <apex:selectList value="{!city}" size="1" id="values">
              <apex:actionSupport event="onchange" reRender="newvalue" />
              <apex:selectOptions value="{!citynames}"/>
          </apex:selectList>
      </apex:pageblocksectionItem>                                        
          <apex:outputpanel id="newvalue">
             <!--<apex:outputpanel rendered="{!city == '--Other--'}">-->
             <div style="position:relative;left:75px;">             
                  <apex:outputlabel value="New value" for="newval" />
                  <apex:inputText value="{!newpicklistvalue}" id="newval"/>
                  <apex:outputText value="This is what displays when you enter All Day Event" rendered="{!IF(city = 'All Day', true, false)}"/>
                  <apex:outputText value="This is what displays when you enter anything else but All Day Events" rendered="{!IF(city != 'All Day', true, false)}"/>
                  <apex:commandbutton action="{!saverec}" value="Add!"/>
             </div>
             <!--</apex:outputpanel>-->
          </apex:outputpanel>             
  </apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>

 

my controller is.............

    public class dynamicpicklist
{
public String city{get; set;}
public DynamicPicklist__c dp{get; set;}
public List<SelectOption> getcitynames()
{
  List<SelectOption> options = new List<SelectOption>();
  List<DynamicPicklist__c> citylist = new List<DynamicPicklist__c>();
  citylist = [Select Id, PicklistValue__c FROM DynamicPicklist__c ];
  //options.add(new SelectOption('--None--','--None--'));
 
  for (Integer j=0;j<citylist.size();j++)
  {
      options.add(new SelectOption(citylist[j].PicklistValue__c,citylist[j].PicklistValue__c));
  }
  return options;
}
public String newpicklistvalue{get; set;}

public void saverec()
{
  DynamicPicklist__c newrec = new DynamicPicklist__c(PicklistValue__c=newpicklistvalue);
  insert newrec;
  newpicklistvalue=NULL;
}

}

 

Did this post solve your problem..if so please mark it solved for reference otherwise let me know about your problem..

 

Thanks

asish


Thanks for your help. The code doesn't seem to work, the City picklist did not get populated with values. 

asish1989asish1989

Hi Love

  Its working I have checked.you have to enter some value In DynamicPicklist__c object manually. Then run the page .city is populated. I am sure It will work .you check it and let me know error

would you like to share why do you think this code will not work...?

 

 

 

Thanks

asish

bob_buzzardbob_buzzard

You should be able to just add the query line I put at the end of the method.

lovetolearnlovetolearn

asish1989 wrote:

Hi Love

  Its working I have checked.you have to enter some value In DynamicPicklist__c object manually. Then run the page .city is populated. I am sure It will work .you check it and let me know error

would you like to share why do you think this code will not work...?

 

 

 

Thanks

asish


Ah okay cool, now I understand. That worked. I was able to piggyback off this example. I changed it quite a bit, but this example helped.