Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算ng repeat中的行和?_Javascript_Angularjs_Angularjs Ng Repeat_Ng Repeat_Angularjs Filter - Fatal编程技术网

Javascript 如何计算ng repeat中的行和?

Javascript 如何计算ng repeat中的行和?,javascript,angularjs,angularjs-ng-repeat,ng-repeat,angularjs-filter,Javascript,Angularjs,Angularjs Ng Repeat,Ng Repeat,Angularjs Filter,我有一个稍微不同的要求,请您在标记为副本之前通读一遍。 举个例子: <table ng:init="stuff={items:[{description:'gadget', cost:99,date:'jan3'},{description:'thing', cost:101,date:'july6'},{description:'thing', cost:101,date:'jan3'} ]}"> <tr> <th>Descripti

我有一个稍微不同的要求,请您在标记为副本之前通读一遍。 举个例子:

<table ng:init="stuff={items:[{description:'gadget', cost:99,date:'jan3'},{description:'thing', cost:101,date:'july6'},{description:'thing', cost:101,date:'jan3'} ]}">
    <tr>
        <th>Description</th>
        <th>Cost</th>
    </tr>

    <tr ng:repeat="item in stuff.items|filter">   /*only filtered item grouped by date*/    
        <td>{{item.description}}</td>
        <td ng-bind='item.cost'>{{item.cost}}</td>
    </tr>

    <tr>
        <td></td>
        <td>{{total}}</td>   /*cost of items grouped by date jan3*/
    </tr>
</table>

描述
成本
/*仅按日期分组的筛选项目*/
{{item.description}
{{item.cost}
{{total}}/*按日期1月3日分组的项目成本*/

如何计算按项目分组的总成本?angular中是否有任何数据属性,我可以在其中添加分组项目的成本,然后再次为下一个分组项目重新初始化成本?

您可以创建自己的自定义过滤器,该过滤器将接受数组并返回所有项目的总成本

标记

<tr ng:repeat="item in filteredData = (stuff.items|filter)">
    <td>{{item.description}}</td>
    <td ng-bind='item.cost'>{{item.cost}}</td>
</tr>
<tr>
    <td></td>
    <td>{{filteredData| total}}</td>   /*cost of items grouped by date jan3*/
</tr>

您可以创建自己的自定义筛选器,该筛选器将接受数组并返回所有项目的总成本

标记

<tr ng:repeat="item in filteredData = (stuff.items|filter)">
    <td>{{item.description}}</td>
    <td ng-bind='item.cost'>{{item.cost}}</td>
</tr>
<tr>
    <td></td>
    <td>{{filteredData| total}}</td>   /*cost of items grouped by date jan3*/
</tr>

Angular 1.3增加了与过滤器结合使用时非常有用的功能

表达式中的变量为别名\表达式
–您还可以提供可选的别名表达式,该表达式将在应用过滤器后存储中继器的中间结果。通常,当中继器上的筛选器处于活动状态,但筛选的结果集为空时,此选项用于呈现特殊消息

例如:
item in items | filter:x as results
将重复项目的片段存储为
results
,但仅在通过过滤器处理项目之后

因此,您可以使用此
作为别名_表达式
对列表的筛选子集执行计算。i、 e:

<tr ng-repeat="item in stuff.items|filter as filteredStuff">
  {{filteredStuff.length}}
  {{calculateTotal(filteredStuff)}}
</tr>

Angular 1.3增加了与过滤器结合使用时非常有用的功能

表达式中的变量为别名\表达式
–您还可以提供可选的别名表达式,该表达式将在应用过滤器后存储中继器的中间结果。通常,当中继器上的筛选器处于活动状态,但筛选的结果集为空时,此选项用于呈现特殊消息

例如:
item in items | filter:x as results
将重复项目的片段存储为
results
,但仅在通过过滤器处理项目之后

因此,您可以使用此
作为别名_表达式
对列表的筛选子集执行计算。i、 e:

<tr ng-repeat="item in stuff.items|filter as filteredStuff">
  {{filteredStuff.length}}
  {{calculateTotal(filteredStuff)}}
</tr>

但我只需要在ng repeat中按日期过滤的项目。@curiousUser您检查了我的答案吗?但我只需要在ng repeat中按日期过滤的项目。@curiousUser您检查了我的答案吗?