Angular filter a object by its properties

后端 未结 6 2019
感情败类
感情败类 2020-11-30 04:47

I have an object with a series of object properties that is in the following similar structure (which is the way the data is coming back from a service):

{
          


        
相关标签:
6条回答
  • 2020-11-30 05:14

    Little late to answer, but this might help :

    If you want to filter on a grandchild (or deeper) of the given object, you can continue to build out your object hierarchy. For example, if you want to filter on 'thing.properties.title', you can do the following:

    <div ng-repeat="thing in things | filter: { properties: { title: title_filter } }">
    

    You can also filter on multiple properties of an object just by adding them to your filter object:

    <div ng-repeat="thing in things | filter: { properties: { title: title_filter, id: id_filter } }">
    

    The syntax of 'not equals' is just a little off, try the following:

    <div ng-repeat="thing in things | filter: { properties: { title: '!' + title_filter } }">
    
    0 讨论(0)
  • 2020-11-30 05:15

    No need of all that. This solution works fine without having to convert or creating your own filter:

     {{myModel.userSelectedHour["Start"]}}
    

    Therefore, object.["property"] instead of object.property.

    0 讨论(0)
  • 2020-11-30 05:28

    Although I agree that converting your object might be the best option here, you can also use this filter function:

    angular.module('app').filter('objectByKeyValFilter', function () {
    return function (input, filterKey, filterVal) {
        var filteredInput ={};
         angular.forEach(input, function(value, key){
           if(value[filterKey] && value[filterKey] !== filterVal){
              filteredInput[key]= value;
            }
         });
         return filteredInput;
    }});
    

    like this:

    <div ng-repeat="(key, value) in data | objectByKeyValFilter:'type':'foo'">{{key}}{{value.type}}</div> 
    

    See also the Plunker.

    0 讨论(0)
  • 2020-11-30 05:28

    Try this to filter object

    var rateSelected = $filter('filter')($scope.GradeList, function (obj) {
                            if(obj.GradeId == $scope.contractor_emp.save_modal_data.GradeId)
                            return obj;
                    });
    
    0 讨论(0)
  • 2020-11-30 05:30

    Filter works on arrays but you have an object literal.

    So you can either convert your object literal into an array or create your own filter than takes in the object literal.

    If you don't need those index values then converting to an array may be your best bet( Here's a fiddle with the array working: http://jsfiddle.net/NqA8d/3/):

    $scope.items = [{
        "type": "foo",
            "name": "blah"
    }, {
        "type": "bar"
    }, {
        "type": "foo"
    }, {
        "type": "baz"
    }, {
        "type": "test"
    }];
    

    In case you'd like to do a filter, here's one way to do that:

    myApp.filter('myFilter', function () {
        return function (items, search) {
            var result = [];
            angular.forEach(items, function (value, key) {
                angular.forEach(value, function (value2, key2) {
                    if (value2 === search) {
                        result.push(value2);
                    }
                })
            });
            return result;
    
        }
    });
    

    And that fiddle: http://jsfiddle.net/NqA8d/5/

    0 讨论(0)
  • 2020-11-30 05:35

    Try this(in controller):

    $scope.notFooFilter = function(item)
    {
        return (item.type !== 'foo');
    };
    

    And in HTML markup:

    <div ng-repeat="item in items| filter:notFooFilter">{{item.type}}</div>
    
    0 讨论(0)
提交回复
热议问题