0

I'm New with angularJS and I just want to filter my data using textbox together with checkbox.

Here is my data:

  $scope.array = [
    {name: 'Tobias', lname: 'TLname', company: 'x'},
    {name: 'Jeff', lname: 'JLname', company: 'x'},
    {name: 'Brian', lname: 'BLname', company: 'x'},
    {name: 'Igor', lname: 'ILname', company: 'y'},
    {name: 'James', lname: 'JLname', company: 'z'},
    {name: 'Brad', lname: 'BLname', company: 'y'}
  ];

All I want is if I going to choose x from checkbox it will show the rows related with Tobias Jeff Brian and if I'm going to uncheck the checkbox and type br, Brian and Brad will show but if I'm going to check the x checkbox only Bryan should show.

Thanks in advance.

3 Answers 3

8

You would use filters to accomplish this.

See fiddle: http://jsfiddle.net/w4XqV/16/

Markup:

<div ng-app="app" ng-controller="mainCtrl">
    <div>Search by Name: <input type="text" ng-model="filters.search"></div>
       <div>Show only Company X: <input type="checkbox" ng-model="filters.x" ng-change="actions.updateCompany()"/>           
       </div>
       <div ng-repeat="arr in array | filter:filters.search | filter:{company: filters.company}">
           <span ng-bind="arr.name"></span>
       </div>
</div>

JavaScript:

var app = angular.module('app', []);
app.controller('mainCtrl', function($scope) {
    $scope.filters = {
        x: false,
        company: '',
        search: ''
    };

    $scope.actions = {
        updateCompany: function () {
            if($scope.filters.x) {
                $scope.filters.company = 'x';
            } else {
                 $scope.filters.company = '';   
            }
        }
    };


    $scope.array = [
    {name: 'Tobias', lname: 'TLname', company: 'x'},
    {name: 'Jeff', lname: 'JLname', company: 'x'},
    {name: 'Brian', lname: 'BLname', company: 'x'},
    {name: 'Igor', lname: 'ILname', company: 'y'},
    {name: 'James', lname: 'JLname', company: 'z'},
    {name: 'Brad', lname: 'BLname', company: 'y'}
  ];
});

Hope this helps.

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

3 Comments

Hi, Could it be possible if I check x and y it will show the all the related data on it? please see: jsfiddle.net/w4XqV/17
can somebody help me?
please help me how to show related records if I check x and y
0
<div ng-controller="ctrl2">
<div>
  <input type="text" ng-model="myfilter" placeholder="filter">
  <br>avilable:<input type="checkbox" ng-model="check" checked value="avilable">
  <br>
  <span ng-click="sortby('name')">name</span><span ng-click="sortby('country')">country</span>
</div>
<p></p>
<div class="row1" ng-repeat="x in names | orderBy:myorder | filter:check | filter:myfilter">
  <span class="row2">
  {{x.name}}
</span>
  <span class="row2">
  {{x.country}}
</span>
</div>
<h1>{{myfilter}}</h1>

Comments

0
app.controller('ctrl2', function ($scope) {
$scope.sortby = function (x) {
    $scope.myorder = x;
}

$scope.names = [
    { name: 'Jani', country: 'Norway', available: true },
    { name: 'Carl', country: 'Sweden', available: false },
    { name: 'Margareth', country: 'England', available: true },
    { name: 'Hege', country: 'Norway', available: true },
    { name: 'Joe', country: 'Denmark', available: false },
    { name: 'Gustav', country: 'Sweden', available: false },
    { name: 'Birgit', country: 'Denmark', available: false },
    { name: 'Mary', country: 'England', available: true },
    { name: 'Kai', country: 'Norway', available: false }
];

});

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.