ng-repeat filter on boolean

前端 未结 5 1816
再見小時候
再見小時候 2021-02-02 05:16

I am trying to filter on a boolean value in an ng-repeat.

List of unregistered users:

    

Unregistered Users

相关标签:
5条回答
  • 2021-02-02 05:48

    In case an item does not have a boolean property set, you can find the item with a property not set to true by using '!' with quotes. e.x. filter: {property:'!'+true}.

    Example:

    $scope.users = [
            {
                name : 'user1 (registered)',
                registered : true
            },
            {
                name : 'user2 (unregistered)'
            },
            {
                name : 'user3 (registered)',
                registered : true
            },
             {
                name : 'user4 (unregistered)'
            }
    

    To get unregistered users filter:

    <h3>Unregistered Users</h3>
    <div ng-repeat="user in users | filter:{registered:'!'+true}">
        <div class="row-fluid">
            <div class="span2">
              {{user.name}}
            </div>
          </div>
        </div>
    
    0 讨论(0)
  • 2021-02-02 06:01

    filter by obj expression:

    <h3>Unregistered Users</h3>
    <div ng-repeat="user in users | filter:{registered:false}">
        <div class="row-fluid">
        <div class="span2">
          {{user.name}}
        </div>
      </div>
    </div>
    

    JSFiddle: http://jsfiddle.net/alfrescian/9ytDN/

    0 讨论(0)
  • 2021-02-02 06:02

    Create a method in the controller which returns true or false based on the logic you need and specify that function in the filter.

    Something like this:

    $scope.isRegistered = function(item) {
      return item.registered;
    };
    
    <h3>Unregistered Users</h3>
    <div ng-repeat="user in users | filter:!isRegistered ">
        <div class="row-fluid">
        <div class="span2">
          {{user.name}}
        </div>
      </div>
    </div>
    
    0 讨论(0)
  • 2021-02-02 06:02

    Slight modification to Websirnik's answer, this allows any column name for the dataset:

    .filter('onlyBooleanValueFilter', [function () {
        return function (input, column, trueOrFalse) {
            var ret = [];
    
            if (!angular.isDefined(trueOrFalse)) {
                trueOrFalse = false;
            }
    
            angular.forEach(input, function (v) {
                if (angular.isDefined(v[column]) && v[column] === trueOrFalse) {
                    ret.push(v);
                }
            });
    
            return ret;
        };
    }])
    

    Markup:

    <div repeat="row in your.dataset | onlyBooleanValueFilter: 'yourColumn' : true"> 
         ...your stuff here....
    </div>
    
    0 讨论(0)
  • 2021-02-02 06:03

    Had the same question. Alfrescian solution didn't work for me on Angular 1.1.5.

    Found this fiddle: http://jsfiddle.net/buehler/HCjrQ/

    .filter('onlyBooleanValueFilter', [function(){
        return function(input, param){
            var ret = [];
            if(!angular.isDefined(param)) param = true;
            angular.forEach(input, function(v){
                // 'active' is a hard-coded field name
                if(angular.isDefined(v.active) && v.active === param){
                    ret.push(v);
                }
            });
            return ret;
        };
    }])
    

    You would need to adjust the code of the filter according to your field.

    0 讨论(0)
提交回复
热议问题