Dynamic orderBy in AngularJS

只谈情不闲聊 提交于 2019-12-01 04:48:11

Update orderBy:myCalculatedValueFunction to something like orderBy:dynamicOrderFunction:

$scope.dynamicOrderFunction = function() {
    if (orderByString) {
        return '-creationDate';
    }
    else {
        return myCalculatedValueFunction;
    }
}

orderBy also has a 3rd property that accepts a boolean and will reverse orderBy when true. (orderBy:dynamicOrderFunction:reverseOrder where $scope.reverseOrder = true; // or false)


edit

You will actually run into issues trying to switch orderBy between a string a function this way. Checkout out this jsfiddle for a working dynamic order function.

So you have to create your own filter and do what ever you want in it, there's tons of example on google. Just search :

angular custom filter

Theses last days, i experience somes issues with filters creation and i found that: https://github.com/a8m/angular-filter

I've added it immediately in my dependcies, i know i will use it really soon. May be it will help you too. Don't forget to valid my answer if it helps you to resolve your problem ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!