4

I have an array like-:

aw_score_list = {
    '6':99,'5.5':98,'5':93,'4.5':80,'4':56,'3.5':38,'3':15,'2.5':7,'2':2,'1.5':1,'1':1,
};  

I want to convert this to html table so it will become like

keys     Values
   6         99
 5.5         98 

... and so on

please advise me how to set a for loop for it

1
  • use ng-repeat . ng-repeat="(key, val) in aw_score_list Commented Jun 18, 2015 at 5:42

2 Answers 2

4

See ngRepeat - Iterating over object properties.

Assuming your array is in scope for the template...

<table>
<thead>
<tr>
    <th>keys</th>
    <th>Values</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, val) in aw_score_list">
    <td>{{key}}</td>
    <td>{{val}}</td>
</tr>
</tbody>
</table>
Sign up to request clarification or add additional context in comments.

5 Comments

it is not working . let me tell you i using your code in my app like <div ng-app="gre_angu_app" class="form-group" ng-controller="gre_angu_ctrl"> <!-- angular table code-->.. </div>
@vipulsharma as I mentioned in my answer, I assumed you'd have something like $scope.aw_score_list = {...} in your controller
oh how can i get them in desc order ?? :(
@vipulsharma Get what in descending order? The keys or values? See the other answer for a possible solution
with respect to keys desc order
1

That is possible, but the order will be messed up, if you want to preserve order, you need something like this:

aw_score_list_preserve_order = [    
    {key:'6'   , value:99},
    {key:'5.5' , value:98},
    {key:'5'   , value:93},
    {key:'4.5' , value:80},
    {key:'4'   , value:56},
    {key:'3.5' , value:38},
    {key:'3'   , value:15},
    {key:'2.5' , value:7},
    {key:'2'   , value:2},
    {key:'1.5' , value:1},
    {key:'1'   , value:1},
  ]

this is pretty basic ng-repeat iteration, you should probably check out the angular documentation.

Plunker

4 Comments

If u want it descending ordered, u can simply do: <div ng-repeat="item in aw_score_list_preserve_order | orderBy:key:true">...</div>, if you want ascending order, take out the :true part, check documentation for orderBy.
Check the plunker again, I have renew my code. You probably need your keys to be number not string to get the result you want.
i can't change array sturcture .. i m following the @phil way . which is working too the only issue remain for set order by desc w.r.t. keys
You can change the array structure in your code, like so plunker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.