basil basil - 3 months ago 8
Javascript Question

drop down filter not working in angularjs

I am trying to develop angularjs filter but I can't get result in the code below to show only one scope filter. How can I fix it? How to filter using drop down box value . it filter two type like select box filter and text box value filter

<html >
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<title>search</title>
</head>
<body>
<div ng-app="app" ng-controller="mainCtrl">
<div>Search by Name: <input type="text" ng-model="filters.search"></div>


<div>
<select ng-model="selectedCompany" >
<option ng-repeat="xi in array" value="{{xi.company}}">{{xi.company}}</option>
</select>

</div>

<div ng-repeat="arr in array | filter:filters.search | filter:{company: filters.company}">
<span ng-bind="arr.name"></span>
</div>
<h1>You selected: {{selectedCompany}}</h1>
</div>

<script type="text/javascript" src="js/angular.min.js"></script>
<script src="js/ui-bootstrap-tpls-0.9.0.js"></script>
<script type="text/javascript">
var app = angular.module('app', []);
app.controller('mainCtrl', function($scope) {

$scope.filters = {
x: false,
company: '',
search: ''
};

$scope.actions = {
updateyCompany: function () {
if($scope.filters.y) {
$scope.filters.company = 'y';
} 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'}
];
});
</script>


</body>
</html>


Thank you @basil

Answer

Just make below change and it should work (you have bind your select to "selectedCompany" so will have to use that in your filter as well):

 <div ng-repeat="arr in array | filter:filters.search | filter:{company: selectedCompany}">
           <span ng-bind="arr.name"></span>
       </div>