简体   繁体   English

AngularJS筛选器值例程为null时不起作用

[英]AngularJS Filter values routine does not work when null

I have a view with a table that I want to be able to filter what is displayed based on a filtered string entered by the user. 我有一个带有表的视图,该表希望能够根据用户输入的过滤字符串来过滤显示的内容。 The one problem that I am running into is that the fields could be null so my filter does not work. 我遇到的一个问题是字段可能为null,因此我的过滤器无法正常工作。

var quoteNameFilter = function () {

    return function (quotes, filterValue) {
        if (!filterValue) return quotes;

        var matches = [];
        filterValue = filterValue.toLowerCase();
        for (var i = 0; i < quotes.length; i++) {
            var quote = quotes[i];
            if (quote.rfq.toLowerCase().indexOf(filterValue) > -1 ||
                quote.comments.toLowerCase().indexOf(filterValue) > -1 ||
                quote.priority.name.toLowerCase().indexOf(filterValue) > -1 ||
                quote.customer.customerName.toLowerCase().indexOf(filterValue) > -1 ||
                quote.status.name.toLowerCase().indexOf(filterValue) > -1) {

                matches.push(quote);
            }
        }
        return matches;
    };
};

quote.comments could be null, so how do I check if the value is null in this routine? quote.comments可以为null,那么如何在此例程中检查该值是否为null?

只需使用&&运算符确保quote.comments是一个true值:

(quote.comments && quote.comments.toLowerCase().indexOf(filterValue) > -1) || ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM