ng-repeat filter negate string from object - angularjs

◇◆丶佛笑我妖孽 提交于 2019-12-08 03:13:51

问题


I want to put this filter: filter:'!Category' on to this element like so:

<div ng-repeat="(prop, ignoredValue) in wines[0] | filter:'!Category'" ng-init="filter[prop]={}">

But it does not filter out the property "Category". However if I put a similar filter to exclude "Wine" on the following ng-repeat element, it works fine:

<span class="quarter" ng-repeat="opt in getOptionsFor(prop) | filter:'!Wine'">

I don't understand why I can't filter out property values here. I am very new to angularjs and I'm going out of my mind trying to figure this out. I want to exclude specific property names from the object. either "name" or "category"

I linked the fiddle below. Any help much appreciated! Thanks!

jsfiddle <- link to the fiddle

<div ng-controller="myCtrl">
<div ng-repeat="(prop, ignoredValue) in wines[0]" ng-init="filter[prop]={}">
    <b>{{prop | capitalizeFirst}}:</b><br />
    <span class="quarter" ng-repeat="opt in getOptionsFor(prop)">
        <b><input type="checkbox" ng-model="filter[prop][opt]" />&nbsp;{{opt}}</b>
    </span>
    <hr />
</div>
<div ng-repeat="w in filtered=(wines | filter:filterByProperties)">
    {{w.name}} ({{w.category}})
</div>
<hr />
Number of results: {{filtered.length}}

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.wines = [
    { name: "Wine A", category: "red" },
    { name: "Wine B", category: "red" },
    { name: "wine C", category: "white" },
    { name: "Wine D", category: "red" },
    { name: "Wine E", category: "red" },
    { name: "wine F", category: "white" },
    { name: "wine G", category: "champagne"},
    { name: "wine H", category: "champagne" }    
];
$scope.filter = {};

$scope.getOptionsFor = function (propName) {
    return ($scope.wines || []).map(function (w) {
        return w[propName];
    }).filter(function (w, idx, arr) {
        return arr.indexOf(w) === idx;
    });
};

$scope.filterByProperties = function (wine) {
    // Use this snippet for matching with AND
    var matchesAND = true;
    for (var prop in $scope.filter) {
        if (noSubFilter($scope.filter[prop])) continue;
        if (!$scope.filter[prop][wine[prop]]) {
            matchesAND = false;
            break;
        }
    }
    return matchesAND;
/**/
/*
    // Use this snippet for matching with OR
    var matchesOR = true;
    for (var prop in $scope.filter) {
        if (noSubFilter($scope.filter[prop])) continue;
        if (!$scope.filter[prop][wine[prop]]) {
            matchesOR = false;
        } else {
            matchesOR = true;
            break;
        }
    }
    return matchesOR;
/**/
};

function noSubFilter(subFilterObj) {
    for (var key in subFilterObj) {
        if (subFilterObj[key]) return false;
    }
    return true;
}
});

app.filter('capitalizeFirst', function () {
return function (str) {
    str = str || '';
    return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
};
});

回答1:


You could write a simple filter to extract keys first:

app.filter('keys', function () {
    return function (object) {
        return Object.keys(object || {}).filter(function (key) {
            return key !== '$$hashKey'; // this is from ng-repeat
        });
    };
});

and use it like this:

<div ng-repeat="prop in wines[0] | keys | filter:'!Category'"

Example JSFiddle: http://jsfiddle.net/1qhba9fs/1/

Hope this helps.



来源:https://stackoverflow.com/questions/25322035/ng-repeat-filter-negate-string-from-object-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!