在jsrender中选择数组的一部分

时间:2015-01-16 07:45:54

标签: javascript jquery jsrender

告诉我如何在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的子文档?

1 个答案:

答案 0 :(得分:0)

    <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);

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