gpt4 book ai didi

jquery - Mustache.js - 显示键而不是值

转载 作者:行者123 更新时间:2023-12-03 22:52:41 24 4
gpt4 key购买 nike

我在这里使用这些数据:http://pastie.org/3231052 - 如何使用 Mustache 或 Handlebars 显示键而不是值?

[{"interval":"2012-01-21",
"advertiser":"Advertisers 1",
"offer":"Life Insurance",
"cost_type":"CPA",
"revenue_type":"CPA",
... etc ...
}]

最佳答案

如果你想显示键值对,你可以在Handlebars中编写一个helper。

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;
});

$(function() {
var data = {"interval":"2012-01-21",
"advertiser":"Advertisers 1",
"offer":"Life Insurance",
"cost_type":"CPA",
"revenue_type":"CPA"};

var source = $("#template").html();
var template = Handlebars.compile(source);
$('#content').html(template({'data': data}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0.beta2/handlebars.min.js"></script>
<script id="template" type="text/x-handlebars-template">
{{#eachkeys data}}
<li>{{this.key}} - {{this.value}}</li>
{{/eachkeys}}
</script>
<div id="content">
</div>

编辑

似乎这并不完全是您想要的,但是可以想出一个可以完成此任务的助手。

关于jquery - Mustache.js - 显示键而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8961939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com