0

I have a data array like below in the controller

$scope.sampleArray=[{
    'title': 'SampleTitleOne',
    'description': 'SampleDescriptionOne',
    'category': 'Saving'
},
{
    'title': 'SampleTitleTwo',
    'description': 'SampleDescriptionTwo',
    'category': 'Saving'
},
{
    'title': 'SampleTitle3',
    'description': 'SampleDescription3',
    'category': 'Current'
},
{
    'title': 'SampleTitleIV',
    'description': 'SampleDescriptionIV',
    'category': 'Current'
},
{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Group'
},
{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Group'
},
{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Mixed'
},
{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Other'
}];

I have order the above array based on user input. Suppose user input array is

$scope.order = ['Other', 'Group', 'Mixed', 'Saving', 'Current'];

After applying the filter the output should be like below

$scope.sampleArray=[{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Other'
},
{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Group'
},
{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Group'
},

{
    'title': 'SampleTitleFive',
    'description': 'SampleDescriptionFive',
    'category': 'Mixed'
},
{
    'title': 'SampleTitleOne',
    'description': 'SampleDescriptionOne',
    'category': 'Saving'
},
{
    'title': 'SampleTitleTwo',
    'description': 'SampleDescriptionTwo',
    'category': 'Saving'
},
{
    'title': 'SampleTitle3',
    'description': 'SampleDescription3',
    'category': 'Current'
},
{
    'title': 'SampleTitleIV',
    'description': 'SampleDescriptionIV',
    'category': 'Current'
}];

Is there any way to apply angular $filter to achieve this?

2
  • You want to order in ng-repeat or only in variable? Commented Feb 15, 2017 at 13:51
  • in the variable itself Commented Feb 15, 2017 at 13:51

2 Answers 2

1

Use the orderBy filter

$scope.sampleArray = $filter('orderBy')($scope.sampleArray, function(item) { return $scope.order.indexOf(item.category);});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!! How can I filter if I change the $scope.order variable something like $scope.order = ['Other', 'Group', 'Mixed']; in this case scenario, $scope.sampleArray should contain only 'other', 'group' and 'mixed' related records in the array
then add $scope.samleArray = $filter('filter')($scope.sampleArray,function(item) { return $scope.order.indexOf(item.category) !== -1; }) before the sorting
I would do something like $scope.originalData = [....]; $scope.$watchCollection('order', function(newOrder) { var filteredData = $filter('filter')($scope.originalData ,function(item) { return $scope.order.indexOf(item.category) !== -1; }); $scope.filteredOrderedData = $filter('orderBy')(filteredData, function(item) { return $scope.order.indexOf(item.category);}) })
0

You should check orderBy filter and somehow change something in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.