AngularJS排序依据与空场 [英] AngularJS orderby with empty field

查看:119
本文介绍了AngularJS排序依据与空场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我订购我的数据和工作的所有correcty除了一些字段为空或没有任何价值。当这些订购空场上来先。例如订货号码时,我们会得到0 - 值前获得巨大的空列表。

I am ordering a my data and its working all correcty except some fields are empty or have no value. When ordered these empty field come up first. For example when ordering numbers we would get a huge empty list before getting the "0"-values.

我做它像thise:

ng-click="predicate = 'name'; reverse=!reverse"

ng-repeat="name in names | orderBy:predicate:reverse"

的jsfiddle: http://jsfiddle.net/JZuCX/1/

有一个简单优雅的方式来解决这一问题?我想空字段排在最后,不管是什么。

Is there an easy elegant way to fix this? I want the empty fields to come last, no matter what.

推荐答案

我会写一个过滤器,需要从有序阵列与空的名字的物品,并在年底把它们:

I'd write a filter that takes items with empty name from ordered array and places them at the end:

<li ng-repeat="item in (items|orderBy:'name'|emptyToEnd:'name')">{{item.name}}</li>

code可能是这样的:

Code might look like this:

.filter("emptyToEnd", function () {
    return function (array, key) {
        if(!angular.isArray(array)) return;
        var present = array.filter(function (item) {
            return item[key];
        });
        var empty = array.filter(function (item) {
            return !item[key]
        });
        return present.concat(empty);
    };
});

工作实例

顺便说一句,你拨弄不包含任何有关code。是否使用了错误的链接?

By the way, your fiddle doesn't contain any relevant code. Did you use the wrong link?

更新2:
您与我的过滤小提琴。

这篇关于AngularJS排序依据与空场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆