2017-09-25 83 views
0

按照这些文章:如何显示json键并通过handlebars.js更改它?

How to get index in handlebars each helper

Mustache.js - display key instead of value

我想显示JSON的键,然后我想改变这种对象下键的名称。

{ 
    "success":true, 
    "msg":"The request to access the credit report successfully processed", 
    "errors":{ 

    }, 
    "arf":{ 
     "TradeLine":{ 
     "TradeLine":{ 
      "Mortgage Accounts":[ 
       { 
        "SubscriberDisplayName":"abc", 
        "MonthsHistory":99, 
        "PaymentProfile":{ 
        "2017":{ 
         "1":"C", 
         "2":"C", 
         "3":"C", 
         "4":"C", 
         "5":"C", 
         "6":"C", 
         "7":"C", 
         "8":" ", 
         "9":" ", 
         "10":" ", 
         "11":" ", 
         "12":" " 
        }, 
        "2016":{ 
         "1":"C", 
         "2":"C", 
         "3":"C", 
         "4":"C", 
         "5":"C", 
         "6":"C", 
         "7":"C", 
         "8":"C", 
         "9":"C", 
         "10":"C", 
         "11":"C", 
         "12":"C" 
        } 
       }], 

我已经创建了表格,并且我已经显示了其他数据。 现在在不同的行作为标题我想显示2017年和2017年内的所有关键值。但我想更改为“1”的关键“1”,“2”作为feb等。 我收到表的格式,但我没有拿到钥匙,如2017,2016等

我的代码: 尝试1:

<thead class="thead-default"> 
        <tr> 
        <th>{{SubscriberDisplayName}}</th> 
        </tr> 
</thead> 
    {{# repData}} 
      {{#each arf.TradeLine.TradeLine.[Mortgage Accounts]}} 
       <table id="RealEstateTbl" class="table table-sm table-bordered"> 
        {{#each PaymentProfile}} 
         <tr> 
         <th>Payment History (Upto 25 months)</th> 
         <th>{{this.key}}</th> 
         {{/each}} 
         </tr> 
       </table> 
      {{/each}} 
    {{/ repData}} 

尝试2:

Javascript/Handlers: 
      var reportTemplate = $("#report-template").html(); 
      var theTemplate = Handlebars.compile(reportTemplate); 
      var reportData = theTemplate(this.ReportbyIDdata); 
      $('body').html(theTemplate({repData: this.ReportbyIDdata})); 
      // console.log(repData); 
      Handlebars.registerHelper('eachkeys', function(context, options) { 
       var fn = options.fn, inverse = options.inverse; 
       var ret = ""; 

       var empty = true; 
       for (key in context) { empty = false; break; } 

       if (!empty) { 
        for (key in context) { 
         ret = ret + fn({ 'key': key, 'value': context[key]}); 
        } 
       } else { 
        ret = inverse(this); 
       } 
       return ret; 
      }); 

     }) 

{{# repData}} 
      {{#each arf.TradeLine.TradeLine.[Mortgage Accounts]}} 
       <table id="RealEstateTbl" class="table table-sm table-bordered"> 
        {{#eachkeys PaymentProfile}} 
         <tr> 
         <th>Payment History (Upto 25 months)</th> 
         <th>{{this.key}}</th> 
         {{/eachkeys}} 
         </tr> 
       </table> 
      {{/each}} 
    {{/ repData}} 

什么做到

  1. 显示年份即键
  2. 更改对象内的键?即显示“Jan”而不是“1”? - > PS:我还没有尝试过。

回答

0

通过

{{#each PaymentProfile}} 
        <tr> 
        <th>Payment History (Upto 25 months)</th> 
        <th>{{@key}}</th> 
        </tr> 
{{/each}} 

解决的第一个问题,但它显示如下:

付款历史(高达25个月)2015年

付款历史(高达25个月)2016

支付历史(高达25个月)2017

我希望2017年再到2016年,等等。 我该怎么做? 也请给我一些见解我的第二个问题。