javascript 在 Angular js 中使用 ng-repeat 指令在表中显示数组数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21378836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
displaying an array data in a table using ng-repeat directive in Angular js
提问by user67867
I am building an application using Angular.js,Node.js and MongoDB.
我正在使用 Angular.js、Node.js 和 MongoDB 构建应用程序。
I have got an array of data through ajax call and is as follows
我通过ajax调用得到了一组数据,如下
[{
"event":"treat",
"expenselist":[
{"text":"a","done":true,"value":"100"},
{"text":"b","done":true,"value":"400"}
],
"expense":500,
"sharelist":[
{"text":"b","done":true},
{"text":"c"},
{"text":"d","done":true},
{"text":"a","done":true},
{"text":"e","done":true}
],
"shareno":4,
"shareamount":125
},{
"event":"lunch",
"expenselist":[
{"text":"c","done":true,"value":"500"}
],
"expense":500,
"sharelist":[
{"text":"b","done":true},
{"text":"c","done":true},
{"text":"d","done":true},
{"text":"a","done":true},
{"text":"e","done":true}
],
"shareno":5,
"shareamount":100
}]
I am pushing the data one by one in an array $scope.sharedetails
inorder to display each event in each row.
我正在一个数组中一个一个地推送数据,$scope.sharedetails
以便在每一行中显示每个事件。
angular.forEach(data,function(event,k){
$scope.sharedetails.push(data[k]);
$scope.expensedetails.push($scope.sharedetails[k].expenselist);
$scope.attendeedetails.push($scope.sharedetails[k].sharelist);
angular.forEach($scope.expensedetails,function(text,i){
angular.forEach($scope.expensedetails[i],function(text,j){
if($scope.expensedetails[i][j].done==true){
$scope.spentpeople.push($scope.expensedetails[i][j].text);
$scope.expensevalues.push($scope.expensedetails[i][j].value);
}
});
});
angular.forEach($scope.attendeedetails,function(text,i){
angular.forEach($scope.attendeedetails[i],function(text,j){
if($scope.attendeedetails[i][j].done==true){
$scope.sharelistresult.push($scope.attendeedetails[i][j].text);
}
});
});
});
HTML:
HTML:
<td>{{sharedetail.event}}</td>
<td><ul class="unstyled">
<li ng-repeat="spent in spentpeople">
<span>{{spent}}</span>
</li>
</ul>
</td>
<td>{{sharedetail.expenselist.value}}
<ul class="unstyled">
<li ng-repeat="expensevalue in expensevalues">
<span>{{expensevalue}}</span>
</li>
</ul></td>
<td>{{sharedetail.expense}}</td>
<td><ul class="unstyled">
<li ng-repeat="sharename in sharelistresult">
<span>{{sharename}}</span>
</li>
</ul></td>
<td>{{sharedetail.shareamount}}</td>
</tr>
I want the result like
我想要这样的结果
Event |Spent By |Spent Amounts|Totalamount |Shared By |No of shares|ShareAmount
--------------------------------------------------------------------------------------
treat |a |100 |500 |b |4 |125
|b |400 | |d
| | | |a
| | | |e
--------------------------------------------------------------------------------
lunch |c |500 |500 |b |5 |100
|c
|d
|a
|e |
I have edited my code.In this,I get the required result only when there is a single row.
我已经编辑了我的代码。在这里,只有当有一行时,我才能得到所需的结果。
When there are multiple entries(event),I get
当有多个条目(事件)时,我得到
Error: [ngRepeat:dupes] http://errors.angularjs.org/1.2.6/ngRepeat/dupes?p0=spent%20in%20spentpeople&p1=string%3A
How to display expense list array with "text" seperately and "value" seperately. Please advice
如何分别显示带有“文本”和“值”的费用列表数组。请指教
回答by mainguy
You dont need to do all this pushing if you allready have a json array. Just asign it to the scope and render your table like this:
如果您已经有一个 json 数组,则不需要进行所有这些推送。只需将其分配给范围并像这样呈现您的表格:
<tr ng-repeat="sharedetail in sharedetails">
<td>{{sharedetail.event}}</td>
<td>
<li ng-repeat="item in sharedetail.expenselist">
{{item.text}}
</li>
</td>
<td>
<li ng-repeat="item in sharedetail.expenselist">
{{item.value}}
</li>
</td>
<td>{{sharedetail.expense}}</td>
</td>
<td>
<li ng-repeat="item in sharedetail.sharelist">
{{item.text}}
</li>
</td>
<td>{{sharedetail.shareno}}</td>
<td>{{sharedetail.shareamount}}</td>
</tr>
Look at this plunker here: Plunker
看看这里的 plunker:Plunker
Don't forget to setup the controller in the table tag.
不要忘记在 table 标签中设置控制器。