javascript - Dynamic orderBy in AngularJS -
i have array of objects want order dynamically, based on value drop down menu. have far in list:
ng-repeat="item in filtereditems = (items | filter:searchinput | orderby:canbeanything)"
but problem sorting can attribute of object or calculated value using function. should able sort in descending way (optionally).
i know can use string canbyanything variable behind orderby passing attribute of object like:
“-creationdate” // descending ordering on creation date “customer.lastname” // ascending ordering on customers last name
i know can orderby function like:
orderby:mycalculatedvaluefunction // order in ascending way based on calculated value, example calculating total price of object if order or
but don't know , want achieve is:
- how combine can use function(s) sorting in combination attributes/properties of objects. mean 1 or other, dynamically, based on user has selected. can attribute or calculated value, either descending or ascending.
- how sort calculated value in descending way
update orderby:mycalculatedvaluefunction
orderby:dynamicorderfunction
:
$scope.dynamicorderfunction = function() { if (orderbystring) { return '-creationdate'; } else { return mycalculatedvaluefunction; } }
orderby
has 3rd property accepts boolean , reverse orderby when true
. (orderby:dynamicorderfunction:reverseorder
$scope.reverseorder = true; // or false
)
edit
you run issues trying switch orderby between string function way. checkout out this jsfiddle working dynamic order function.
Comments
Post a Comment