elreeda
elreeda

Reputation: 4597

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

Upvotes: 1

Views: 87

Answers (2)

nitin
nitin

Reputation: 156

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 :)

Upvotes: 1

hpatel
hpatel

Reputation: 199

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

Upvotes: 3

Related Questions