• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

JSP for loop issue during creation of JavaScript Object in JSP page

I created  an Object list based on below class and send that over to JSP page  
List<list> account = ArrayList<String> ()         <----- list of account

List<?> myList = new Client( clientId , account  )     <-----   Object that will be sent to JSP


class Client {
    String clientId;
    List<String> accountList;

}

Below is the list content that I iterate before sending to JSP page  to make sure the data in a list are correct:

                  clinetID        account          
Index[0] = 15E5Y8------15E5Y8A
Index[1] = 15E6L8------No Account.
Index[2] = 15E882------15E882A
Index[3] = 15E8L8------15E8L8A
Index[4] = 15E8L8------15E8L8B
Index[5] = 15E8R6------No Account.
Index[6] = 15E9M9------15E9M9A

in order to create the Javascript Object I have made below Script

<script type="text/javascript">
      var clientObjectList = new Array();        
      function Client(clientId,accountId) {
                this.clientId=clientId;
                this.accountId = accountId;
      }  

<c:forEach var="view" items="${sessionScope.keyValueResult.key }"  varStatus="status">      
  <c:set var="listAccount" value="${view.accountList}" />
  <c:forEach var="viewAccount" items="${listAccount}" varStatus="listStatus">
   clientObjectList[${status.count - 2 + listStatus.count}] = new Client("${view.clientId}","${viewAccount}");
</c:forEach>
</c:forEach>  
</scrip>


below is my JavaScript Object list after running the JSP page

      clientObjectList[0] = new client("15E5Y8" , "15E5Y8A");

      clientObjectList[1] = new client("15E6L8" , "No Account in .")

      clientObjectList[2] = new client("15E882" , "15E882A");

      clientObjectList[3] = new client("15E8L8" , "15E8L8A");

      clientObjectList[4] = new client("15E8L8" , "15E8L8B");       

       the next one should be 5 but it's 4
      clientObjectList[4] = new client("15E8R6" , "No Account .");      

      clientObjectList[5] = new client("15E9M9","15E9M9B");            

Please look at the next index after  clientObjectList[4]   --> Must be 5 but it's getting 4 also

Problem :  it seems  when the list has a size more than 1  the index or var="status.count" is  not increased  when iteration is done in nested FOR <c:foreach var="viewAccount")

I did couple different way of programming syntax but the result were the same, something ike below  

<c:set var="index" value="0" />
<c:forEach var="view" items="${sessionScope.keyValueResult.key }"  varStatus="status">      
<c:forEach var="viewAccount" items="${listAccount}" varStatus="listStatus">            
clientObjectList[${index}] = new Client("${view.clientId}","${viewAccount}");
<c:set var="index" value="${index + 1}" />
</c:forEach>
</c:forEach>  

Does anybody have any Ideas ? Thanks
0
arkhls
Asked:
arkhls
  • 2
1 Solution
 
sweetfa2Commented:
  clientObjectList[${status.count - 2 + listStatus.count}] = new Client("${view.clientId}","${viewAccount}");

maybe should be:

   clientObjectList[${status.count} - 2 + ${listStatus.count}] = new Client("${view.clientId}","${viewAccount}");
0
 
arkhlsAuthor Commented:
Tnx for respond , I did  that already unfortunatelt it's not working
0
 
sweetfa2Commented:
The problem is in the way you are calculating the index.  So why are you subtracting to from status.count.

Presumably as status.count is the outer loop the actual number of entries will be different for each iteration.  May I suggest a simple incrementing count value every time you add or maybe use clientObjectList.length as the index instead.

It may be clientObjectList.size() but I can't remember at the moment.

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now