如何在angular2的搜索过滤器管道中实现“找不到匹配项”?

时间:2018-12-10 16:08:38

标签: angular

嗨,我如何实现在angular2的搜索过滤器管道中找不到匹配项?如果找不到匹配项,我希望输入文本“找不到项目”。 谢谢

 transform(flightlist: FlightModel[], text: any): any {
        if (!text) {
            return flightlist;
        }

        return flightlist.filter((it) => {
            return it.arrivalModel.city.includes(text) || it.arrivalModel.fs.includes(text) || it.departure.includes(text) || it.arrival.includes(text) || it.departureModel.city.includes(text);
        })

    }

1 个答案:

答案 0 :(得分:1)

您可以在已过滤列表中添加检查以查看是否已填充,而不是返回原始已过滤列表。

transform(flightlist: FlightModel[], text: any): any {
    if (!text) {
        return flightlist;
    }

    const filtered = flightlist.filter((it) => {
        return it.arrivalModel.city.includes(text) || 
               it.arrivalModel.fs.includes(text) || 
               it.departure.includes(text) || 
               it.arrival.includes(text) || 
               it.departureModel.city.includes(text);
    });

    if (filtered.length === 0) { 
        filtered.push("No item found" as any); 
        return filtered; 
    } 

    return filtered;
}