Solved

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

Posted on 2008-10-14
3
872 Views
Last Modified: 2008-11-05
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
Comment
Question by:arkhls
  • 2
3 Comments
 
LVL 17

Expert Comment

by:sweetfa2
Comment Utility
  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
 

Author Comment

by:arkhls
Comment Utility
Tnx for respond , I did  that already unfortunatelt it's not working
0
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 250 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ShiftLeft challenge 21 65
post4 challenge 28 81
Return ZipOutputStream using Servlet 2 32
count11 challenge 6 47
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now