Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)

前端 未结 13 2404
北恋
北恋 2020-11-22 17:08

Take a look at the example here: http://docs.angularjs.org/api/ng.filter:filter

You can search by any of the phone properties by using

13条回答
  •  死守一世寂寞
    2020-11-22 17:59

    I might be very late but this is what I ended up doing. I made a very general filter.

    angular.module('app.filters').filter('fieldFilter', function() {
            return function(input, clause, fields) {
                var out = [];
                if (clause && clause.query && clause.query.length > 0) {
                    clause.query = String(clause.query).toLowerCase();
                    angular.forEach(input, function(cp) {
                        for (var i = 0; i < fields.length; i++) {
                            var haystack = String(cp[fields[i]]).toLowerCase();
                            if (haystack.indexOf(clause.query) > -1) {
                                out.push(cp);
                                break;
                            }
                        }
                    })
                } else {
                    angular.forEach(input, function(cp) {
                        out.push(cp);
                    })
                }
                return out;
            }
    
        })
    

    Then use it like this

    
    

    Your search box be like

    
    

    where name and device_id are properties in dvs

提交回复
热议问题