坚持根据或运算符逻辑

时间:2017-03-03 06:58:14

标签: javascript angularjs

我有一个观点:

 <tr>
     <td>
         <label class="radio-inline">
              <input type="radio" name="Active" checked ng-click="sort('false','false','false')">Active
              <span class="label label-success">{{totalActive}}</span>
         </label>
     </td>

     <td>
         <label class="radio-inline">
             <input type="radio" name="Active" ng-click="sort('true','','true')">Archieve
             <span class="label label-warning">{{appliedJob.length-totalActive-totalClosed}}</span>
         </label>
     </td>
     <td>
         <label class="radio-inline">
             <input type="radio" name="Active" ng-click="sort('','true','')">Closed
             <span class="label label-warning">{{totalClosed}}</span>
         </label>
     </td>
 </tr>

 <tr data-dir-paginate="item in appliedJob|orderBy:'AppliedDate':true|filter:{HiredAnother:filterText, Closed:Closed,Withdraw:w}|itemsPerPage:5">

MY JS功能:

$scope.filterText = false;
$scope.Closed = false;
$scope.w = false;


$scope.sort = function (key,c,w) {
    $scope.filterText = key;
    $scope.Closed = c;
    $scope.w = w;

}

我想要的是,如果Withdraw为false,它将继续Archieve(无论HiredAnother是真还是假)。但在过滤器中,它只会Archieve列出HiredAnother =trueWithdraw=true。 如何在ng-repeat过滤器中创建或(||)运算符?

清除我的想法:

Active-->HiredAnother=false,Closed=false, Withdraw=false
Archieve-->HiredAnother=true/false,Closed=false, Withdraw=false/true(HiredAnother and Withdraw- must be false at least one)
Closed-->HiredAnother=true/false,Closed=true, Withdraw=false/true

=================按预期结果提升==========================

 <tr>
                <td>
                    <label class="radio-inline">
                        <input type="radio" name="Active" checked ng-click="sort('ac')">Active
                        <span class="label label-success">{{totalActive}}</span>
                    </label>
                </td>

                <td>
                    <label class="radio-inline">
                        <input type="radio" name="Active" ng-click="sort('ar')">Archieve


                        <span class="label label-warning">{{appliedJob.length-totalActive-totalClosed}}</span>
                    </label>
                </td>
                <td>
                    <label class="radio-inline">
                        <input type="radio" name="Active" ng-click="sort('cl')">Closed
                        <span class="label label-warning">{{totalClosed}}</span>
                    </label>
                </td>
            </tr>

<tr data-dir-paginate="item in appliedJob|orderBy:'AppliedDate':true|filter:myFilter|itemsPerPage:5">


 //Filter based on radio button
$scope.myFilter = function (i) {
    return i.HiredAnother == false && i.Withdraw == false && i.Closed == false; //Active
};

$scope.sort = function (term) {

    if (term == 'ac') {
        $scope.myFilter = function (i) {
            return i.HiredAnother == false && i.Withdraw == false && i.Closed == false; //Active
        };
    }

    else if (term == 'ar') {
        $scope.myFilter = function (i) {
            return i.HiredAnother == true || i.Withdraw == true && i.Closed == false; //Arcieve
        };
    }
    else if (term == 'cl') {
        $scope.myFilter = function (i) {
            return i.Closed == true; //Arcieve
        };
    }
}
//Filter based on radio button END

1 个答案:

答案 0 :(得分:0)

我建议使用为每个appliedJob调用的自定义过滤器函数。

<tr data-dir-paginate="item in appliedJob | filter: myCoolFilter">

和函数(看看docs

$scope.myCoolFilter(value, index, array) {
    // change here according to your logic
    return $scope.filterText || $scope.Closed; 
}