【问题标题】:Selecting part of array in jsrender在jsrender中选择数组的一部分
【发布时间】:2015-01-16 07:45:54
【问题描述】:

告诉我如何在jsrender中只选择部分数据

例如我有那个数组:

сatlist = [
{
    id: "1",
    title: "Category 1",
        products: [
        { name: "Child doc 1" },
        { name: "Child doc 2" }
    ]

},
    {
    id: "2",
    title: "Category 2",
        products: [
        { name: "Child doc 3" },
        { name: "Child doc4" }
      ]

    }
];

如何在模板中只显示id类别为2的子文档?

【问题讨论】:

    标签: javascript jquery jsrender


    【解决方案1】:
        <div id="peopleList"></div>
    
    <script id="personTmpl" type="text/x-jsrender">
      <label>id:</label> {{:id}}<br/>
    <label>name:</label> {{:title}}<br/>
    {{if id==2}}
    {{for products }}
    <label>product name:</label> {{:name}}<br/>
    {{/for}}
    {{/if}}
    </script>
    
    var myTemplate = $.templates("#personTmpl");
    
    var people = [
    {
        id: "1",
        title: "Category 1",
            products: [
            { name: "Child doc 1" },
            { name: "Child doc 2" }
        ]
    
    },
        {
        id: "2",
        title: "Category 2",
            products: [
            { name: "Child doc 3" },
            { name: "Child doc4" }
          ]
    
        }
    ];
    
    var html = myTemplate.render(people);
    
    $("#peopleList").html(html);
    

    如果你想排除任何你可以做的事情,我会放置所有代码,你可以自己排除事情,希望它会帮助你。

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2021-11-03
      相关资源
      最近更新 更多