【问题标题】:Number of objects in a ng-repeat that is filtered [duplicate]过滤的 ng-repeat 中的对象数 [重复]
【发布时间】:2015-04-26 17:55:03
【问题描述】:

我在一个站点中使用 angularjs,并且我有一个搜索输入来过滤视图上的列表。此列表显示为带有来自搜索输入的过滤器的 ng-repeat:

搜索输入:

<input type="text" placeholder="Device Search" class="form-control hasclear" 
  ng-model="searchText"/>

这里是 ng-repeat:

<tr ng-click="openModal(device['device_id'], device)" 
  ng-repeat="device in devices | filter:searchText | orderBy:predicate:reverse">

如您所见,ng-repeat 中的过滤器具有使用 ng-model 中的 searchText 变量的过滤器。我想知道当用户在搜索输入中输入文本时是否有可能知道找到了多少对象(ng-repeat 正在显示多少个设备被过滤)。喜欢:0 devices were found3 devices were found...

有没有办法通过我建立这个搜索的方式来显示这些信息?

【问题讨论】:

标签: javascript angularjs angularjs-ng-repeat ng-repeat angularjs-ng-model


【解决方案1】:

如果您在 &lt;tr&gt; 标记之外需要它,我会这样做:

<tr ng-click="openModal(device['device_id'], device)" 
ng-repeat="device in filteredResults = (devices | filter:searchText | orderBy:predicate:reverse)">

{{filteredResults.length}} devices were found

【讨论】:

  • 这正是我所需要的,谢谢!
【解决方案2】:

编辑:

根据 Dave 的正确响应,您必须将变量分配给过滤后的结果

<tr ng-click="openModal(device['device_id'], device)" 
ng-repeat="device in filtered_devices = (devices | filter:searchText | orderBy:predicate:reverse)">

然后,您可以在tr 标签下方输入:

<div>{{filtered_devices.length + ' devices were found'}}</div>

Plunkr 示例here

【讨论】:

  • 这不会显示过滤后的长度,只显示原始数组的总和
  • 我错过了什么?我有一个示例 plunkr,它工作正常。
  • 当然,编辑后的版本有效,但在编辑前发表了评论
【解决方案3】:

使用像filter:x as result 这样的过滤器,它将结果集合存储在变量result 上。

所以你可以这样做:

<tr ng-click="openModal(device['device_id'], device)" ng-repeat="device in devices | filter:searchText as filteredCollection | orderBy:predicate:reverse">
  <td><span>Filtered collection size: {{filteredCollection.length}}</span><td>
</tr>

【讨论】:

    【解决方案4】:

    我确信这些属性是有可能的:

    • $last
    • $索引

    类似:

    <span ng-if="$last" > {{$index}} devices found </span>
    

    文档:https://docs.angularjs.org/api/ng/directive/ngRepeat

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      相关资源
      最近更新 更多