我只想显示 id="firstTr-1"的第一个 tr,但它应该从同一对象获取数据,而其他 tr 应该具有除第一个之外的数据。我应该添加任何注册表帮助程序还是它是内置帮助程序方法!!我查看了文档,但无法弄清楚。
模板:
<script id="firsttemp" type="text/x-handlebars-template">
<tr id="firstTr-1">
<td class="tableFirstCol"><input type="checkbox" class="check" id="childcheck" /></td>
<td class="tableFirstCol"><input type="checkbox" class="star" id="childstar" /></td>
<td class="tableFirstCol"><input type="checkbox" class="alarm" id="childalarm" /></td>
<td class="tableFirstCol">Hero Honda<span>Hero Honda</span></td>
</tr>
{{#each objects}}
<tr>
<td class="tableFirstCol"><input type="checkbox" class="check" id="childcheck" /></td>
<td class="tableFirstCol"><input type="checkbox" class="star" id="childstar" /></td>
<td class="tableFirstCol"><input type="checkbox" class="alarm" id="childalarm" /></td>
<td class="tableFirstCol">{{name}}<span>{{name}}</span></td>
</tr>{{/each}}
</script>
Json:
var company= [
{
"name": "Hero Honda",
"lastprice": 310.2,
"dayschange": 20.1,
"dayshigh": 220.9,
"volume": 140.3,
"52weekhigh": 200.3,
"name2": "herohonda",
"dataset": [1200,3000,3200],
"lastprice2": "1:10pm",
"dayschange2": "8%",
"dayshigh2": "1.5%",
"volume2": "1:10 Pm",
"52weekhigh2": "1:10 Pm"
},
{
"name": "Hero Honda",
"lastprice": 310.2,
"dayschange": 20.1,
"dayshigh": 220.9,
"volume": 140.3,
"52weekhigh": 200.3,
"name2": "herohonda",
"dataset": [3200,3500,5000],
"lastprice2": "1:10pm",
"dayschange2": "8%",
"dayshigh2": "1.5%",
"volume2": "1:10 Pm",
"52weekhigh2": "1:10 Pm"
}]
最佳答案
我认为你把事情过于复杂化了。 Handlebars 更喜欢您在 Handlebars 看到数据之前将逻辑应用于数据。因此,不要乱搞可能只在一个地方使用的自定义帮助程序,而是更改数据并使用 bool 标志标记第一个数据,以便您可以执行简单的 {{#if _first}}
checkin 您的模板。
模板看起来像这样:
<script id="firsttemp" type="text/x-handlebars-template">
{{#each objects}}
<tr{{#if _first}} id="firstTr-1"{{/if}}>
<!--...-->
</tr>
{{/each}}
</script>
您可以像这样设置 _first
标志:
company[0]._first = true;
演示:http://jsfiddle.net/ambiguous/3Cq9K/
一旦解决了这个问题,您就会遇到另一个问题:您正在复制复选框上的 id
属性,但 id
attributes must be unique :
There must not be multiple elements in a document that have the same
id
value.
如果您想避免日后出现各种奇怪且令人困惑的问题,则每个复选框都需要唯一的 id
。
关于javascript - 使用每个 Handlebars 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14934260/