簡體   English   中英

帶有嵌套對象的小胡子寺廟化問題

[英]Mustache templeting Probrem with Nested Object

我遇到了在ajax成功后呈現數據的胡須問題。 我有這樣的返回json文件

{
    "Malaysia-office": {
        "office_name": "Malaysia-office",
        "employee": [
         {
            "capacity": 3600,
            "type": "Office",
            "name": "Office Center"
         },
         {
             "capacity": 3600,
             "type": "WorkSpace",
             "name": "Workspace KL"
          }
        ]
    }
}

和這樣的示例js文件

$.get(url, function (data, textStatus, xhr) {
      $('#office-wrapper').html('');
      $('#office-wrapper').append(Mustache.render($('#section_office').html(), {
       dataOffice: data
      }));
      $("#myModal").modal('show');
});

但是,如果我在刀片文件中調用,則dataOffice不能完美運行,這是我的刀片文件

<template id="section_office">
    @{{#dataOffice}}
    <tr>
        <td>
            <p class="m-0 font-weight-normal">@{{ office_name }}</p>
        </td>   
        @{{#employee}}
        <td>
            <p class="mb-0 text-muted">@{{ name}}</p>
        </td>
        <td>
            <p class="mb-0 text-muted">@{{ type}} Ton</p>
        </td>
        @{{/employee}}
    </tr>   
    @{{/dataOffice}}
</template>

我的問題是,如何顯示數據office_name,名稱和類型? 如果console.log使用javascript,我必須這樣寫

console.log(data['Malaysia-office'].employee[1]['capacity'])

如果您的json結構是這樣,則可以使用此代碼

var c = [];
$.get(url, function (data, textStatus, xhr) {
    var j = 0;
    _.each(data, (o,i) => {
           c.push({'key':i,'value':data[i]})

    });
    $('#office-wrapper').html('');
    $('#office-wrapper').append(Mustache.render($('#section_per_divre').html(), {
         dataOffice: c,
    }));
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM