0

我想用递归 JSON 做一个车把模板,但我不知道 JSON 的键是什么(可能会改变),我还需要一个 IF 语句,它正在工作。有什么方法可以创建一个 Helper 来做到这一点?现在我的答案是[object object]。这是我的 jsfiddle https://jsfiddle.net/deherinu/dvn4076h/

我的 JSON:

var json={
"results": [
 {
   "collection": "employer",
   "summary":{
     "person": [
       { "name": "My name" },
       { "lastname": "My lastname" },
       { "address": "Street" }
     ],
     "job": "CEO"
   }
 },
 {
   "collection": "employer",
   "summary":{
     "person": [
       { "name": "My name" },
       { "lastname": "My lastname" },
       { "address": "Street" }
     ],
     "job": "Administrator"
   }
 },
 {
   "collection": "company",
   "summary": "Company1"
 },
 {
   "collection": "company",
   "summary": "Company2"
 }
   ]
 };

我的输出

谢谢!

4

1 回答 1

0

将您的内容包裹在person对象周围。

<script id="template" type="text/x-handlebars-template">
  <div style="width: 80%; margin: 0 auto;">
    {{#results}}
    <h3 style="text-transform: uppercase;">{{collection}}</h3>
    {{#xif " collection == 'employer' " }}
    {{#summary}}
    {{#person}}
    <div>
      <table>
        <tbody>
          {{#each this}}
          <tr>
            <td><b>{{@key}}</b></td>
            <td>{{this}}</td>
          </tr>
          {{/each}}
        </tbody>
      </table>
    </div>
    {{/person}}
    {{/summary}}
    {{else}}
    <div>
      {{summary}}
    </div>
    {{/xif}}
    {{/results}}
  </div>
</script>
于 2017-09-26T13:15:26.793 回答