3

I'm using angularjs and I have data like this :

$scope.users = [{
name: "Pratik"
queue: [{number: "199"},{number: "111"}],
status: "OK"
}]

My view :

   .available{
    background-color: #00226f;
    color: #f8f8f8;
    }

 <div class="col-xs-12 col-sm-3" ng-repeat="user in users">
    <span ng-class="{'available': user.queue[0].number == 111}" class="badges ">111</span>
</div>

My problem is that I want to assign the class "available" if the queue array in users contains the number "111" at any index. In the users array the number:"111" may appear at any index so in the view I can't always use user.queue[1].number == 111 or user.queue[1].number == 111 to assign the class. I want a solution which will check if the queue array contains number:"111" and assign the class of available accordingly. I tried to do it like this : ng-class="{'available': user.queue[i].number == 111}" but it's not working. How do I do it?

This my current workaround to apply the class:

ng-class="{'available': user.queue[0].number == 111 ||  user.queue[1].number == 111}"
3
  • Don't you have to use ng-repeat als to queue??? Commented Nov 19, 2018 at 6:58
  • but I think that will also repeat the <span> Commented Nov 19, 2018 at 7:01
  • @pravinnavle check the updated answer Commented Nov 19, 2018 at 7:13

3 Answers 3

1

try below code snippet

can achieve by using lodash js also using _.filter

_.filter(array, { 'number': '111' } )

Ref: https://lodash.com/docs/4.17.11#find

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

$scope.findObjectByKey = function(array, key, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][key] === value) {
            return array[i];
        }
    }
    return null;
};

$scope.users = [{
    name: "Pratik",
    queue: [{number: "199"},{number: "666"}],
    status: "OK"
  },
  {
    name: "Pratik 2",
    queue: [{number: "111"},{number: "555"}],
    status: "OK 2"
  },
  {
    name: "Pratik 3",
    queue: [{number: "999"},{number: "888"}],
    status: "OK 3"
  }
  ];
  
  $scope.searcInArray =  function(array){
   // var obj = $scope.findObjectByKey(array, 'number', '111');
   
   // using loadsh
   var obj = _.filter(array, { 'number': '111' }  );
   return obj.length > 0;
  };



}
.available{
   background-color: #00226f;
   color: #f8f8f8;
}
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>


</head>
<body>
<div ng-app="myApp" ng-controller="MyCtrl">
  
 <div class="col-xs-12 col-sm-3" ng-repeat="user in users">
    <span ng-class="{'available' : searcInArray(user.queue)}" class="badges ">111</span>
 </div>

</div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

7 Comments

can you tell me a similar function to sort data if there's no array. I mean I want a functionality will let me sort values which are not array like this ('name' , 'Pratik') instead of (array, 'number', '111'). So that if the name is Pratik then I'll set some other class to that element
i would suggest to include & use lodash lodash.com for sorting searching etc.
can you help me with this
@pravinnavle updated my answer with lodash using _.filter method. so use _.filter(array, { 'number': '111' } ) instead findObjectByKey method.
thanks you so much. this is really helpful to optimise the code
|
1

You have to add use ng-repeat to loop all queue and find how has item.number == '111'

<div class="col-xs-12 col-sm-3" ng-repeat="user in users">
  <div ng-repeat="item in user.queue">
     <span ng-class="{'available': item.number == '111'}" class="badges ">111</span>
  </div>
</div>

NOTE!

if you want show only the item.number == '111' you can use ng-hide/show

Comments

1

To check that condition where 111 appears at any index the easy fix could be to call a function that will check that property in the array and return true or false based on the condition. Something like below:

var app = angular.module('myApp', []);
app.controller('MainCtrl', ['$scope', function($scope){
$scope.users = [{
    name: "Pratik",
    queue: [{
      number: "111"
    }, {
      number: "119"
    }],
    status: "OK"
  },
  {
    name: "Pratik123",
    queue: [{
      number: "199"
    }, {
      number: "185"
    }],
    status: "OK"
  }];
  $scope.checkQueue = function(queue){
    return queue.find(({number})=>number === '111');
  }
}]);
.available {
  background-color: #00226f;
  color: #f8f8f8;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MainCtrl">
  <div class="col-xs-12 col-sm-3" ng-repeat="user in users">
    <span ng-class="{'available': checkQueue(user.queue)}" class="badges ">{{user.name}}</span>
  </div>
</div>

2 Comments

it's working fine the way I have implemented it. This is my current workaround ng-class="{'available111': user.queue[0].number == 111 || user.queue[1].number == 111}"
@pravinnavle glad to help. You can tick mark the answer if it was helpful to you :)

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.