【问题标题】:add conditional to filter添加条件过滤
【发布时间】:2016-07-31 20:51:13
【问题描述】:

我正在使用带有过滤功能的 Angular ng-table 用于测试准备平台。我将测验结果存储为数据库中每个问题的0 or 1,但需要将它们显示为CorrectIncorrect 给用户。

我可以通过 {{q.result == 1 ? 'Correct' : 'Incorrect'}} 做到这一点

但这也需要应用于过滤,也就是说,用户应该能够输入“正确”并过滤所有问题,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>

我尝试更改 filter="{ result: 'text'} 以遵循上述三元约定,但它不起作用。

filter="{(result == 1 ? 'Correct' : 'Incorrect') : 'text'}" 给出错误:

Syntax Error: Token '(' invalid key at column 2 of the expression [{(result == 1 ? 'Correct' : 'Incorrect') : 'text'}] starting at [(result == 1 ? 'Correct' : 'Incorrect') : 'text'}].

我正在使用指令:

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 给出:

【问题讨论】:

  • 你能在你的控制器中包含你创建表的部分吗?
  • @AlonEitan 见上文,这是一个指令

标签: javascript angularjs filter ngtable


【解决方案1】:

这个模块的文档很糟糕,这就是我对表格进行排序和过滤的方式

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 注入您的指令中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2014-02-03
    • 2018-04-15
    • 1970-01-01
    相关资源
    最近更新 更多