0

I'm trying to create checkbox for ng-repeat list. I'm facing some issues.

HTML

<input type="text" ng-model="findname" placeholder="Search Name"  />
<ul>
    <li class="no-decoration" ng-repeat="tech in technologyArray">
    <label>
        <input type="checkbox" ng-model="tech.on" />{{tech.u_proffession}}
    </label>
    {{tech.on}}
    </li>
</ul>
<ul>
    <li  ng-repeat="stu in students  | filter:findname | filter:myFunc ">                
        <strong>Name :{{stu.u_name}}</strong><br />
    </li>
</ul>

JS

var ngApp = angular.module('angapp',[]);
ngApp.controller('angCtrl',function($scope,$http){
    $scope.myFunc = function(a) {
    for(tech in $scope.technologyArray){
            var t = $scope.technologyArray[tech];
            if(t.on && a.technology.indexOf(t.u_proffession) > -1){
                return true;   
            }               
        }
    };
    $scope.technologyArray = [{ u_proffession: "developer", on: false}, {u_proffession:"driver", on:false}];
    $http.get("data.php").then(function(response) {
        $scope.students= response.data.records
    });
});

JSON ARRAY

{"records":[{"u_name":"adrian","u_mail":"[email protected]","u_proffession":"developer"},{...}]}

1000 Rows

The simple search ng-model="findname" is working fine when i remove the checkbox filter inside the ng-list | filter:myFunc. But when i add both of them in ng-list then no data showing in student list and also text search does not working. I wanted to use both of them.

Can anyone guide me where i'm wrong that i can fix my issue. I would like to appreciate if someone guide me. Thank You.

1
  • filter:myFunc is a function shouldn't it be like this way filter:myFunc() ? Commented Jul 27, 2016 at 14:47

1 Answer 1

2

I am definitely not a fan of putting filter functions in controllers. In that vein, I would recommend writing an actual filter.

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.technologyArray = [{
      u_proffession: "developer",
      on: false
    }, {
      u_proffession: "driver",
      on: false
    }];
    $scope.students = [{
      "u_name": "adrian",
      "u_mail": "[email protected]",
      "u_proffession": "developer"
    }, {
      "u_name": "adam",
      "u_mail": "[email protected]",
      "u_proffession": "driver"
    }, {
      "u_name": "alex",
      "u_mail": "[email protected]",
      "u_proffession": "developer"
    }, {
      "u_name": "allen",
      "u_mail": "[email protected]",
      "u_proffession": "driver"
    }];
  })
  .filter('customFilter', function() {
    return function(input, techs) {
      if(!techs || techs.length === 0) return input;
      var out = [];
      angular.forEach(input, function(item) {
        angular.forEach(techs, function(tech) {
          if (item.u_proffession === tech.u_proffession) {
            out.push(item);
          }
        });
      });
      return out;
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <input type="text" ng-model="findname" placeholder="Search Name">
  <ul>
    <li ng-repeat="tech in technologyArray">
      <label>
        <input type="checkbox" ng-model="tech.on">{{tech.u_proffession}}
      </label>
    </li>
  </ul>
  <ul>
    <li ng-repeat="stu in students | filter:{u_name: findname} | customFilter:(technologyArray|filter:{on:true})">
      <strong>Name :{{stu.u_name}}</strong> ({{stu.u_proffession}})
    </li>
  </ul>
</div>

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

6 Comments

Its working fine as i wanted only one thing wanted to ask that currently data is showing only when i filter by checkbox otherwise nothing to show. So is that possible to show all data on page load.
I updated the snippet. To show all items on page load just make the check boxes checked by default.
Now its showing only filtered record, i'm asking about all records, Also search filter does not working.
I wanted to show all the records on page load that i can filter the data from the records other wise page will be blank.
I'm not sure I'm understanding, but I've modified the way the filter works. Only those check boxes that are checked will be passed to the custom filter. If no check boxes are passed then nothing is filtered by the custom filter. The search filter is working fine when I run the code snippet so I'm not sure why it's not working when you do.
|

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.