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}}
什么做到
- 显示年份即键
- 更改对象内的键?即显示“Jan”而不是“1”? - > PS:我还没有尝试过。