如何将forEach循环限制为仅迭代16次

时间:2015-04-16 16:30:09

标签: javascript html knockout.js

我想将forEach循环限制为仅显示最多16个结果。有没有办法实现这个目标?

  <div class="events-list" data-bind="if: (typeof(Events) != 'undefined')" style="padding-top:5px; padding-bottom:5px;">
    <div data-bind="foreach: Events">
      <!-- ko if: StatusImage -->
      <div data-bind="foreach: EventNames" style="margin-bottom:-6px;">
        <article data-bind="attr:{class: OfferType== 'TM Event' ? 'TMEvents event-item smooth-hover1' : 'SpecialEvents event-item smooth-hover1'}" style="display:table">
          <img src="http://placehold.it/131x70">



          <div data-bind="attr:{class: OfferType== 'TM Event' ? 'btn-cell TMEvents' : 'btn-cell SpecialEvents'}" class="btn-cell" style="display:table-cell!important;">
            <a style="width:233px; margin-bottom:2px;" target="_blank" data-bind="attr: {href: Ticket, class: OfferType && OfferType=='TM Event' ? 'button buy' : 'button view'}">
              <span data-bind="text: OfferType && OfferType== 'TM Event'  ? 'TICKET/EVENT INFO' : 'VIEW EVENT'"></span>
            </a>
          </div>
        </article>
      </div>
      <!-- /ko -->
    </div>                                                                  

1 个答案:

答案 0 :(得分:1)

使用slice有效:

 <ul class="list-unstyled" data-bind="foreach: items.slice(0, 16)">
    <li data-bind="text: number + ' '+ name"></li>
 </ul>

这是显示此内容的Plunkr