博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何显示过滤的ng-repeat数据的长度
阅读量:2379 次
发布时间:2019-05-10

本文共 4861 字,大约阅读时间需要 16 分钟。

本文翻译自:

I have a data array which contains many objects (JSON format). 我有一个包含许多对象(JSON格式)的数据数组。 The following can be assumed as the contents of this array: 以下可以假设为此数组的内容:

var data = [  {    "name": "Jim",    "age" : 25  },  {    "name": "Jerry",    "age": 27  }];

Now, I display these details as: 现在,我将这些细节显示为:

Here, query is modeled to an input field in which the user can restrict the data displayed. 这里,查询被建模为输入字段,其中用户可以限制所显示的数据。

Now, I have another location in which I display the current count of people / person being display, ie Showing {

{data.length}} Persons 现在,我有另一个位置,我在其中显示当前显示的人/ Showing {
{data.length}} Persons
,即Showing {
{data.length}} Persons

What I want to do is that when the user searches for a person and the data displayed is filtered based on the query, the Showing...persons also change the value of people being shown currently. 我想要做的是,当用户搜索一个人并且根据查询过滤显示的数据时, Showing...persons也会更改当前Showing...persons的值。 But it is not happening. 但它没有发生。 It always displays the total persons in data rather than the filtered one - how do I get the count of filtered data? 它始终显示数据中的总人数而不是过滤数据 - 如何获取过滤数据的计数?


#1楼

参考:


#2楼

ngRepeat creates a copy of the array when it applies a filter, so you can't use the source array to reference only the filtered elements. ngRepeat在应用过滤器时会创建数组的副本,因此您无法使用源数组仅引用过滤后的元素。

In your case, in may be better to apply the filter inside of your controller using the service: 在您的情况下,使用服务在控制器内应用过滤器可能更好:

function MainCtrl( $scope, filterFilter ) {  // ...  $scope.filteredData = myNormalData;  $scope.$watch( 'myInputModel', function ( val ) {    $scope.filteredData = filterFilter( myNormalData, val );  });  // ...}

And then you use the filteredData property in your view instead. 然后在视图中使用filteredData属性。 Here is a working Plunker: 这是一个有效的Plunker: ://plnkr.co/edit/7c1l24rPkuKPOS5o2qtx?p = preview


#3楼

Here is worked example 这是工作示例

        
Showing {
{data.length}} Persons;
Filtered {
{counted}}
  • {
    {person.name}}


#4楼

For completeness, in addition to previous answers (perform calculation of visible people inside controller) you can also perform that calculations in your HTML template as in the example below. 为了完整性,除了先前的答案(执行控制器内可见人员的计算)之外,您还可以在HTML模板中执行该计算,如下例所示。

Assuming your list of people is in data variable and you filter people using query model, the following code will work for you: 假设您的人员列表是data变量,并且您使用query模型过滤人员,以下代码将适用于您:

Number of visible people: {

{(data|filter:query).length}}

Total number of people: {

{data.length}}

  • {
    {data.length}}
    - prints total number of people
    {
    {data.length}}
    - 打印总人数
  • {
    {(data|filter:query).length}}
    - prints filtered number of people
    {
    {(data|filter:query).length}}
    - 打印已过滤的人数

Note that this solution works fine if you want to use filtered data only once in a page. 请注意 ,如果您只想在页面中使用过滤数据一次 ,此解决方案可以正常工作。 However, if you use filtered data more than once eg to present items and to show length of filtered list, I would suggest using alias expression (described below) for AngularJS 1.3+ or the solution proposed by @Wumms for AngularJS version prior to 1.3. 但是,如果您多次使用过滤数据,例如呈现项目并显示已过滤列表的长度,我建议使用AngularJS 1.3+的 别名表达式 (如下所述)或@Wumms为1.3之前的AngularJS版本提出的解决方案。

New Feature in Angular 1.3 Angular 1.3中的新功能

AngularJS creators also noticed that problem and in version they added "alias" expression which will store the intermediate results of the repeater after the filters have been applied eg AngularJS创建者也注意到了这个问题,并且在版本他们添加了“别名”表达式,该表达式将在应用过滤器之后存储转发器的中间结果,例如

Number of visible people: {

{results.length}}

The alias expression will prevent multiple filter execution issue. 别名表达式将防止多个过滤器执行问题。

I hope that will help. 我希望这会有所帮助。


#5楼

For Angular 1.3+ (credits to @Tom) 适用于Angular 1.3+@Tom积分)

Use an alias expression (Docs: , scroll down to the Arguments section): 使用别名表达式 (文档: ,向下滚动到Arguments部分):

For Angular prior to 1.3 对于1.3之前的Angular

Assign the results to a new variable (eg filtered ) and access it: 将结果分配给新变量(例如,已filtered )并访问它:

Display the number of results: 显示结果数量:

Showing {
{filtered.length}} Persons

Fiddle a . 一个 。 Credits go to 积分去


#6楼

Since AngularJS 1.3 you can use aliases: 从AngularJS 1.3开始,您可以使用别名:

item in items | filter:x as results

and somewhere: 在某个地方:

Total {
{results.length}} result(s).

From : 来自 :

You can also provide an optional alias expression which will then store the intermediate results of the repeater after the filters have been applied. 您还可以提供可选的别名表达式,然后在应用过滤器后存储转发器的中间结果。 Typically this is used to render a special message when a filter is active on the repeater, but the filtered result set is empty. 通常,这用于在转发器上的筛选器处于活动状态时呈现特殊消息,但筛选结果集为空。

For example: item in items | 例如:项目中的项目 filter:x as results will store the fragment of the repeated items as results, but only after the items have been processed through the filter. filter:x as results将重复项的片段存储为结果,但仅在通过过滤器处理了项之后。

转载地址:http://tfexb.baihongyu.com/

你可能感兴趣的文章
libxml2.dylb 添加后找不到<libxml/tree.h> 头文件
查看>>
关于 [[self class] alloc]的理解
查看>>
Eclipse导入Web项目后代码不报错但项目报错(左上角有红叉)解决方案
查看>>
List、Set、数据结构、Collections
查看>>
Sudoku Solver
查看>>
Combination Sum
查看>>
First Missing Positive
查看>>
Trapping Rain Water
查看>>
Permutation Sequence
查看>>
Valid Number
查看>>
Text Justification
查看>>
Simplify Path
查看>>
Add Two Numbers
查看>>
Longest Substring Without Repeating Characters
查看>>
Median of Two Sorted Arrays
查看>>
Search for a Range
查看>>
罗马数字与阿拉伯数字的相互转化
查看>>
3Sum
查看>>
Next Permutation
查看>>
sys文件系统
查看>>