Click here to Skip to main content
15,888,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings ...
I have a folowing in my $scope.SelectedData[]


JavaScript
[{"groupGuid":"d20ab30b-977e-4641-affb-878b45d753da","groupName":"Kicked","Start":8000,"End":8999,"assignedToUserName":"Admin","errors":{"assignedToUserName":null}}]






Now i want to filter array data and get only these values :


groupName, Start, assignedToUserName and Stop ...


And add these to ng-grid in later fase ....

This should be done only in Controller no search box in view.


How to do that?


Thnx for helping me out!

What I have tried:

Wiki , forums , angularJS site etc...................
Posted
Updated 18-Apr-16 22:55pm
v4

1 solution

The module you need is $filter. It's in the native angular so you only need to include it in your controller inserts:
JavaScript
myApp.controller('myController',['$scope','$filter', function($scope,$filter){
   //...
}]);


It's use is well documented. It's kinda like the model filter (item in items | filter:{Id:20})

JavaScript
//...
   var myFilteredItems = $filter('filter')(myItems,{"groupGuid":"d20ab30b-977e-4641-affb-878b45d753da","groupName":"Kicked","Start":8000,"End":8999,"assignedToUserName":"Admin","errors":{"assignedToUserName":null}});
//...


The filter can be as simple as you like:{groupGuid:"d20ab30b-977e-4641-affb-878b45d753da"}, or as complex as you have it. Any field in the myItems object that is not included in the filter object will not be filtered on. cool eh ^_^


Advanced usage:

The reason you use $filter('filter') is because you are using the default angular filter which take a filter object as a parameter. You can create your own filters too:
JavaScript
myApp.filter('getMyItem', ["$filter", function ($filter) { //define the filter
    return function (data, groupId,start, end /*etc...*/) { //the actual filter
        var filterObj =  {
            groupId: groupId,
            start:start,
            end:end };
        var found = $filter('filter')(data, filterObj, true); //perform filtering however you like
        if (!found || !found.length) //return however you like
            return null;
        return found;
    }
}]);


Then use that instead of 'filter':
JavaScript
var myFilteredItems = $filter('getMyItem')(myItems, groupId, start, end);


These filters can also be used in model filtering

Let me know if you get stuck ^_^

Andy
 
Share this answer
 
Comments
C00d3 19-Apr-16 7:17am    
Hi Andy !


Great explanation but i have more issues with that i presume at myItems is my


$scope.Selections = [];


When i try to use a filter i trying to add a $scope.Selections = [];
on this way ...


var myFilteredItems = $filter('getMyItem')(Selections, groupId, start, end);



But i geting a undefined on Selections , i presume i using this in wrong way , please assist ...
Andy Lanng 19-Apr-16 9:13am    
well for one thing I can see that your variable is $scope.Selections, not Selections:
$filter('getMyItem')($filter.Selections, groupId, start, end)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900