[英]JQuery - Number items in a list
I would like the number of item's in my list to be numbered from 10 --> 0 我希望列表中的项目编号从10 - > 0开始编号
I have the following code: 我有以下代码:
<ul class="StoryBoard" id="StoryBoard">
{#storylines}
<li>{text|bl|s}</li>
{/storylines}
</ul>
An example of how this would render in the browser would be: 这将在浏览器中呈现的示例如下:
<ul class="StoryBoard" id="StoryBoard">
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
</ul>
However I would like it to render like this: 但是我希望它像这样渲染:
<ul class="StoryBoard" id="StoryBoard">
<li>Test (10)</li>
<li>Test (9)</li>
<li>Test (8)</li>
<li>Test (7)</li>
</ul>
There will always be a maximum of 10 items 总会有最多10件商品
Since you're using dustjs, you have access to an @idx
helper when iterating: 由于您使用的是dustjs,
@idx
在迭代时可以访问@idx
助手:
{#storylines}
<li>{text|bl|s} ({@idx}{.}{/idx})</li>
{/storylines}
From the dustjs docs : 来自dustjs 文档 :
The idx tag passes the numerical index of the current element to the enclosed block.
idx标记将当前元素的数字索引传递给封闭块。
EDIT: I didn't read your question closely enough - it looks like you're looking for a descending count. 编辑:我没有仔细阅读你的问题 - 看起来你正在寻找降序数。 the
idx
helper will count up. idx
助手会计数。
EDIT AGAIN: In the comments it was asked if you could just do {@idx}{10 - .}{/idx}
. 再次编辑:在评论中询问您是否可以
{@idx}{10 - .}{/idx}
。
The answer to that is no, because dust doesn't evaluate arbitrary expressions. 答案是否定的,因为灰尘不会评估任意表达。 But if you look at the source of
dust.helpers.idx
, which is where the tag comes from, it's just: 但是如果你看一下
dust.helpers.idx
的来源,它就是标签的来源,它只是:
function (chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index))
}
It's pushing a new context with the value of the current index of the iteration. 它正在推动一个新的上下文,其中包含当前迭代索引的值。 It turns out that the
context.stack
object has an attribute of
that gives you the number of items in the stack, so you can write your own negidx
helper: 事实证明,
context.stack
对象有一个属性of
它给你堆栈中的项目数,所以你可以编写自己的negidx
帮助器:
dust.helpers.negidx = function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.of - context.stack.index));
};
At this point, the following will accomplish what the original question asked: 此时,以下内容将完成原始问题:
{#storylines}
<li>{text|bl|s} ({@negidx}{.}{/negidx})</li>
{/storylines}
I'd suggest: 我建议:
$('#StoryBoard li').text(function(i,t) { return t + ' (' + (10 - i )+ ')'});
The anonymous function in the text()
method, has two parameters i
(the index of the current element (as it iterates over all the elements matched by the selector)) and t
, which is the text of the current element. text()
方法中的匿名函数有两个参数i
(当前元素的索引(因为它迭代选择器匹配的所有元素))和t
,它是当前元素的文本。
This, then, returns the current text after appending an opening-bracket, the index and a closing bracket. 然后,这会在附加开括号,索引和结束括号后返回当前文本。
References: 参考文献:
$('#StoryBoard').find('li').each(function(i){
$(this).append(' ' + (10 - i ));
});
Test? 测试? : http://jsbin.com/aduqix/1/edit
: http : //jsbin.com/aduqix/1/edit
here's a demo http://jsfiddle.net/GPEth/ - in jQuery I'm not sure if you want this fix in dust.js or jQuery 这是一个演示http://jsfiddle.net/GPEth/ - 在jQuery中我不确定你是否想要在dust.js或jQuery中修复此问题
$(function () {
var $li = $('ul#StoryBoard li'),
count = $li.size();
$li.each(function (index) {
$(this).text($(this).text() + (count - index));
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.