1

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

4
  • Can you post your html. Commented Jun 13, 2015 at 9:29
  • I already post a JSFiddle link :) ! Commented Jun 13, 2015 at 9:31
  • 1
    You can find the answer here. jsfiddle.net/d3cn5x5v/5 Commented Jun 13, 2015 at 9:54
  • Yeah Thnx, I already know about ng-init, I want to filter but without using ng-repeat ??? Commented Jun 13, 2015 at 10:04

2 Answers 2

3
    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);
                    }
                });
            }
        };
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Just need to specify cell number of the heading row e.g. el.cells[3] for Code column
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.