a8m / angular-filter

Bunch of useful filters for AngularJS (with no external dependencies!)
https://github.com/a8m/angular-filter
MIT License
2.93k stars 331 forks source link

Using filters programatically #67

Open kwab opened 9 years ago

kwab commented 9 years ago

What is the syntax to use the 'filterBy' filter in JS code?

mallowigi commented 9 years ago
.controller(['groupByFilter', function(groupByFilter) {
  $scope.groups = groupByFilter(collection);
}]);
kwab commented 9 years ago

Thank you. How do you specify the filtering criterion? Eg. Angular $filter would be:

/* return collection members that contain a property whose value is 'Adam'. */ $scope.groups = $filter('filter')(collection, {name:'Adam'});

I read the relevant section in the wiki but I can't seem to figure out how to translate the HTML usage to JS usage.

Thanks again.

On Dec 31, 2014, at 5:16 AM, Elior Boukhobza notifications@github.com wrote:

.controller(['groupByFilter', function(groupByFilter) { $scope.groups = groupByFilter(collection); }]); — Reply to this email directly or view it on GitHub.

a8m commented 9 years ago

@kwab here's a quick example, see jsbin Thanks for your ongoing contribution @mallowigi

kwab commented 9 years ago

Awesome. Thanks. How about adding the JS code examples to the README. Eg:

filterBy

Filter a collection by a specific property.
Usage: collection | filterBy: [prop, nested.prop, etc..]: search
Note: You can even use compound properties (e.g: |filterBy: [property + property]: model)

$scope.users = [
  { id: 1, user: { first_name: 'Rob', last_name: 'John',  mobile: 4444 } },
  { id: 2, user: { first_name: 'John', last_name: 'Wayne',  mobile: 3333 } },
  { id: 3, user: { first_name: 'Rob', last_name: 'Johansson',  mobile: 2222 } },
  { id: 4, user: { first_name: 'Mike', last_name: 'Terry',  mobile: 1111 } }
];

Return users whose id is 1

<!--search only by id -->
<th ng-repeat="user in users | filterBy: ['id']: 1">
  {{ user.id }} : {{ user.first_name }} {{ user.last_name }}
</th>

//JS
$scope.filteredUsers = filterByFilter($scope.users, ['user.id'], 1);

<!--result:
  1: Rob John
-->
kvetis commented 9 years ago

@kwab That would seem ubiquitous to me -- it is by default in angular that you can inject $filter and then use it like this $filter('filterBy')(collection, firstParam, secondParam) which is equivalent for collection | filterBy:firstParam:secondParam angular expression. It works with any filter like that.

kwab commented 9 years ago

Thanks Martin.

On Feb 4, 2015, at 4:58 AM, Martin Večeřa notifications@github.com wrote:

@kwab That would seem ubiquitous to me -- it is by default in angular that you can inject $filter and then use it like this $filter('filterBy')(collection, firstParam, secondParam) which is equivalent for collection | filterBy:firstParam:secondParam angular expression. It works with any filter like that.

— Reply to this email directly or view it on GitHub.