Angular directive implements to custom filter concept is apply to filter large data array object will filter specific terms and condition write to yours. Angularjs have some basic filters directive is there. Some more filter need your project means, will try own filter concept is available from angularjs.
Custom filter is more reuse more project from this custom logic. This filter like a library, this write same file or external also.
I have sample array Object:
$scope.supplierList = [
{
"productname": "heliumcylinder",
"catagoriesName": "cylinder"
},
{
"productname": "boost",
"catagoriesName": "cylinder"
},
{
"productname": "test 3",
"catagoriesName": "cylinder"
},
{
"productname": "test1",
"catagoriesName": "cylinder"
},
{
"productname": "test5",
"catagoriesName": "cylinder"
},
{
"productname": "test6",
"catagoriesName": "gas"
},
{
"productname": "test6",
"catagoriesName": "test4"
},
{
"productname": "test16",
"catagoriesName": "test5"
},
{
"productname": "test10",
"catagoriesName": "test5"
},
{
"productname": "product1",
"catagoriesName": "test5"
}
];
HTML :
<div ng-app="repeatModule">
<div ng-controller="repeatController">
Supplier List {{supplierList}}
<hr/><br/>
First Filter {{catFilter}}
<hr/><br/>
Second Filter {{cat1Filter}}
</div>
</div>
Angular
app = angular.module("repeatModule", []);
app.controller("repeatController", function ($scope, $filter) {
$scope.supplierList = [
{
"productname": "heliumcylinder",
"catagoriesName": "cylinder"
},
{
"productname": "boost",
"catagoriesName": "cylinder"
},
{
"productname": "test 3",
"catagoriesName": "cylinder"
},
{
"productname": "test1",
"catagoriesName": "cylinder"
},
{
"productname": "test5",
"catagoriesName": "cylinder"
},
{
"productname": "test6",
"catagoriesName": "gas"
},
{
"productname": "test6",
"catagoriesName": "test4"
},
{
"productname": "test16",
"catagoriesName": "test5"
},
{
"productname": "test10",
"catagoriesName": "test5"
},
{
"productname": "product1",
"catagoriesName": "test5"
}
];
angular.forEach($scope.supplierList, function(val, key){if(val.catagoriesName=="cylinder"){} });
$scope.catFilter = $filter('unique')($scope.supplierList,'catagoriesName');
$scope.cat1Filter = $filter('filter')($scope.supplierList, function(val, key){if(val.catagoriesName=="cylinder"){
console.log(val.productname);
return val;
}
},true)
console.log($scope.cat1);
});
app.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
});
Result:
Comments
Post a Comment