3

Visual force page:

In visualforce page I have a javascript for loop which calls a action function according to elements size

    <apex:form >
            <apex:actionFunction name="myActionFunction" action="{!actionFunctionMethod}"
                rerender="source,srcTable" oncomplete="">
            </apex:actionFunction>
    </apex:form>
<script>
       function addToList {
        for(var i=0; 1<ele.length; i++) {
            myActionFunction(); // This calls a method in contoller
        }
}
</script>

In controller I have a list initialised by its constructor and I add values by actionFunction as:

public with sharing class Details
{
  List<WrapperClass> detailsList {get; set;}

  public Details()
  {
     detailsList = new List<WrapperClass>();
  }

  public void actionFunctionMethod()
  {
    detailsList.add(new WrapperClass());
    System.debug('======  '+ detailsList.size()); // In system debug it is always 1
  }

  public class WrapperClass {

     public WrapperClass()
     {
     }
  }
}

Lets suppose the for loop execute myActionFunction 5 times. So, method actionFunctionMethod called 5 times and List must have size of 5. But it is always 1.

The Expected value for List should be 5 but actual value is 1. Context is same, page didn't reload but how this happening? and what should be work around it controller don't maintain values like this way?

I think some how list get initialised otherwise it will through null pointer exception. I have checked my code there is no initialisation except constructor.

1

Try to submit the array size ele.length to the apex method and not calling an apex method in the javascript loop:

Apex page:

<apex:form >
    <apex:actionFunction name="myActionFunction" 
                         action="{!actionFunctionMethod}"
                         reRender="source,srcTable" 
                         oncomplete="">
        <apex:param name="p1" assignTo="{!arraySize}" value="" />
    </apex:actionFunction>
</apex:form>

<script>
function addToList() {
    myActionFunction(ele.length); 
}
</script>

Controller:

public Integer arraySize { get; set; }
public void actionFunctionMethod()
{
    for(Integer i = 0; i < arraySize; i++)
    {
        detailsList.add(new WrapperClass());
    }
}

==================== Update ====================
Nevertheless, if you want to call the actionFunction in the loop try to insert a small delay between exections:

<script>
var lastIndex = 0;
var myVar;

function addToList() {
    myVar= setInterval(function(){ 
               executeMe(); 
           },500);
}

function executeMe(){
    if(lastIndex == ele.length){
        clearInterval(myVar);
    }
    else{
        myActionFunction();
        lastIndex++;
    }
}
</script>

<apex:actionFunction name="myActionFunction" 
                     action="{!actionFunctionMethod}"
                     reRender="source" 
                     oncomplete=""
                     timeout="500">
</apex:actionFunction>

public void actionFunctionMethod(){
    detailsList.add(new Account(Name='ttt'));
}
  • This is not the answer. What will happen if I am doing detailsList.add(someValueWhichChangeOnEachCall); If you can see actiionFunction i am rerendering "srcTable", it get render on each call and display value. I cannot pass value from page to controller in actionFunction. Value in list decided by myActionFunctionMethod. And key question is why this happening? – Ashwani Sep 9 '13 at 13:01
  • This happens because the actionFunction needs some time to execute the apex method. You need to wait in your javascript loop till each execution is completed and then execute the actionFunction again. Updated my answer. – Sergej Utko Sep 9 '13 at 13:42
  • Thanks for answer. I tried this approach, it doesn't work. Also I want know what is your perception about the approach I used in code in question, means what will be the expected result using this? I think actionFunction timing is not the reason. @mast0r – Ashwani Sep 9 '13 at 15:21

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.