admin管理员组文章数量:1400195
I am building an application using Angular.js,Node.js and MongoDB.
I have got an array of data through ajax call and is as follows
[{
"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.
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>
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] .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
I am building an application using Angular.js,Node.js and MongoDB.
I have got an array of data through ajax call and is as follows
[{
"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.
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>
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/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
Share Improve this question edited Jan 27, 2014 at 13:11 user67867 asked Jan 27, 2014 at 10:49 user67867user67867 4214 gold badges11 silver badges24 bronze badges 4- Sorry, not sure what you want to get. Could you please provide an example of what shall be in result? – GRaAL Commented Jan 27, 2014 at 11:50
- Please refer my edits – user67867 Commented Jan 27, 2014 at 12:30
- Why are you pushing your objects one by one ? (Understand it as, you shouldn't be doing it) – Florian F. Commented Jan 27, 2014 at 12:36
- I have updated my latest code – user67867 Commented Jan 27, 2014 at 13:01
1 Answer
Reset to default 4You 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:
<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
Don't forget to setup the controller in the table tag.
本文标签: javascriptdisplaying an array data in a table using ngrepeat directive in Angular jsStack Overflow
版权声明:本文标题:javascript - displaying an array data in a table using ng-repeat directive in Angular js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744136386a2592416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论