0

I parsed three different json files using AngularJS. Here's my code:

Factory code

app.factory('myapp', ['$http', function($http) {        
    function getLists() {
        var tab = ['url1', 'url2', 'url3'];
        var list = [];
        for(i = 0; i < tab.length; i++) {
            $http.get(tab[i]) 
            .then(function(res) {
                list.push(res.data);
            });
        }
        return list;
    }

    return {
        getLists: getLists
    };
]);

What I want is to display the data of the different files in a , the data of the first url in the first line , the second one in the second line , etc... I have to display the data depending on the alphabetic order of the names 'nm'.

Html code:

<tr ng-repeat="d in list">
    <td>{{d.nm}}</td>
    <td>{{d.cty}}</td>   
    <td>{{d.hse}}</td>
    <td>{{d.yrs}}</td> 
</tr> 

What should I do?

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
nour nour
  • 85
  • 1
  • 2
  • 9
  • 1
    You may want to look into this popular question http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – elclanrs Jun 22 '16 at 16:05

2 Answers2

2

you can simply use orderBy

https://docs.angularjs.org/api/ng/filter/orderBy

like so

<tr ng-repeat="d in list | orderBy: 'nm'">
    <td>{{d.nm}}</td>
    <td>{{d.cty}}</td>   
    <td>{{d.hse}}</td>
    <td>{{d.yrs}}</td> 
</tr> 
WalksAway
  • 2,769
  • 2
  • 20
  • 42
0

You are not explaining where you are getting the list from, so I'll assume one axiom and two options:

Axiom: You don't need to sort on-line (meaning, the user cannot trigger a different sort.

Option 1 - You are getting the list from a DB: In this case, make sure that you add an index to each record and order the data by the index in ascending order.

Option 2 - You are populating the list within the JavaScript code: in this case, you can build the list in order and with the addition of an index (I would suggest you make it numerical and not textual).

FDavidov
  • 3,505
  • 6
  • 23
  • 59