0

I want to order a list of items by multiple properties. So I use an array that I bind in a variable.

Everything works fine until I let the user choose order options with a select. It breaks when my array has more than 1 value... and it seems to defaults to the track by $index order.

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope){
    $scope.items = [
	{a: "3", b: "First"},
	{a: "2", b: "Second"},
	{a: "1", b: "Third"}
    ];
    $scope.my_orderby = ['a','b'];
    $scope.my_selected = {
	a: ['a'],
	b: ['b'],
	ab: ['a','b'],
	ba: ['b','a']
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
<div ng-app="myApp" ng-controller="myCtrl">
  <button ng-click="my_orderby.push(my_orderby.shift())">In place: {{my_orderby}}</button>
  <select ng-model="my_orderby">
    <option value="{{v}}" ng-repeat="(k,v) in my_selected">{{k}}</option>
  </select>
  <button ng-click="items.push(items.shift())">{{items}}</button>
  <hr/>
  <div ng-repeat="item in items | orderBy:my_orderby track by $index">{{item.a}} {{item.b}}</div>
</div>

How am I supposed to do to allow sorting by multiple properties?

3 Answers 3

1

It seems to be working with ng-options. I guess that "value" is converting the value to a string instead of keeping it as an array.

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope){
    $scope.items = [
	{a: "3", b: "First"},
	{a: "2", b: "Second"},
	{a: "1", b: "Third"}
    ];
    $scope.my_orderby = ['a','b'];
    $scope.my_selected = {
	a: ['a'],
	b: ['b'],
	ab: ['a','b'],
	ba: ['b','a']
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    
<div ng-app="myApp" ng-controller="myCtrl">
  <button ng-click="my_orderby.push(my_orderby.shift())">In place: {{my_orderby}}</button>
  <select ng-model="my_orderby" ng-options="item as index for (index, item) in my_selected">
  </select>
  <button ng-click="items.push(items.shift())">{{items}}</button>
  <hr/>
  <div ng-repeat="item in items | orderBy:my_orderby track by $index">{{item.a}} {{item.b}}</div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted. Not 100% working but you gave me the right lead for a solution. Thanks!
1

@Poney gave me the right lead for an answer.

OPTION converts my array to CDATA object. Not sure what a CData array looks like, but avoiding it solved my issue.

https://www.w3.org/TR/html401/interact/forms.html#edef-OPTION.

To preserve my array, I had to combine ng-options with ng-change and do the change in a javascript function.

Comments

0

Try this. Hope it will help you. Thank you.

<div ng-repeat="item in items | orderBy:'a'">{{item.a}} {{item.b}}</div>

1 Comment

It does not help because I want it to work with an array... thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.