5

Glance this JSON for a sec. Yes, it's nested like hell. And I need it to be nested to keep the data-hierarchy.

JSON from firebug console

My problem is that the keys are not generic (due to C# Dictionary keys can't be the same). They vary depending on the data. My template looks like this so far:

<script id="customerTemplate" type="text/x-jQuery-tmpl">
        {{each $data}}
            <div class="Customer">
                <input class="CustomerId" type="hidden" value="${ $index }" />
                <div class="CustomerHeader">
                    <div class="NameAndCheckbox">
                        <input type="checkbox" checked="checked" class="CustomerCheckbox" />
                        <span class="HeadlineText">${ $index }</span>
                    </div>
                </div>
                <div class="CustomerProjectWrapper">

                    /* HOW TO ACCESS DATA WITHIN $data */
                </div>
            </div>
        {{/each}}
    </script>

As you see, I want to access the json within $data. $data's value contains JSON, but I don't know the syntax to access that.. and inside each $data's value there's also JSON.

How can I do this?

Note:

This is my jQuery code:

$.template("ctmpl", $("#customerTemplate"));

$.tmpl("ctmpl", jsonobject).appendTo("#CustomerContainer");

3
  • Just FYI, this isn't a JSON, it's a JavaScript Object. JSON is a serialization format. You may receive it from the server as JSON, but once you parse it it's just an Object. Commented Sep 12, 2011 at 11:53
  • yeah okay... I have JSON (string) from the ajax call I make prior to this. Which is done like this var jsonobject = $.parseJSON(response);. You think I should use the json (not the javascript object)? Commented Sep 12, 2011 at 12:03
  • my question remains... How can I iterate through $data, and after that iterate through $data's $data? Commented Sep 12, 2011 at 12:23

1 Answer 1

9

You can use a syntax like this: {{each(index, value) array}} to have a clear idea of the index/value of what you are looping on. {{each}} can iterate through properties on an object as well.

So, if you had data like this:

var data = {
    testA: {
        testA1: {
            testA1A: "blahA1A",
            testA1B: "blahA1B" 
        },
        testA2: {
            testA2A: "blahA2A",
            testA2B: "blahA2B"  
        }
    },
    testB: {
        testB1: {
            testB1A: "blahB1A",
            testB1B: "blahB1B" 
        },
        testB2: {
            testB2A: "blahB2A",
            testB2B: "blahB2B" 
        },
    }
};

You could write a template like this:

<script id="contentTmpl" type="text/html">
    <ul>
    {{each(i, item) $data}}
        <li>${i}</li>
        <ul>
        {{each(j, subItem) item}}
            <li>${j}</li>    
            <ul>
            {{each(k, subSubItem) subItem}}
                <li>${k} = ${subSubItem}</li>
            {{/each}}
            </ul>
        {{/each}}
        </ul>
    {{/each}}
    </ul>
</script>

Sample here: http://jsfiddle.net/rniemeyer/8PLGP/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.