2

I have an array of products and models that I'm using 'filter'

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {

  $scope.products = [{
      'id': 1,
      'category': 'Tv',
      'models': [{
          'modelno': 12,
          'modelname': 'ASF456'

        },
        {
          'modelno': 13,
          'modelname': 'Aip456'

        }
      ]
    },

    {
      'id': 2,
      'category': 'Mobile',
      'models': [{
          'modelno': 21,
          'modelname': 'FGH74'

        },
        {
          'modelno': 22,
          'modelname': 'UIO06'

        }
      ]

    }

  ];

  $scope.search = '';
  $scope.filterData = function() {
    return $scope.products.filter(function(item) {

      return (item.id.toString().indexOf($scope.search) > -1

        ||
        (item.category.toLowerCase().indexOf($scope.search)) > -1)



    });

  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
  <input type="text" ng-model="search">
  <h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>

to filter these products by id and category. The filter is working but i want to add one more field inside filter modelname .

How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?

How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?

i want to filter by id category ,modelname

now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem

4
  • What's wrong with extending your current approach? Commented Nov 22, 2018 at 14:12
  • nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl Commented Nov 22, 2018 at 14:18
  • Did you try just adding another ||? Commented Nov 22, 2018 at 14:21
  • we can't do like that bcoz again models is array @charlietfl Commented Nov 22, 2018 at 14:30

2 Answers 2

1

You can add another or condition like this

 || (item.models !== null && item.models.length > 0 && item.models.filter(e => {return e.modelname.search($scope.search) > 0 }).length > -1)
Sign up to request clarification or add additional context in comments.

5 Comments

can you run my code based on user enter value we need to filter
@komal I don't understand what do you want
based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
@komal can you test now?
@komal tried now on plunker and it works. I replaced it with -1
0

Do i understood correctly - you want to filter your $scope.products by means of id ? if so try like this:

<input ng-model="id " type="number" min="0"/>
<br />
<div ng-repeat="x in products | IDFiletr:id">
  {{x.category}}
  <br />
</div>

JS:

app.filter('IDFiletr',function(){
    return function(data, id) {
        if (!id) return data;
        var ids = [];
        angular.forEach(data, function(item){
            if(item.id === id) {
                ids.push(item);
            }
        });
        return ids;
    };
});

plunker: http://plnkr.co/edit/q9DsvZP4scA1oKsJj3Vu?p=preview

Comments

Your Answer

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