Guillaume Lavoie
Guillaume Lavoie

Reputation: 567

JS Render For Loop in child JSON object

Trying to make this work. I need to display FIRST_NAME and LAST_NAME values beetween to new TDs. The value are stored in an object of the data item. I can't seem to loop througt the child object.

Template example

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

Here is an example of the json object sent to the template ( actually, data.results is sent to the template )

{
  "results":[
     {
     "id":"1",
     "email":"[email protected]",
     "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":"[email protected]",
     "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"
           }
        }
     }
  }
  ]
}

Upvotes: 0

Views: 1446

Answers (1)

idbehold
idbehold

Reputation: 17168

That's a poorly structured response. I should not need to know the id of data's parent to access its children. Also, data isn't an array.

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

Upvotes: 1

Related Questions