AngularJS filtering by code

96 views Asked by At

I'm working with AngularJS. How can I search by Code? My current implementation searches what ever you type: id , name, city, code... How can I search only by code?

app.js:

var app = angular.module('stack', []);

app.directive('filterList', function ($timeout) {
    return {
        link: function (scope, element, attrs) {

            var td = Array.prototype.slice.call(element[0].children);

            function filterBy(value) {
                td.forEach(function (el) {
                    el.className = el.textContent.toLowerCase().indexOf(value.toLowerCase()) !== -1 ? '' : 'ng-hide';
                });
            }

            scope.$watch(attrs.filterList, function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    filterBy(newVal);
                }
            });
        }
    };
});

example on jSFiddle

2

There are 2 answers

1
hpatel On BEST ANSWER
    var app = angular.module('stack', []);

    app.directive('filterList', function ($timeout) {
        return {
            link: function (scope, element, attrs) {

                var td = Array.prototype.slice.call(element[0].children);

                function filterBy(value) {
                    td.forEach(function (el) {
                        el.className = el.cells[3].textContent.toLowerCase().indexOf(value.toLowerCase()) !== -1 ? '' : 'ng-hide';
                    });
                }

                scope.$watch(attrs.filterList, function (newVal, oldVal) {
                    if (newVal !== oldVal) {
                        filterBy(newVal);
                    }
                });
            }
        };
    });
0
nitin On

If you have any text box to write the code and filter based upon on that, then you can use | inbuilt filter of angular like this,

<div ng-repeat="product in products | filter:{ colour: by_colour }">

Hope this will help to solve your problem :)