开发者

Accessing Multidimensional JSON with jQuery Templates

开发者 https://www.devze.com 2023-02-08 15:35 出处:网络
<script id=\"dropdownTemplate\" type=\"text/x-jquery-tmpl\"> <label for=\"${Name.toLowerCase()}\">${Name}</label>
<script id="dropdownTemplate" type="text/x-jquery-tmpl">
    <label for="${Name.toLowerCase()}">${Name}</label>
        <select n开发者_开发问答ame="${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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消