Javascript 将条件添加到筛选器

Javascript 将条件添加到筛选器,javascript,angularjs,filter,ngtable,Javascript,Angularjs,Filter,Ngtable,我正在使用Angularng表格,并对备考平台进行过滤。我将测验结果存储为数据库中每个问题的0或1,但需要向用户显示为Correct或Correct 我可以用{{q.result==1?'Correct':'Correct'}} 但这也需要应用于过滤,也就是说,用户应该能够键入“Correct”并过滤q.result=1的所有问题 <tr ng-repeat="q in $data"> <td title="'Correct/Incorrect'" filter="{

我正在使用Angular
ng表格
,并对备考平台进行过滤。我将测验结果存储为数据库中每个问题的
0或1
,但需要向用户显示为
Correct
Correct

我可以用
{{q.result==1?'Correct':'Correct'}}

但这也需要应用于过滤,也就是说,用户应该能够键入“Correct”并过滤q.result=1的所有问题

<tr ng-repeat="q in $data">
    <td title="'Correct/Incorrect'" filter="{ result: 'text'}" sortable="'q.result'" ng-class="{correct: q.result == 1, incorrect: q.result == 0}">
        {{q.result == 1 ? 'Correct' : 'Incorrect'}}
    </td>

我正在使用一个指令:

angular
    .module('DDE')
    .directive('results',['$rootScope', 'NgTableParams', function ($rootScope, NgTableParams) {
        return {
            restrict: 'AE',
            scope: {},
            transclude: true,
            templateUrl: '/html/directives/results.html',
            link: function(scope, elem, attr){
                scope.questions = [];
                /*
                    If user took a new test, show results
                 */
                scope.$on('show-results', function(event, args) {
                    scope.setData(args);
                });

                /*
                    Set question data
                 */
                scope.setData = function (args) {
                    console.log(args);
                    scope.questions = args;
                    scope.tableParams = new NgTableParams({}, { dataset: args});
                };
            }
        }
}]);
Console.logging
args
提供:


本模块的文档非常糟糕,我就是这样对表进行排序和筛选的

scope.tableParams = new NgTableParams({}, { 
    total: args.length,
    data: args,
    getData: function ( $defer, params ) {
        var orderedData = params.filter() ? $filter( 'filter' )( scope.questions, params.filter() ) : scope.questions;
        orderedData = params.sorting() ? $filter( 'orderBy' )( orderedData, params.orderBy() ) : orderedData;
        params.total( orderedData.length );
        $defer.resolve( orderedData );
    }
});

您需要使用
filter=“{result:'text'}
”设置过滤器,并将
$filter
注入指令。

您可以在控制器中包含创建表的部分吗?@AlonEitan请参见上文,这是一个指令
scope.tableParams = new NgTableParams({}, { 
    total: args.length,
    data: args,
    getData: function ( $defer, params ) {
        var orderedData = params.filter() ? $filter( 'filter' )( scope.questions, params.filter() ) : scope.questions;
        orderedData = params.sorting() ? $filter( 'orderBy' )( orderedData, params.orderBy() ) : orderedData;
        params.total( orderedData.length );
        $defer.resolve( orderedData );
    }
});