Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table in which I am outputting many rows of data. In that table, I have a column status.

There are about 30 different status values. There is a dropdown for the column to filter by different statuses, but in the dropdown I only have these statuses: Any, Pending, Inactive, and Placed.

Each of the 4 statuses has many different status values.

If I create a JavaScript object like so:

var statuses = {
    'Pending': [
         'Entered',
         'Submitted,
         'Approved'
     ]
};

And after Pending there is another array with Inactive which has more statuses.

How can I create a filter in Angular to show the results not where the status from the dropdown is Pending but where the statuses are in the Pending array from above?

share|improve this question

2 Answers 2

In order to populate the table based on the selected element you have to get the value using ng-model in the select element. And to make sure the ng-model variable gets it each time a selection is made you can use ng-options to populate all the options from your Statuses. Such as:

<select ng-change='loadStats()' ng-model='selectedStatus' ng-options='k as k for (k,v) in stats'>
</select>

The ng-change is what triggers the event when a selection is made it calls the function from your controller's $scope. The string 'k as k for (k,v) in stats' is simply for handling your statuses object and reading it as (Key, Value) pairs then printing the key as Label.

The definition of the controller holds the loadStats() and other initializations of $scope variables:

function Ctrl($scope, $http) {
    $scope.stats = statuses;
    $scope.table = tableData;
    $scope.tableShow = $scope.table;
    $scope.selectedStatus = 'Pending';
    $scope.status = $scope.stats[$scope.selectedStatus];
    $scope.loadStats = function() {
        $scope.status = $scope.stats[$scope.selectedStatus];
    };

}

After the selection is made, to populate the table you need the filter to be defined. The filter depends on the array of the selected status and all the rows. For populating the table you can use an array of objects. This array can help us filter too. Suppose the table's data is represented like:

var tableData = [
{status: 'Entered', value: '1'},
{status: 'Entered', value: '2'},
{status: 'Submitted', value: '3'},
{status: 'Approved', value: '4'},
{status: 'A', value: '1'},
{status: 'B', value: '2'},
{status: 'C', value: '1'},
]

Now to populate the table we use the filter defined like:

var app = angular.module('myApp', []);
app.filter('filterMyStats',function(){
    return function (table, status) {
        return table.filter(function(row){
            if (status.indexOf(row.status) != -1) {
                return true;
            } else {
                return false;
            }

        })
    }
});

And we apply the filter using the ng-repeat:

<tr ng-repeat="tr in tableShow | filterMyStats:status">
    <td>{{tr.status}}</td><td>{{tr.value}}</td>
</tr>

Here's a fiddle

share|improve this answer

You can just access the "Pending" inside the statuses:

$scope.statuses = statuses;

And inside your select:

<select>
   <option ng-repeat="options in statuses.Pending">{{options}}</option>
</select>

JSFiddle

share|improve this answer
    
The status values are reflected in the table, and I'm trying to have the table show all the rows where the status equals one of the statuses belonging to the selected value from the dropdown. Is that clearer? User selects Inactive -> code checks all statuses under Inactive, shows rows where status -> inactive statuses. –  asgwar12 Oct 1 '14 at 20:57
    
They're not shown in the options. –  asgwar12 Oct 1 '14 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.