在angularjs中使用groupby [英] Using groupby in angularjs
问题描述
我有一张桌子
id amt
---- ----
1 10
1 20
2 30
2 40
3 40
4 50
4 10
4 40
i希望结果为
id amt
---- -----
1 10
20
2 30
40
3 40
4 50
10 br />
使用ng repeat和groupby
我尝试过:
id amt
---- -----
1 10
20
2 30
40
3 40 >
4 50
10
40
使用ng repeat和groupby
试试这个
< !DOCTYPE html >
< html lang = en-US >
< head >
< script src = http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js &克t; < / script >
< script src = https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js > < / script >
< script >
var app = angular.module(' myapp',[' angular.filter'跨度>]);
app.controller(' myctrl',myctrl);
function myctrl(
scope){
scope.items = [{id: 1 ,amt: 10 },{id: 1 ,amt: 20 },{id: 2 , amt: 30 },{id: 2 ,amt: 40 },{id: 3 ,amt: 10 },{id: 4 ,amt: 10 },{id: 4 ,amt: 30 },{id: 4 ,amt: 50 }];
}
< / script >
< < span class =code-leadattribute> / head >
< 正文 >
< div ng -app = myapp ng-controller = myctrl >
< TABL e cellspacing = 1 border = 1 >
< tr >
< td > id < / td >
< td > amt < / td >
< / tr >
< tr ng-repeat = >
< td > {{key}} < / td >
< td colspan = 2 >
< 表格 >
< tr ng-repeat = 项目值 >
< td > {{item.amt}} < / td >
< / tr >
< / table >
< / td >
< / tr >
< / table >
< / div >
< / body >
< / html >
i'm having a table as
id amt
---- ----
1 10
1 20
2 30
2 40
3 40
4 50
4 10
4 40
i want the result to be as
id amt
---- -----
1 10
20
2 30
40
3 40
4 50
10
40
using ng repeat and groupby
What I have tried:
id amt
---- -----
1 10
20
2 30
40
3 40
4 50
10
40
using ng repeat and groupby
try this
<!DOCTYPE html> <html lang="en-US"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js"></script> <script> var app = angular.module('myapp', ['angular.filter']); app.controller('myctrl', myctrl); function myctrl(
scope) {
scope.items = [{ id: 1, amt: 10 }, { id: 1, amt: 20 }, { id: 2, amt: 30 }, { id: 2, amt: 40 }, { id: 3, amt: 10 }, { id: 4, amt: 10 }, { id: 4, amt: 30 }, { id: 4, amt: 50 }]; } </script> </head> <body> <div ng-app="myapp" ng-controller="myctrl"> <table cellspacing="1" border="1"> <tr> <td>id</td> <td>amt</td> </tr> <tr ng-repeat="(key, value) in items | groupBy: 'id'"> <td>{{key}}</td> <td colspan="2"> <table> <tr ng-repeat="item in value"> <td>{{item.amt}}</td> </tr> </table> </td> </tr> </table> </div> </body> </html>
这篇关于在angularjs中使用groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!