在子JSON对象中JS Render For循环

时间:2012-10-03 22:15:08

标签: json jsrender

尝试使这项工作。我需要在新TD之间显示FIRST_NAME和LAST_NAME值。该值存储在数据项的对象中。我似乎无法循环通过子对象。

模板示例

<script id="row_tmpl" type="text/x-jsrender">
      <tr id="{{>id}}">
        <td class="textC">{{>id}}</td>
        <td class="">{{>email}}</td>
        {{for data}}

            ??????

        {{/for}}
      </tr>
</script>

以下是发送到模板的json对象的示例(实际上,data.results被发送到模板)

{
  "results":[
     {
     "id":"1",
     "email":"aaaa@test.com",
     "data":{
        "1":{
           "first_name":{
              "value":"Name 1",
              "public":"1",
              "field":"1",
              "mandatory":"1",
              "possible_value":"",
              "type":"text"
           },
           "last_name":{
              "value":"Name 2",
              "public":"1",
              "field":"2",
              "mandatory":"1",
              "possible_value":"",
              "type":"text"
           }
        }
     }
  },
  {
     "id":"2",
     "email":"test2@test.com",
     "data":{
        "2":{
           "first_name":{
              "value":"Name 3",
              "public":"0",
              "field":"1",
              "mandatory":"1",
              "possible_value":"",
              "type":"text"
           },
           "last_name":{
              "value":"Name 4",
              "public":"0",
              "field":"2",
              "mandatory":"1",
              "possible_value":"",
              "type":"text"
           }
        }
     }
  }
  ]
}

1 个答案:

答案 0 :(得分:1)

这是一个结构不合理的回应。我应需要知道id的父data来访问其子级。此外,data不是数组。

<script id="row_tmpl" type="text/x-jsrender">
      <tr id="{{>id}}">
        <td class="textC">{{>id}}</td>
        <td class="">{{>email}}</td>
        <td class="">{{>data.{{:id}}.first_name.value}}</td>
        <td class="">{{>data.{{:id}}.lastt_name.value}}</td>
      </tr>
</script>