简体   繁体   English

限制要在列表表达式中显示的列表项的数量

[英]Limit number of list items to be displayed in List expression

I have a requirement of displaying a list array in two divs, first half in first DIV and second half list in second DIV. 我需要在两个div中显示一个list array ,第一个DIV中的前半部分在第二个DIV中显示后半个列表。 Please not that I don't want to splice the list array in two parts and display them in two lists. 请不要说我不想将列表数组分为两部分并在两个列表中显示它们。

Here is the template I have, for example 例如,这是我的模板

<!-- First half number of items should be displayed in firstHalf div -->
<div id="firstHalf">
   {{#each names:i}}
      {{firstName}} {{lastName}}
   {{/each}}
</div>
<!-- Second half number of items should be displayed in secondHalf div -->
<div id="secondHalf">
   {{#each names:i}}
      {{firstName}} {{lastName}}
   {{/each}}
</div>

For example If I have data like below 例如,如果我有如下数据

var data = [ {
    firstName : "Joe", lastName: "Armstrong"
}, {
    firstName : "Jose", lastName: "Valim"
}];

It needs to be rendered like below 它需要像下面这样渲染

<div id="firstHalf">
   Joe Armstrong
</div>
<div id="firstHalf">
   Jose Valim
</div>

Please give me suggestions to proceed further. 请给我建议以继续进行。

Mustache is logic less, and if you do not provide two arrays, you cannot split the entries in two divs. 胡须少了逻辑,如果您不提供两个数组,则不能将条目分成两个div。

When the template is rendered each occurring {{key}} is substituted with the same value, for each element in the list. 呈现模板后,列表中的每个元素都会用相同的值替换每个出现的{{key}}

For this you could probably use handlebars. 为此,您可能会使用把手。

You can probably do it by iterating over the list twice in a template and still only using one array. 您可以通过在模板中遍历列表两次并且仍然仅使用一个数组来完成此操作。 Just use a conditional in each iteration to to the first or second half of the list. 只需在每次迭代中使用条件到列表的前半部分或后半部分即可。

Template has two sections: one for each half 模板分为两部分:每半部分

<div id="firstHalf">
    {{#each names:i}}
        {{#if i < names.length / 2}} // Conditional to only display first half of `names`
            {{firstName}} {{lastName}}
        {{/if}}
    {{/each}}
</div>
<div id="secondHalf">
    {{#each names:i}}
        {{#if i > names.length / 2}} // Conditional to only display second half of `names`
            {{firstName}} {{lastName}}
        {{/if}}
    {{/each}}
</div>

Inspired by code from here: http://pastie.org/9415897# 此处的代码启发了我们: http : //pastie.org/9415897#

Another valid approach would be to reference list members indirectly, via an index reference. 另一种有效的方法是通过索引引用间接引用列表成员。 In this example we're generating a range of indices, and then referring to list[this] : 此示例中,我们将生成一系列索引,然后引用list[this]

<!-- First half number of items should be displayed in firstHalf div -->
<div id="firstHalf">
   {{#each range(0,names.length/2) }}
      {{names[this].firstName}} {{names[this].lastName}}
   {{/each}}
</div>

<!-- Second half number of items should be displayed in secondHalf div -->
<div id="secondHalf">
   {{#each range(names.length/2,names.length) }}
      {{names[this].firstName}} {{names[this].lastName}}
   {{/each}}
</div>

The range() function just looks like this: range()函数如下所示:

function ( start, end ) {
    var arr = [];
    for ( i = Math.floor(start); i < Math.floor(end); i += 1 ) {
        arr.push( i );
    }
    return arr;
}

Even though there's a perfectly good solution from pseudosavant, I'm mentioning this because indirectly referencing elements is a useful trick to have up your sleeve when doing things like two-way binding with tabular data. 即使从伪专家那里有一个非常好的解决方案,我也要提到这一点,因为在进行诸如表格式数据的双向绑定之类的事情时,间接引用元素是使您精疲力尽的有用技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM