Search code examples
javascriptangularjsangular-filters

AngularJS filtering by code


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


Solution

  •     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);
                        }
                    });
                }
            };
        });