Last active
August 29, 2015 14:25
-
-
Save arlago/02ab99687de4f6016b40 to your computer and use it in GitHub Desktop.
Angular custom input filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html ng-app="asdf"> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> | |
<script type="text/javascript"> | |
angular.module('asdf', []).controller('fdsa', function($scope) { | |
}).filter('beginningWith', function() { | |
return function(array, query) { | |
if(undefined === query || 0 === query.length) { | |
return array; | |
} | |
var retorno = []; | |
var regex = new RegExp('^' + query, 'i'); | |
array.forEach(function(element, index) { | |
if(element.name.match(regex)) { | |
retorno.push(element); | |
} | |
}); | |
return retorno; | |
}; | |
}); | |
</script> | |
<title></title> | |
</head> | |
<body ng-controller="fdsa"> | |
<div ng-init="friends = [{name:'John', phone:'555-1276'}, | |
{name:'Mary', phone:'800-BIG-MARY'}, | |
{name:'Mike', phone:'555-4321'}, | |
{name:'Adam', phone:'555-5678'}, | |
{name:'Julie', phone:'555-8765'}, | |
{name:'Juliette', phone:'555-5678'}]"></div> | |
<label>Search: <input ng-model="searchText"></label> | |
<table id="searchTextResults"> | |
<tr><th>Name</th><th>Phone</th></tr> | |
<tr ng-repeat="friend in friends | beginningWith:searchText"> | |
<td>{{friend.name}}</td> | |
<td>{{friend.phone}}</td> | |
</tr> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment