Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

javascript - 如何显示过滤的ng-repeat数据的长度(How to display length of filtered ng-repeat data)

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:

(现在,我将这些细节显示为:)

<div ng-repeat="person in data | filter: query">
</div

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?

(它始终显示数据中的总人数而不是过滤数据 - 如何获取过滤数据的计数?)

  ask by user109187 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Angular 1.3+ (credits to @Tom)

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

Use an alias expression (Docs: Angular 1.3.0: ngRepeat , scroll down to the Arguments section):

(使用别名表达式 (文档: Angular 1.3.0:ngRepeat ,向下滚动到Arguments部分):)

<div ng-repeat="person in data | filter:query as filtered">
</div>

For Angular prior to 1.3

(对于1.3之前的Angular)

Assign the results to a new variable (eg filtered ) and access it:

(将结果分配给新变量(例如,已filtered )并访问它:)

<div ng-repeat="person in filtered = (data | filter: query)">
</div>

Display the number of results:

(显示结果数量:)

Showing {{filtered.length}} Persons

Fiddle a similar example .

(一个类似的例子 。)

Credits go to Pawel Kozlowski

(积分去Pawel Kozlowski)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...