{"id":602,"date":"2015-11-24T09:44:00","date_gmt":"2015-11-24T09:44:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=602"},"modified":"2025-02-20T07:39:55","modified_gmt":"2025-02-20T07:39:55","slug":"custom-filters-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/custom-filters-in-angularjs","title":{"rendered":"How to Create Custom Filters in AngularJS?"},"content":{"rendered":"\n<p>Have you ever used filters with the ng-repeat directive as shown in the listing below?<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div ng-controller=\"ProductController\">\n     &lt;table class=\"table\">              \n         &lt;tr ng-repeat=\"a in products|filter:searchTerm\">\n             &lt;td>{{a.name}}&lt;\/td>\n             &lt;td>{{a.price}}&lt;\/td>\n         &lt;\/tr>\n     &lt;\/table>\n&lt;\/div><\/pre>\n\n\n\n<p>If so, then you\u2019ve used a filter in an AngularJS application. AngularJS provides us many in-built directives like search. If required, AngularJS also allows us to create custom filters, which we\u2019ll explore in this post.<\/p>\n\n\n\n<p>AngularJS gives us a simple API to create a custom filter. You\u2019ll remember that we use app.controller() to create controllers and app.module() to create modules. In exactly the same way, AngularJS has given us the angular.filter API to create a custom filter in AngularJS.&nbsp;&nbsp;<\/p>\n\n\n\n<p>A custom filter can be created using the following syntax:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"328\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image.png\" alt=\" custom filter can be created using the following syntax\" class=\"wp-image-1526\" title=\" custom filter can be created using the following syntax\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image.png 720w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-300x137.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-480x219.png 480w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p>To create a custom filter, you need to do the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a filter using the app.filter by passing a custom filter name and a function as input parameters to the app.filter()<\/li>\n\n\n\n<li>App.filter() will return a function<\/li>\n\n\n\n<li>The returned function can take various optional input parameters<\/li>\n\n\n\n<li>The returned function will have custom filter code and it will return the output.<\/li>\n<\/ul>\n\n\n\n<p>Let us start with creating a very simple custom filter. We can apply this custom filter on a string and it will return the string with each character in a capital case.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyApp.filter('toUpperCase', function () {\n    return function (input)\n    {\n        var output = \"\";       \n        output = input.toUpperCase();\n        return output;\n    }\n})<\/pre>\n\n\n\n<p>We can use the toUpperCase custom filter in the view as shown the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div ng-controller=\"ProductController\">\n     &lt;table class=\"table\">              \n         &lt;tr ng-repeat=\"a in products|filter:searchTerm\">\n             &lt;td>{{a.name|toUpperCase}}&lt;\/td>\n             &lt;td>{{a.price}}&lt;\/td>\n         &lt;\/tr>\n     &lt;\/table>\n&lt;\/div><\/pre>\n\n\n\n<p>We need to keep in mind that the name of the custom filter is case sensitive. The above-created view is reading data from the controller as shown the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyApp.controller(\"ProductController\", function ($scope) {\n    $scope.products = [\n\t\t{ 'name': 'pen', 'price': '200' },\n\n\t\t{ 'name': 'pencil', 'price': '400' },\n\n\t\t{ 'name': 'book', 'price': '2400' },\n\n\t\t{ 'name': 'ball', 'price': '400' },\n\n\t\t{ 'name': 'eraser', 'price': '1200' },\n    ];\n})<\/pre>\n\n\n\n<p>Now we\u2019ll get the product name rendered in a capital case on the view as shown in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"225\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-1.png\" alt=\"The above-created view is reading data from the controller as shown the listing\" class=\"wp-image-1527\" title=\"The above-created view is reading data from the controller as shown the listing\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-1.png 804w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-1-300x84.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-1-768x215.png 768w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-1-480x134.png 480w\" sizes=\"auto, (max-width: 804px) 100vw, 804px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyApp.filter('toPositionUpperCase', function () {\n \n    return function (input,position)\n    {\n        var output = [];       \n        var capLetter = input.charAt(position).toUpperCase();\n        for (var i = 0; i &lt; input.length; i++) {\n \n            if (i == position) {\n                output.push(capLetter);\n            } else {\n                output.push(input[i]);\n            }\n \n        }\n        output = output.join('');\n        return output;\n    }\n})<\/pre>\n\n\n\n<p>We can use toPositionUpperCase custom filter in the view as shown the listing below. As you will notice that we are passing the input parameter to the custom filter using the colon.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div ng-controller=\"ProductController\">\n    &lt;table class=\"table\">              \n        &lt;tr ng-repeat=\"a in products|filter:searchTerm\">\n             &lt;td>{{a.name|toPositionUpperCase:1}}&lt;\/td>\n            &lt;td>{{a.price}}&lt;\/td>\n        &lt;\/tr>\n    &lt;\/table>\n&lt;\/div><\/pre>\n\n\n\n<p>We will get the second letter of product name rendered in the capital case on the view as shown in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"204\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-2.png\" alt=\"We will get the second letter of product name rendered in the capital case on the view as shown\" class=\"wp-image-1528\" title=\"We will get the second letter of product name rendered in the capital case on the view as shown\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-2.png 748w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-2-300x82.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-2-480x131.png 480w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n\n<p>Before we conclude this article, let us create a custom filter which will be applied on the collection of items. Let us say from the list of products, we want to filter all the products greater than a given price. We can write this custom filter as shown in the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyApp.filter('priceGreaterThan', function () {\n \n    return function (input, price) {\n        var output = [];\n        if (isNaN(price)) {\n \n            output = input;\n        }\n        else {\n            angular.forEach(input, function (item) {\n \n                if (item.price > price) {\n                    output.push(item)\n                }\n            });\n        }\n        return output;\n    }\n})<\/pre>\n\n\n\n<p>We can use the custom filter on the view as shown in the listing below. We are passing the price parameter from the input type text box.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;h1>With Custom Filter&lt;\/h1>\n       \n&lt;div ng-controller=\"ProductController\">\n   &lt;input type=\"number\" class=\"form-control\" placeholder=\"Search here\" ng-model=\"priceterm\" \/>\n   &lt;br\/>\n   &lt;table class=\"table\">\n       &lt;tr ng-repeat=\"b in products|priceGreaterThan:priceterm\">\n           &lt;td>{{b.name}}&lt;\/td>\n           &lt;td>{{b.price}}&lt;\/td>\n       &lt;\/tr>\n   &lt;\/table>\n&lt;\/div><\/pre>\n\n\n\n<p>With this we will get a filtered array on the view as shown in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"231\" src=\"https:\/\/staging.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-3.png\" alt=\"we will get a filtered array on the view as shown\" class=\"wp-image-1529\" title=\"we will get a filtered array on the view as shown\" srcset=\"https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-3.png 741w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-3-300x94.png 300w, https:\/\/www.infragistics.com\/blogs\/wp-content\/uploads\/2015\/11\/image-3-480x150.png 480w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/figure>\n\n\n\n<p>So there you have it \u2013 that\u2019s how to create a custom filter! It\u2019s easy \u2013 they\u2019re nothing but functions that take one input and optional parameters to return a function. I hope you enjoyed reading!<\/p>\n\n\n\n<p><em>Deliver modern, responsive web applications with no limits on browsers, devices, and platforms with Infragistics jQuery&amp; HTML5 controls.&nbsp;<a href=\"https:\/\/www.infragistics.com\/downloads\/generate\/00000000-0000-0000-0000-000000005091\">Download Free Trial<\/a>&nbsp;now and see their power in action!<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.infragistics.com\/downloads\/generate\/00000000-0000-0000-0000-000000005620\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/download.infragistics.com\/marketing\/Blog-in-content-ads\/Ignite-UI-JavaScript\/Blog-incontent-IgniteUI-650x200.jpg\" alt=\" \"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever used filters with the ng-repeat directive as shown in the listing below? If so, then you\u2019ve used a filter in an AngularJS application. AngularJS provides us many in-built directives like search. If required, AngularJS also allows us to create custom filters, which we\u2019ll explore in this post. AngularJS gives us a simple [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":1282,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,17],"tags":[],"class_list":["post-602","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":3,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":2002,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/602\/revisions\/2002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/1282"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}