过滤NG重复,但保留初始长度/计数

filter ng-repeat yet preserve initial length / count

本文关键字:计数 保留 NG 重复 过滤      更新时间:2023-09-26

我一直在扩展angularjs网站上的一个演示,以添加更多的动态过滤。

我有 3 个过滤器:

  1. 通过输入字段查询筛选器。

  2. 通过选择字段进行订单筛选。

  3. 要通过选择字段显示的结果数。

虽然所有 3 个都独立工作,但当第 1 个被归档时,我在处理第 3 个时遇到了麻烦。我希望第三个选择字段中的下拉选项保留初始计数。

我有以下小提琴。在输入字段中输入单词"dell"会给出 2 个工作正常的结果。

这将在第三个过滤器上提供以下选项。"全部"、"1"、"2"。但是,当我尝试在第三个过滤器上选择"1"(仅显示一个结果)时,一切都消失了。

首先,我不知道为什么会发生这种情况 - 因为当您搜索"moto"一词时,此功能可以正常工作。其次,最好在第三个过滤器中保留初始结果数。

当我在第一个字段中输入"moto"时,它会显示 10 个结果,以下数字仍显示在第三个字段中。"全部"、"1"、"2"、"3"、"4"、"5"、"6"、"7"、"8"、"9"、"10"。

我想要的是,当单击第 3 字段中的上述任何数字时,它们仍然保留。例如,当前当我单击"3"时,仅显示以下内容"全部","1","2","3"。

法典。

  <div class="form-group">
    <label for="search">Search:</label>
    <input id="search" ng-model="query">
  </div>
  <div class="form-group">
   <label for="sort">Sort by:</label>
   <select id="sort" ng-model="phoneCtrl.orderProp">
     <option value="name">Alphabetical</option>
     <option value="age">Newest</option>
     <option value="-age">Oldest</option>
   </select>
  </div>
  <div class="form-group">
    <label for="show">Show:</label>
    <select id="show" ng-model="phoneCtrl.limitProp">
      <option value="">All</option>
      <option ng-repeat="phone in filtered" value="{{$index+1}}">{{$index+1}}</option>
    </select>
  </div>
  <ul class="phones list-unstyled">
    <li ng-repeat="phone in filtered = (phoneCtrl.phones | limitTo : phoneCtrl.limitProp : begin | filter:query | orderBy:phoneCtrl.orderProp)">
      <article class="phone">
         <h2 class="h3">{{phone.name}}</h2>
         <p>{{phone.snippet}}</p>
      </article>
    </li>
   </ul>

为了使第三个过滤器限制为多个项目,您需要重复phoneCtrl.phones而不是filtered,因为filtered对象会更改,但phoneCtrl.phones不会。

<option ng-repeat="phone in phoneCtrl.phones" value="{{$index+1}}">{{$index+1}}</option>

若要使筛选器协同工作,请将limitTo筛选器移到filtered对象定义之外,否则筛选器将仅查看n数量的电话。

<li ng-repeat="phone in filtered = (phoneCtrl.phones | filter:query | orderBy:phoneCtrl.orderProp) | limitTo : phoneCtrl.limitProp : begin ">

更新的小提琴