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
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Inventory</title>
- <style>
- .basicDIV{ border:thin solid blue; display:block; margin:2px; padding:2px; }
- .hidDIV{ visibility:hidden; }
- .visDIV{ visibility:visible; }
- </style>
- <script type="text/javascript" src="http://localhost:56486/Scripts/jquery-1.7.1.js"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- //I have the feeling this is mandatory to debug in VS2010 but I am not sure yet...
- jQuery.support.cors = true; // force cross-site scripting (as of jQuery 1.5)
- //First, I initialize the buttons
- $("button").each(InitButton);
- });
- function InitButton(idx, btn) {
- $(btn).text(btn.id);
- $(btn).click(Button_OnClick);
- }
- function Button_OnClick(ev) {
- //On click... I get the ID of the button and update the title
- var sId = ev.srcElement.id;
- $("#title").html(sId);
- //I empty the list
- $("#list").empty();
- //I call my Restfull service that returns JSON objects
- //like this: {"Id":"0","Name":"MyInv","Areas":[]},{"Id":"1","Name":"MyApp","Areas":[] }
- var sRestService = "http://localhost:56486/api/" + sId;
- $.getJSON(sRestService, get_Callback);
- }
- function get_Callback(data, textStatus, jqXHR) {
- if (textStatus == "success") {
- //First, I show to blocks if they are hidden
- $("#inputBlock.hidDIV").removeClass("hidDIV").addClass("visDIV");
- $("#listBlock.hidDIV").removeClass("hidDIV").addClass("visDIV");
-
- //Second, read each object received
- $(data).each(ReadData);
- }
- else
- alert(textStatus);
- }
- function ReadData(idx, obj) {
- /*
- First, show the index and the Name property of this this object, this works.
- But, as a detail of this <LI>, I would like another list with all members of this object.
- For example :
- <UL>
- <LI>0. MyInv
- <DIV>
- <UL>
- <LI>0. (Id: 0)</LI>
- <LI>0. (Name: MyInv)</LI>
- <LI>0. (Areas: {})</LI>
- </UL>
- </DIV>
- </LI>
- <LI>1. MyApp
- <DIV>
- <UL>
- <LI>1. (Id: 1)</LI>
- <LI>1. (Name: MyApp)</LI>
- <LI>1. (Areas: {})</LI>
- </UL>
- </DIV>
- </LI>
- </UL>
- So, while I am in the Append() method, I don't see how I could iterate on each member of the object
- and at the same time, append HTML.
- */
- $("#list").append("<li>" + idx + ". " + obj.Name + "</li>").append("<div class='basicDIV visDIV'></div>").append("<ul></ul>").append("<li></li>");
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="main-content">
- <div class="basicDIV visDIV">
- <button id="Application" type="button"></button>
- <button id="Area" type="button"></button>
- <button id="Contact" type="button"></button>
- </div>
- <h1 id="title"></h1>
- <div id="inputBlock" class="basicDIV hidDIV">
- <label for="prodId">ID:</label>
- <input type="text" id="prodId" size="5"/>
- <input type="button" value="Search" onclick="find();" />
- <p id="product" />
- </div>
- <div id="listBlock" class="basicDIV hidDIV">
- <ul id="list"/>
- </div>
- </div>
- </form>
- </body>
- </html>