Jak uzyskać indeks w kierownicy każdego pomocnika?
Używam kierownic do tworzenia szablonów w moim projekcie. Czy istnieje sposób, aby uzyskać indeks bieżącej iteracji" każdego " pomocnika w kierownicy?
<tbody>
{{#each item}}
<tr>
<td><!--HOW TO GET ARRAY INDEX HERE?--></td>
<td>{{this.key}}</td>
<td>{{this.value}}</td>
</tr>
{{/each}}
</tbody>
5 answers
W nowszych wersjach Handlebars index (lub key w przypadku iteracji obiektu) jest domyślnie dostarczany ze standardem each helper.
Fragment z: https://github.com/wycats/handlebars.js/issues/250#issuecomment-9514811
Indeks aktualnej pozycji tablicy jest dostępny od jakiegoś czasu przez @index:
{{#each array}}
{{@index}}: {{this}}
{{/each}}
Do iteracji obiektu, użyj @key zamiast:
{{#each object}}
{{@key}}: {{this}}
{{/each}}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-29 21:10:52
To się zmieniło w nowszych wersjach Ember.
Dla tablic:
{{#each array}}
{{_view.contentIndex}}: {{this}}
{{/each}}
Wygląda na to, że #każdy blok nie działa już na obiektach. Moja sugestia polega na tym, aby uruchomić własną funkcję pomocnika.
Dzięki za ten napiwek .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:34:48
Wiem, że jest za późno. Ale rozwiązałem ten problem za pomocą następującego kodu:
Java Script:
Handlebars.registerHelper('eachData', function(context, options) {
var fn = options.fn, inverse = options.inverse, ctx;
var ret = "";
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ctx = Object.create(context[i]);
ctx.index = i;
ret = ret + fn(ctx);
}
} else {
ret = inverse(this);
}
return ret;
});
HTML:
{{#eachData items}}
{{index}} // You got here start with 0 index
{{/eachData}}
Jeśli chcesz rozpocząć Indeks od 1, powinieneś wykonać następujący kod:
Javascript:
Handlebars.registerHelper('eachData', function(context, options) {
var fn = options.fn, inverse = options.inverse, ctx;
var ret = "";
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ctx = Object.create(context[i]);
ctx.index = i;
ret = ret + fn(ctx);
}
} else {
ret = inverse(this);
}
return ret;
});
Handlebars.registerHelper("math", function(lvalue, operator, rvalue, options) {
lvalue = parseFloat(lvalue);
rvalue = parseFloat(rvalue);
return {
"+": lvalue + rvalue
}[operator];
});
HTML:
{{#eachData items}}
{{math index "+" 1}} // You got here start with 1 index
{{/eachData}}
Dzięki.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-08 11:29:11
W wersji 3.0,
{{#each users as |user userId|}}
Id: {{userId}} Name: {{user.name}}
{{/each}}
W tym przykładzie użytkownik będzie miał taką samą wartość jak bieżący kontekst, a identyfikator userId będzie miał wartość indeksu dla iteracji. Refer - http://handlebarsjs.com/block_helpers.html w sekcji pomocników bloków
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-20 16:43:43
Tablice:
{{#each array}}
{{@index}}: {{this}}
{{/each}}
Jeśli masz tablice obiektów... można iterować przez dzieci:
{{#each array}}
//each this = { key: value, key: value, ...}
{{#each this}}
//each key=@key and value=this of child object
{{@key}}: {{this}}
//Or get index number of parent array looping
{{@../index}}
{{/each}}
{{/each}}
Obiekty:
{{#each object}}
{{@key}}: {{this}}
{{/each}}
Jeśli masz zagnieżdżone obiekty, możesz uzyskać dostęp do key
obiektu nadrzędnego z
{{@../key}}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-22 08:31:01