AngularJs Search with drop down filter - angularjs

How would you go about creating a refined search? With the code below, the filter acts on both of the columns. For example, if you type in John, you will get both John Smith & Smith John. I want to use a dropdown as a filter by. I looked at other posts which mentioned filter: object.value or something.
I think I need to set the dropdown to a value which is then fed to the text box and filtered to the table.
HTML
<div ng-app="searchApp" ng-controller="searchCtrl">
<br>
<br>
<input type="text" ng-model="searchData">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<select>
<option value="">FilterBy</option>
<option value="">FirstName</option>
<option value="">LastName</option>
</select>
<tr ng-repeat="name in names | filter:searchData">
<td>{{name.firstName}}</td>
<td>{{name.lastName}}</td>
</tr>
</tbody>
</table>
</div>
<script src=" search.js " type="text/javascript "></script>
.JS
var searchApp = angular.module('searchApp', []);
searchApp.controller('searchCtrl', function($scope) {
$scope.names = [
{"firstName":"John","lastName":"Smith"},
{"firstName":"Smith","lastName":"John"},
{"firstName":"John","lastName":"Doe"},
{"firstName":"Doe","lastName":"Jane"}
];
});

I added an ng-change to the filter drop down and set the filter object on that function call.
<select ng-model="by" ng-change="filter(by)">
<option value="">FilterBy</option>
<option value="firstName">FirstName</option>
<option value="lastName">LastName</option>
</select>
$scope.filter = function(by){
if(by){
$scope.filterData = { };
$scope.filterData[by] = $scope.searchData;
}else{
$scope.filterData = {};
}
};
Here is a link to a working example http://codepen.io/mkl/pen/GqpxGZ

Related

How to show all values when AngularJS strict filter field is empty?

I have a table which can be filtered by several columns. The filter is strict. When the data is initiated, it shows all values. However, after filtering by some column and getting back to empty option to show all values, it shows an empty table. How to apply a strict filter only to non-empty values?
<select
ng-model="search.column1">
<option value=''></option>
<option ng-repeat="e in vm.getExample value="{{e.column1}}">{{e.column1}}</option>
</select>
<select
ng-model="search.column2">
<option value=''></option>
<option ng-repeat="e in vm.getExample value="{{e.column2}}">{{e.column2}}</option>
</select>
<table>
<tr>
<th>Column1</th>
<th>Column2</th>
</tr>
<tr ng-repeat="e in vm.getExample | filter:search:true">
<td>{{ e.column1 }}</td>
<td>{{ e.column2 }}</td>
<tr>
</table>
Your issue was in the use of filter:
You should change filter:search:true to filter:search.
The third parameter the filter is a comparator. In this case, you don't need it:
Comparator which is used in determining if values retrieved using
expression (when it is not a function) should be considered a match
based on the expected value (from the filter expression) and actual
value (from the object in the array).
If you want to learn more a bit about comparators, I recommand you to read this excellent answer (SO).
Working snippet of your code:
var app = angular.module('testApp', []);
app.controller('testController', ['$scope', function($scope) {
this.getExample = [{
'column1': 1,
'column2': 2
}, {
'column1': 3,
'column2': 4
}, {
'column1': 5,
'column2': 6
}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testController as vm">
<select ng-model="search.column1">
<option value=''></option>
<option ng-repeat="e in vm.getExample" value="{{e.column1}}">{{e.column1}}</option>
</select>
<select ng-model="search.column2">
<option value=''></option>
<option ng-repeat="e in vm.getExample" value="{{e.column2}}">{{e.column2}}</option>
</select>
<table>
<tr>
<th>Column1</th>
<th>Column2</th>
</tr>
<tr ng-repeat="e in vm.getExample | filter:search">
<td>{{ e.column1 }}</td>
<td>{{ e.column2 }}</td>
<tr>
</table>
</div>
Created custom filter in controller:
$scope.filterMyData = function (input, searchParam) {
if (searchParam == '')
return true;
return angular.equals(input, searchParam);
}
And changed to:
<tr ng-repeat="e in vm.getExample | filter:search:filterMyData">

dirPagination numbers not change when ng-model search put something

At first, I'm using dirPagination. The problem is that when I search something, it's filtered correctly, but pagination number not change and also show all last number in pagination without any change and I see some page is empty because it does filter but page number is show.
<div data-ng-controller='productsController'>
<form `class='form-inline'>
<div class='form-group'>
<label>search</label>
<input type='text' data-ng-model='search' class='form-control' placeholder='search' />
</div>
</form>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>product</th>
<th>imet</th>
</tr>
</thead>
<tbody>
<tr dir-paginate='product in products|itemsPerPage:12|filter:search|orderBy:sortKey:reverse'>
<td>{{product.id}}</td>
<td>{{product.productName}}</td>
<td>{{product.time}}</td>
</tr>
</tbody>
</table>
<dir-pagination-controls max-size='10' direction-links='true' boundary-links='true' >
</dir-pagination-controls>
<script>
(function(){
var app = angular.module('products', ['angularUtils.directives.dirPagination']);
app.controller('productsController', function($scope, $http){
$scope.products = [];
$http.get('/products/json').success(function(data){
$scope.products= data;
});
});
})();
</script>
</div>
itemsPerPage must be the last filter, as below:
<tr dir-paginate='product in products | filter: search | orderBy: sortKey: reverse | itemsPerPage: 12'>
For more explanation, check it on FAQ on michaelbromley/angularUtils/.

Set dropdown based on $scope match

I have a table that displays two columns using ng-repeat.
First column is a list of items pulled from one source.
Second column is a select element pulled from a second list.
What I want to do is automatically select an option in the dropdown, if it matches what is displayed in the left column.
I put together a fiddle: https://jsfiddle.net/raz1j9rt/1/
Here is my HTML:
<header>
<title>myTittle</title>
</header>
<body ng-app='myApp' ng-controller='myController'>
<div class="col-md-10 col-md-offset-1">
<div>
<form>
<table class="table table-striped">
<thead>
<th>From File</th>
<th>Map To</th>
</thead>
<tr class="selector-row" ng-repeat="(key,value) in List1">
<td><span id="myspan">{{value}}</span>
</td>
<td style="padding:10px;">
<select name="repeatSelect" id="repeatSelect" ng-model="data[value]" class="form-control">
<option ng-repeat="(key,value) in Match" value="{{value}}">{{value}}</option>
</select>
</td>
</tr>
</table>
</form>{{ data }}</div>
</div>
</body>
Here is my JS:
var app = angular.module('myApp', [])
app.controller('myController', ['$scope', function ($scope) {
$scope.data = {};
$scope.List1 = ['product1', 'product2', 'product3', 'product4', 'product5'];
$scope.Match = ['product1', 'somethtingElse1', 'product3', 'somethingElse2', 'product5'];
}])
Not really sure where to start on this...
Expanding on Claies point, here's a working fiddle.
https://jsfiddle.net/dboskovic/raz1j9rt/2/
Add this to your controller.
// setup default matches
angular.forEach($scope.List1, function(v){
$scope.data[v] = $scope.Match.filter(function(d){
if(d === v) return true;
return false;
})[0];
})
And then change your select to:
<select name="repeatSelect" id="repeatSelect" ng-model="data[value]" class="form-control" ng-options="x for x in Match"></select>

Submitting a dynamic form in angularjs

I'm trying to create a function where I build a column from an array that list products. Then a second column that contains a select list of options I want to pair the second column with.
The end result will be an ajax call to a server that will insert each matched pair.
I created an example on JS Fiddl: https://jsfiddle.net/wnzj6wda/2/
Here is the code:
<html>
<header>
<title>myTittle</title>
</header>
<body ng-app='myApp' ng-controller='myController'>
<div class="col-md-10 col-md-offset-1">
<div>
<form>
<table class="table table-striped">
<thead>
<th>From File</th>
<th>Map To</th>
</thead>
<tr class="selector-row" ng-repeat="(key,value) in List1">
<td><span id="myspan">{{value}}</span>
</td>
<td style="padding:10px;">
<select name="repeatSelect" id="repeatSelect" ng-model="data" class="form-control">
<option ng-repeat="(key,value) in Match" value="{{value}}">{{value}}</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<button ng-click="SubmitMatched()">Show matched</button>
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
<script>
var app = angular.module('myApp', [])
app.controller('myController', ['$scope', function($scope) {
$scope.List1 = ['product1', 'product2', 'product3', 'product4', 'product5'];
$scope.Match = ['match1', 'match2', 'match3', 'match4', 'match5'];
$scope.SubmitMatched = function() {
//I want to be able to submit results to be added to a database, where I can pass the coloms that match to the selected.
//Example:
// results = [{'product1':'match4','product2':'match5','product3':'match1','product4':'match3','product5':'match2'}]
}
}])
</script>
</html>
Here is the updated demo https://jsfiddle.net/wnzj6wda/3/
You have to make ng-model dynamic so that finally you will get that object with data as
in js
$scope.data ={};
in html
<select name="repeatSelect" id="repeatSelect" ng-model="data[value]" class="form-control">
<option ng-repeat="(key,value) in Match" value="{{value}}">{{value}}</option>
</select>
Hope this will help you

Angularjs - how to use select with ng-repeat?

In the view available below I'm trying to select a value in the drop down box based on a key(colorId) available in the current ng-repeat object(user). Does anyone know how to do that?
<div ng-app>
<div ng-controller="MyCntrl">
<table>
<thead >
<tr>
<th width="50%">User</th>
<th width="50%" >Color</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td width="50%">{{user.name}}</td>
<td width="50%">
<select ng-model="user.colorid" ng-options="color.name for color in colors">
<option value="">Select color</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
</div>
The controller code is:
'use strict';
angular.module('nes',)
.controller('MyCntrl',['$scope',function ($scope) {
$scope.colors = [
{id:'1', name:'blue'},
{id:'2', name:'white'},
{id:'2', name:'red'}
];
$scope.users = [
{ name:'user1',colorId:'1'},
{ name:'user2',colorId:'2'}
];
}]);
You need to fix a few things in your <select> element:
use color.id as color.name in your ng-options.
ng-options="color.id as color.name for color in colors"
you typoed "colorid":
ng-model="user.colorId"
Here's a plunk of it working: http://plnkr.co/edit/DptvZuamWv9waFzI0Rcp?p=preview

Resources