7

I am having a peculiar predicament in regards to using apex:actionSupport.

I am selecting a collection of values on a VisualForce page (through the use of a wrapper class in the controller) with the use of an inputCheckbox tag on the page.

The only problem is, the onclick event that the actionSupport tag is linked to, only fires after one of my output panels rerenders the list. Otherwise, it does not work.

Here is my code:

In Visualforce

<apex:commandButton action="{!Add_To_Friendship_List}" reRender="List" value="Add New Friend to List!"/>

<apex:outputPanel id="List">
          <apex:outputPanel id="Listable">
              <apex:dataTable value="{!Entries}" var="Friend">
                  <apex:column >   
                     {!Friend.Display_Name} 
                     <apex:inputCheckbox value="{!Friend.Is_Chosen}" selected="{!Friend.Is_Chosen}">
                         <apex:actionSupport event="onclick" action="{!Recount}" reRender="CounterPanel"/>
                     </apex:inputCheckbox>
                 </apex:column>
              </apex:dataTable>
          </apex:outputPanel>
</apex:outputPanel>
......
<apex:outputPanel id="CounterPanel">
    <apex:outputPanel >
         {!Current_Friend_Count}
    </apex:outputPanel>
</apex:outputPanel>

Things I have also tried

I have also tried using an actionFunction tag and linking the checkboxes onclick event to that (as well as on select) but it still wont fire until the "List" output panel is updated.

I have also removed the selected attribute in the inputCheckbox tag to no avail as well.

Any suggestion on why that is and how to get it to work properly the first time is most appreciated.

Added Content

*In Apex - Friendship_Wizard controller*

private Integer Selection_Count;
private List<Possible_Friend_Wrap> Current_Friends;
public List<Possible_Friend_Wrap> Current { get { return this.Current_Friends; } }
private List<Possible_Friend_Wrap> New_Friendship_Entries;
public List<Possible_Friend_Wrap> Entries { get { return this.New_Friendship_Entries; } }

public class Possible_Friend_Wrap
{
    public Friend__c One_Friend { get; private set; }
    public String Display_Name { get; private set; }
    public Boolean Is_Chosen { get; set; }
    protected Boolean In_DB { get; set; }

    public Possible_Friend_Wrap(Friend__c Friend, Boolean Is_Created)
    {
        this.One_Friend = Friend;
        this.In_DB = Is_Created;
        this.Display_Name = this.One_Friend.First_Name__c;
        if(this.One_Friend.Middle_Name__c != null)
            this.Display_Name += ' ' + this.One_Friend.Middle_Name__c;
        this.Display_Name += ' ' + this.One_Friend.Last_Name__c;
        this.Is_Chosen = false;
    }
}

public PageReference Edit_Friendship() 
{
    this.New_Friendship_Entries = new List<Possible_Friend_Wrap>();
    this.Current_Friends = new List<Possible_Friend_Wrap>();
    this.Friend_Addtion = new Friend__c();
    this.Selection_Count = 0;

    for(Friend__c Found_Friend : [SELECT Middle_Name__c, Last_Name__c, Id, First_Name__c FROM Friend__c])
        this.Current_Friends.add(new Possible_Friend_Wrap(Found_Friend, true));

    PageReference Friendship_Editor = Page.FriendsPage;
    return Friendship_Editor;
}

public PageReference Add_To_Friendship_List()
{
    if(this.New_Friendship_Entries == null)
        this.New_Friendship_Entries = new List<Possible_Friend_Wrap>();
    this.New_Friendship_Entries.add(new Possible_Friend_Wrap
                                    (this.Friend_Addtion.clone(true, true), false));
    this.Friend_Addtion = new Friend__c();

    return null;
}

public Integer Current_Friend_Count
{
    get { return this.Selection_Count; }
}

public PageReference Recount()
{
    this.Selection_Count = 0;

    if(this.New_Friendship_Entries != null)
    {
        for(Possible_Friend_Wrap New_Wrap : this.New_Friendship_Entries)
            if(New_Wrap.Is_Chosen == true)
                this.Selection_Count++;
    }
    this.Checker = 'Counted new entries selected.';

    if(this.Current_Friends != null)
    {
        for(Possible_Friend_Wrap Old_Wrap : this.Current_Friends)
            if(Old_Wrap.Is_Chosen == true)
                this.Selection_Count++;
    }

    return null;
}
  • Could you post your extension code as well? If I find some time later (in 3+ hours), i'll try and reproduce your problem, and find a fix, if you haven't found one in the mean time. – pjcarly Mar 20 '13 at 17:42
  • 1
    Try adding <apex:actionRegion> around your <apex:column> – logontokartik Mar 20 '13 at 19:42
  • how did you verify its not firing? – Phil B Mar 20 '13 at 20:04
  • I verified it by looking at the view state. It will only update/increment the count after the rerender of the List outputPanel. I also added a string inside that would only be initialized when the controller method was fired. – Programmable Medley Mar 20 '13 at 20:10
  • If you check under the network tab in the web inspector tools in Chrome do you see any type of ajax event? – Phil B Mar 20 '13 at 20:15
1

logontokartik's suggestion worked, giving me the following:

<apex:form >
  <apex:pageBlock title="Choose {!Possesive_Dispaly} Friends">
      <apex:pageBlockSection title="Enter another friend if not in list" columns="1">
          <apex:inputField value="{!Add_Friend.First_Name__c}"/>
          <apex:inputField value="{!Add_Friend.Middle_Name__c}"/>
          <apex:inputField value="{!Add_Friend.Last_Name__c}"/>
          <apex:commandButton action="{!Add_To_Friendship_List}" reRender="List" value="Add New Friend to List!"/>
      </apex:pageBlockSection>
          <apex:outputPanel id="List">
              <apex:outputPanel id="Listable">
                  <apex:dataTable value="{!Entries}" var="Friend">
                      <apex:column >
                          <apex:actionRegion >   
                             {!Friend.Display_Name} 
                             <apex:inputCheckbox value="{!Friend.Is_Chosen}">
                                 <apex:actionSupport event="onclick" action="{!Recount}" reRender="CounterPanel"/>
                             </apex:inputCheckbox> 
                          </apex:actionRegion>
                     </apex:column>
                  </apex:dataTable>
                  <apex:dataTable value="{!Current}" var="Friend">
                      <apex:column >   
                         <apex:actionRegion >
                         {!Friend.Display_Name} 
                         <apex:inputCheckbox value="{!Friend.Is_Chosen}">
                             <apex:actionSupport event="onclick" action="{!Recount}" reRender="CounterPanel"/>
                         </apex:inputCheckbox>
                         </apex:actionRegion>
                     </apex:column>
                  </apex:dataTable>
          </apex:outputPanel>
      </apex:outputPanel>
      <apex:pageBlockSection >
          <apex:outputPanel id="CounterPanel">
              <apex:outputPanel >
                  {!Current_Friend_Count}
              </apex:outputPanel>
          </apex:outputPanel>
      </apex:pageBlockSection>
  </apex:pageBlock>

Thanks again for everyone's help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.