[英]How to exclude an array/object element with Handlebars?
Right now I have this json structure 现在我有这个json结构
"administration" : [
{
"name" : "Sigfried Bermudez",
"role" : "Associate Director"
},
{
"name" : "Karen Madrigal",
"role" : "QA Manager"
},
{
"name" : "Jorge Lopez",
"role" : "Project Manager"
}
]
as you see the last element in that array says "role" : "Project Manager"
, and the way I am excluding this element from the view is this 如您所见,该数组中的最后一个元素是
"role" : "Project Manager"
,而我从视图中排除该元素的方式是这样的
{{#each chart-container.[3].administration}}
{{#unless @last}}
<span>{{@key}} {{this.name}}</span>
{{/unless}}
{{/each}}
but what about that element changes the position? 但是那个元素会改变位置吗?
what I need to know is if I can do something like {{#unless this.role.toLowerCase().indexof("project")}}
or what is the best option in this case? 我需要知道的是我是否可以做类似
{{#unless this.role.toLowerCase().indexof("project")}}
事情,或者在这种情况下最好的选择是什么? if there is a way to do this from the HTML, it would be awesome :) 如果有办法从HTML做到这一点,那就太好了:)
Working example: JsFiddle 工作示例: JsFiddle
Use Handlebars helper: 使用车把帮手:
var entry_template = document.getElementById('entry-template').innerHTML;
var data = [
{
"name" : "Sigfried Bermudez",
"role" : "Associate Director"
},
{
"name" : "Karen Madrigal",
"role" : "QA Manager"
},
{
"name" : "Jorge Lopez",
"role" : "Project Manager"
}
]
Handlebars.registerHelper("ifvalue", function(conditional, options) {
if (conditional !== options.hash.notequals) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
var template = Handlebars.compile(entry_template);
var template_with_data = template(data);
document.getElementById('data').insertAdjacentHTML('afterbegin', template_with_data);
Template: 模板:
<script id="entry-template" type="text/x-handlebars-template">
{{#each this}}
{{#ifvalue this.role notequals="Project Manager"}}
{{this.name}}
{{/ifvalue}}
{{/each}}
</script>
<div id="data">
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.