Awa Melvine Awa Melvine - 1 year ago 61
AngularJS Question

how to filter data on a table with angularjs

So, I have this table that displays users taken from a server in angularjs. Here is the users object:

'users'= [
"name":"Jane Doe",
"name":"John Doe",

Here is the table that displays this data:

<div class="col-md-4">
<h3>Filter by user role</h3>
<select class="form-control" ng-model="roleOrder">
ng-repeat="user in users"

<table class="table table-bordered">
<tr ng-repeat="user in users | filter:{role: roleOrder}">

The problem is, when the page loads, nothing gets displayed until I select a role from the dropdown to filter the

My goal is to have all the users displayed initially, and then to filter the users by their roles once their corresponding role is selected in the
option select

Is there a better way than the one I'm attempting? Or how do I do it right?
Thanks for any ideas

Answer Source

You can add function for it like here AngularJS custom filter function

In js file

$scope.predicate = function( roleOrder ) {
  return function( item ) {
    return !roleOrder || item.role === roleOrder;

And in html

 <tr ng-repeat="user in users | filter:predicate(roleOrder)">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download