1

I'm pushing two times different data in my array :

$scope.numtickets.push({nbuser:data.users[i].name});

so i retrieve data in my view by doing this :

<ul ng-repeat="user in numtickets track by $index">
        <li>{{user.nbuser}}</li>
        <li>{{user.nbticket}}</li>
</ul>

and few lines after:

$scope.numtickets.push({nbticket:data.tickets.length});

and this display me this:

enter image description here

and what i want is to alternate the name and my number. so i should have : Claire pagniez 1 Michel Polnaref 1 Mathilde zimmer 3

and here is what my array display in my console:

enter image description here

If you see my all code, you can see that i have no choice to push first all names and then all my ticketnumber. So i need to sort elements by alternate name and ticket. So here is my all code from my controller:

$scope.displayuser = function(id) {
  var token = "xxxxxxxx";
  userdisplay
    .send(token, id)
    .then(function(data) {
      console.log(data);
      $scope.userbyorga = data.users;
      $scope.numtickets = [];

      for (i = 0; i < data.users.length; i++) {
        var userid = data.users;
        $scope.numtickets.push({
          nbuser: data.users[i].name
        });
        var userarray = JSON.stringify(userid);
        localStorage.setItem("myid", userarray);
      }
    })
    .then(function() {
      var tabuser = JSON.parse(localStorage.getItem("myid"));
      var urls = [];

      for (i = 0; i < tabuser.length; i++) {
        urls.push({
          url: JSON.stringify("https://cubber.zendesk.com/api/v2/users/" + tabuser[i].id + "/tickets/requested.json")
        });

        displayfilter
          .user(token, tabuser[i].id)
          .then(function(data) {
            $scope.numtickets.push({
              nbticket: data.tickets.length
            });
          });
      }
    });
}
4
  • Have you got a good reason not to simply push an item with both properties in your array? Commented Apr 25, 2016 at 8:29
  • Yes, it is not the same request i send to the API to retrieve datas Commented Apr 25, 2016 at 8:30
  • And can't you wait for the last request to success before you insert the completed object into the array? Commented Apr 25, 2016 at 8:33
  • if you show me how, i can do this, because how can i access to my value data.users[i].name at the end of my last request ? Commented Apr 25, 2016 at 8:36

3 Answers 3

2

use user id nested data. at first request create object for each user

        for(i = 0; i < data.users.length; i++){

            var userid = data.users;
            $scope.numtickets[userid] = {nbuser:data.users[i].name}
            var userarray = JSON.stringify(userid);
            localStorage.setItem("myid",userarray);
        }

at second request push nbticket by user id

       for(i = 0; i < tabuser.length; i++){
            urls.push({
                url:JSON.stringify("https://cubber.zendesk.com/api/v2/users/"+tabuser[i].id+"/tickets/requested.json")
            });
            console.log(urls);
            displayfilter
                .user(token,tabuser[i].id)
                .then(function(data){
                    $scope.numtickets[tabuser[i].id]['nbticket'] = data.tickets.length
                    console.log($scope.numtickets);

                })
        }

you will get object like

  {
     1: {
        'nbuser': 'User name1',
        'nbticket': '1',
      },
     2: {
        'nbuser': 'User name2',
        'nbticket': '2',
     }
     3: {
       'nbuser': 'User name3',
       'nbticket': '2',
      }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You help me so much ! just one thing, on this line $scope.numtickets[tabuser[i].id]['nbticket'] = data.tickets.length, it's saying that " TypeError: Cannot read property 'id' of undefined", it can't recognize my array, and i can't manage why can you help me ?
1

Only if the number of nbuser is equal to nbtickets & they are in same order try doing this when you push the nbtickets :

$scope.numtickets.push({nbuser:data.users[i].name});

then in the second request do this

for(var i = 0; i < $scope.numtickets.length; i++){ $scope.numtickets[i].nbticket = data[i].tickets.length; }

and then for HTML you can iterate over the $scope.numtickets

<li ng-repeat="item in numtickets track by $index" ng-click="displayuser(item.id)"> {{item.nbuser}} <br><br> {{item.name}} <br><br> </li>

Cheers

Comments

0
cols=_.groupBy(usertab, function(item){ return if(item.nbuser); });
usertab = cols[0];
usernum = cols[1];

<li ng-repeat="item in usertab track by $index" ng-click="displayuser(item.id)">{{item.name}}  {{usernum[$index].name}}</li>

Comments

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.