How can I use "$.each" inside "$.each" ?

How can I use "$.each" inside "$.each" ?

Hello, 

I am working on a one HTML page application that uses a REST web site to provide data and JSON to move data back and forth.
My experience with JQuery and JSON is quite limited.  
I have a question on how to use $.each inside $.each to produce a nice display.

I would like to post my HTML page below with comments, the page displays a set of buttons, 
I use jQuery to fill the buttons with a text and attach an OnClick handler, 
on click I call the REST service to receive an array of objects.

For each objects, I create an <LI> with the obj.Name property and an indented box underneath. 
What I would like to do, is to create a new <UL> in this box and display each members of the current object with the $.each function 
but at this point I am already inside a $.each so I can't find a way to iterate inside the iteration and produce the output.

Could you please help me find the right syntax to do that ?

Thanks a lot,
Claude

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.         <title>Inventory</title>
  5.         <style>
  6.                 .basicDIV{ border:thin solid blue; display:block; margin:2px; padding:2px; }
  7.                 .hidDIV{ visibility:hidden; }
  8.                 .visDIV{ visibility:visible; }
  9.         </style>

  10.         <script type="text/javascript" src="http://localhost:56486/Scripts/jquery-1.7.1.js"></script>

  11.         <script type="text/javascript">

  12.                 $(document).ready(function () {
  13.                         //I have the feeling this is mandatory to debug in VS2010 but I am not sure yet...
  14.                         jQuery.support.cors = true; // force cross-site scripting (as of jQuery 1.5)

  15.                         //First, I initialize the buttons
  16.                         $("button").each(InitButton);
  17.                 });

  18.                 function InitButton(idx, btn) {
  19.                         $(btn).text(btn.id);
  20.                         $(btn).click(Button_OnClick);
  21.                 }

  22.                 function Button_OnClick(ev) {
  23.                         //On click...  I get the ID of the button and update the title
  24.                         var sId = ev.srcElement.id;
  25.                         $("#title").html(sId);

  26.                         //I empty the list
  27.                         $("#list").empty();

  28.                         //I call my Restfull service that returns JSON objects 
  29. //like this: {"Id":"0","Name":"MyInv","Areas":[]},{"Id":"1","Name":"MyApp","Areas":[] }
  30.                         var sRestService = "http://localhost:56486/api/" + sId;
  31.                         $.getJSON(sRestService, get_Callback);
  32.                 }

  33.                 function get_Callback(data, textStatus, jqXHR) {
  34.                         if (textStatus == "success") {
  35.                                 //First, I show to blocks if they are hidden
  36.                                 $("#inputBlock.hidDIV").removeClass("hidDIV").addClass("visDIV");
  37.                                 $("#listBlock.hidDIV").removeClass("hidDIV").addClass("visDIV");
  38.                                 
  39.                                 //Second, read each object received
  40.                                 $(data).each(ReadData);
  41.                         }
  42.                         else
  43.                                 alert(textStatus);
  44.                 }

  45.                 function ReadData(idx, obj) {
  46.                         /*
  47.                                 First, show the index and the Name property of this this object, this works.
  48.                                 But, as a detail of this <LI>, I would like another list with all members of this object.
  49.                                 For example :
  50.                                 <UL>
  51.                                         <LI>0. MyInv
  52.                                                 <DIV>
  53.                                                         <UL>
  54.                                                                 <LI>0. (Id: 0)</LI>
  55.                                                                 <LI>0. (Name: MyInv)</LI>
  56.                                                                 <LI>0. (Areas: {})</LI>
  57.                                                         </UL>
  58.                                                 </DIV>
  59.                                         </LI>
  60.                                         <LI>1. MyApp
  61.                                                 <DIV>
  62.                                                         <UL>
  63.                                                                 <LI>1. (Id: 1)</LI>
  64.                                                                 <LI>1. (Name: MyApp)</LI>
  65.                                                                 <LI>1. (Areas: {})</LI>
  66.                                                         </UL>
  67.                                                 </DIV>
  68.                                         </LI>
  69.                                 </UL>
  70.                                 So, while I am in the Append() method, I don't see how I could iterate on each member of the object 
  71. and at the same time, append HTML.
  72.                         */
  73.                         $("#list").append("<li>" + idx + ". " + obj.Name + "</li>").append("<div class='basicDIV visDIV'></div>").append("<ul></ul>").append("<li></li>");
  74.                 }
  75.         </script>
  76. </head>
  77. <body>
  78.     <form id="form1" runat="server">
  79.                 <div class="main-content">
  80.                         <div class="basicDIV visDIV">
  81.                                 <button id="Application" type="button"></button>
  82.                                 <button id="Area" type="button"></button>
  83.                                 <button id="Contact" type="button"></button>
  84.                         </div>

  85.                         <h1 id="title"></h1>

  86.                         <div id="inputBlock" class="basicDIV hidDIV">
  87.                                 <label for="prodId">ID:</label>
  88.                                 <input type="text" id="prodId" size="5"/>
  89.                                 <input type="button" value="Search" onclick="find();" />
  90.                                 <p id="product" />
  91.                         </div>

  92.                         <div id="listBlock" class="basicDIV hidDIV">
  93.                                 <ul id="list"/>
  94.                         </div>
  95.                 </div>
  96.     </form>
  97. </body>
  98. </html>