2

I have an apex:actionFunction in my VF page which is called from a commandbutton inside an apex:repeat. There is an apex:actionStatus component which should be rendered once apex:actionFunction is called.

Since the button and action status are inside repeat I am unable to reference actionStatus component just by the id string. What I did instead is to pass the complete Id of actionStatus component to the actionFunction which is then passed to the controller. I am trying to reference the controller variable in status field in actionFunction but for some reason it is not working.

I hardcored the same complete Id string to the status and it worked. Please help me understand what I am missing here.

<apex:form >
    <apex:actionFunction name="adduser" action="{!testfunction}" rerender="ResultPanel" status="{!currentComponentIdValue}">
        <apex:param name="currentComponentId" value=""/>
    </apex:actionFunction>
</apex:form>

//Some logic here

<apex:form >
 <apex:outputPanel id="ResultPanel" >
 <apex:repeat value="{resultList}" var="res">
 <apex:outputText value="res.Name" />
<apex:commandButton onclick="adduser('{!$Component.mystatus}')" value="Add User"/>
<apex:actionStatus id="mystatus" >
   <apex:facet name="start">Requesting...</apex:facet>
   <apex:facet name="stop"></apex:facet>
</apex:actionStatus>
</apex:repeat>
</apex:outputPanel>
</apex:form>

EDIT:
The main issue here is that status="{!currentComponentIdValue}" in actionFunction is not working. If I hardcode Status with the actual id for a single commandbutton, it works perfectly. But for some reason initializing status from controller is not working.

  • I think you have to put the command button after the status. While you are trying to access the actionStatus component in the commandButton the status component is not initialized, because it is in the code after the button. So try to swap these components. – Sergej Utko Mar 9 '15 at 8:08
  • That didn't work. The main issue here is that status="{!currentComponentIdValue}" in actionFunction is not working. If I hardcode Status with the actual id for a single commandbutton, it works perfectly. But for some reason initializing status from controller is not working. – Aneesha Mar 9 '15 at 8:18
1

If you just want to show the status of the action progress in your case I would try avoid the actionStatus in repeat block and use a normal and poor HTML and a little javascript for that.

<style>
    /* Just a style for the status element */
    .statusCustomStyle {
        background: url(/img/loading24.gif) no-repeat 0 0;
        padding-left: 30px;
        height:24px;
        display:none;
    }
</style>

<script>    
    // The variable to hold the current status ID
    var currentStatusId;

    function statusHandler(show){
        if(show)
            document.getElementById(currentStatusId).style.display = 'inlineBlock';
        else
            document.getElementById(currentStatusId).style.display = 'none';
    }
</script>

<apex:form >
    <!-- If the action is complete the status element should be hidden again -->
    <apex:actionFunction name="adduser" action="{!testfunction}" rerender="ResultPanel" oncomplete="statusHandler(false);"/>
</apex:form>

<apex:form >
    <apex:outputPanel id="ResultPanel" >
        <apex:repeat value="{resultList}" var="res">
            <apex:outputText value="res.Name" />
            <!-- First definind the current javascript statusID variable -->
            <!-- Then showing the status -->
            <!-- Then calling the action function -->
            <apex:commandButton onclick="currentStatusId = 'status-{!res.Id}'; statusHandler(true); adduser(); return false;" value="Add User"/>
            <!-- The ID should be somehow unique. For that I will use the ID of the repeated element -->
            <div id="status-{!res.Id}" class="statusCustomStyle">Requesting...</div>
        </apex:repeat>
    </apex:outputPanel>
</apex:form>
  • Vanilla JS is still so much better than Visualforce for simple tricks, and this is an example of that. – sfdcfox Mar 9 '15 at 8:59
  • @sfdcfox Right! I just wanted to keep it native :) – Sergej Utko Mar 9 '15 at 9:02
0

You need to put the status outside repeat, yes you will need to hardcode the status value in action function in order to call the action status

//Some logic here

    <apex:form >
     <apex:outputPanel id="ResultPanel" >
     <apex:repeat value="{resultList}" var="res">
     <apex:outputText value="res.Name" />
    <apex:commandButton onclick="adduser('{!$Component.mystatus}')" value="Add User"/>

    </apex:repeat>
    </apex:outputPanel>
     <apex:actionFunction name="adduser" action="{!testfunction}" rerender="ResultPanel" status="mystatus">
            <apex:param name="currentComponentId" value=""/>
        </apex:actionFunction>
    <apex:actionStatus id="mystatus" >
       <apex:facet name="start">Requesting...</apex:facet>
       <apex:facet name="stop"></apex:facet>
    </apex:actionStatus>

    </apex:form>
  • Thanks for your response Sagar. Isn't there any way at all where I can put actionStatus component inside the repeat? What I am trying to achieve is to show a "Requesting " text when user clicks on the button. There could be upto 20 buttons on the same page. If I put the actionStatus outside, the text will only show at a particular position which is easy for user to miss if he is scrolling down the page. – Aneesha Mar 9 '15 at 8:09
  • Try replacing <apex:actionStatus id="mystatus" > with this <apex:actionStatus id="{!currentComponentIdValue}" > in your very first posted code. – Sagar Pareek Mar 9 '15 at 8:25
  • Bad practice to put the actionFunction in repeat block. It is like to put the javascript in the repeat. Better to avoid it. – Sergej Utko Mar 9 '15 at 8:28
  • The basic idea is to keep the action status id same with the each call let say your button is b1 then your action function should be set to call the action status (<apex:actionFunction name="adduser" action="{!testfunction}" rerender="ResultPanel" status="s1">)s1 and your action status (<apex:actionStatus id="s1" >) must be id must be s1. So for button b2 the status is s2. – Sagar Pareek Mar 9 '15 at 8:29
  • I understand what you are saying here. But isn't there a way to dynamically do this. It seems a bit strange to me that the status is not taking any value fro controller. – Aneesha Mar 9 '15 at 8:51

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.