【发布时间】:2014-01-27 10:49:24
【问题描述】:
我正在使用 Angular.js、Node.js 和 MongoDB 构建应用程序。
我通过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
}]
我将数据一一推送到数组$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:
<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>
我想要这样的结果
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 |
我已经编辑了我的代码。在此,只有当有一行时,我才能得到所需的结果。
当有多个条目(事件)时,我得到
Error: [ngRepeat:dupes] http://errors.angularjs.org/1.2.6/ngRepeat/dupes?p0=spent%20in%20spentpeople&p1=string%3A
如何分别显示“文本”和“值”的费用列表数组。 请指教
【问题讨论】:
-
抱歉,不确定您想要什么。您能否提供一个结果示例?
-
请参考我的编辑
-
你为什么要一个一个推你的对象? (理解为,你不应该这样做)
-
我更新了我的最新代码
标签: javascript arrays angularjs