Accessing Multidimensional JSON with jQuery Templates

1k views Asked by At
<script id="dropdownTemplate" type="text/x-jquery-tmpl">
    <label for="${Name.toLowerCase()}">${Name}</label>
        <select name="${Name.toLowerCase()}" id="${Name.toLowerCase()}_dropdown">
            <option selected='' value=''>-- Select a ${Name} --</option>
            <option value="${$item.Options.Value}">${$item.Options.Choice}</option> 
        </select>
</script>

    var provinces = {
        Name: "Province",
        Options: [
          { Value: "AB", Choice: "Alberta" },
          { Value: "BC", Choice: "British Columbia" },
          { Value: "MB", Choice: "Manitoba" },
          { Value: "NB", Choice: "New Brunswick" },
          { Value: "NF", Choice: "Newfoundland" },
          { Value: "NS", Choice: "Nova Scotia" },
          { Value: "NT", Choice: "Northwest Territories" },
          { Value: "NU", Choice: "Nunavut" },
          { Value: "ON", Choice: "Ontario" },
          { Value: "PE", Choice: "Prince Edward Island" },
          { Value: "QC", Choice: "Quebec" },
          { Value: "SK", Choice: "Saskatchewan" },
          { Value: "YT", Choice: "Yukon" }
        ]
    };


    // Render the template with the provinces data and insert
    // the rendered HTML under the "movieList" element
    $( "#dropdownTemplate" ).tmpl( provinces ).appendTo( "#movieList" );

What is the correct syntax to display a Value or Choice in my jQuery Template?

1

There are 1 answers

0
Chandu On BEST ANSWER

Need couple of changes: 1) Split the dropdown tempalte to select tag template and options template. 2) Use the nested template option to populate the options for a drop down. 3) Pass the provinces as a array object.

Given below are the script changes:

<script id="dropdownTemplate" type="text/x-jquery-tmpl">
    <label for="${Name.toLowerCase()}">${Name}</label>
        <select name="${Name.toLowerCase()}" id="${Name.toLowerCase()}_dropdown">
            <option selected='' value=''>-- Select a ${Name} --</option>
            {{tmpl(Options) "#optionTemplate"}}
        </select>
</script>

<script id="optionTemplate" type="text/x-jquery-tmpl">
    <option value="${Value}">${Choice}</option> 
</script>
<div id="movieList"></div>
<script>
var provinces = {
            Name: "Province",
            Options: [
              { Value: "AB", Choice: "Alberta" },
              { Value: "BC", Choice: "British Columbia" },
              { Value: "MB", Choice: "Manitoba" },
              { Value: "NB", Choice: "New Brunswick" },
              { Value: "NF", Choice: "Newfoundland" },
              { Value: "NS", Choice: "Nova Scotia" },
              { Value: "NT", Choice: "Northwest Territories" },
              { Value: "NU", Choice: "Nunavut" },
              { Value: "ON", Choice: "Ontario" },
              { Value: "PE", Choice: "Prince Edward Island" },
              { Value: "QC", Choice: "Quebec" },
              { Value: "SK", Choice: "Saskatchewan" },
              { Value: "YT", Choice: "Yukon" }
            ]
        };


        // Render the template with the provinces data and insert
        // the rendered HTML under the "movieList" element
        $( "#dropdownTemplate" ).tmpl( [provinces] ).appendTo( "#movieList" );
</script>