Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a page in an AngularJS single page application that displays data from multiple tables in my database by reading metadata about the table structure. When a field is a lookup to a foreign key it automatically creates a select drop-down list. Everything is working except I can't get the list to sort by name. It is sorting by the primary key ID field which is usually an int type. Below is the controller script that loads the options list in the model and the view script that displays the select control and tries to sort the items. Can you tell me how to redo this so that it will work? I have also tried with and without the single quotes in the orderBy clause and I have tried using toString() in the orderby clause, but nothing has worked.

// controller script
var responseData = responseFK.data;
angular.forEach(responseData, function (item)
{
    columnOptions = columnOptions.concat({ "ID": item[columnNameFK], "Name": item[columnNameFK_Name] });
});
column.COLUMN_OPTIONS = columnOptions;

// view script
<select ng-switch-when="select" ng-options="item.ID as item.Name for item in x.COLUMN_OPTIONS | orderBy: 'item.Name'" ng-model="x.COLUMN_VALUE" ng-required="x.REQUIRED" required></select>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>

share|improve this question
2  
Try to use orderBy: 'Name', just a property name without item. – Ali Baig yesterday
    
You should just write code with the array of data and the select loading it, then we can analyze and test it concretely. – sylvain1264 yesterday
    
orderBy: 'item.Name' should be orderBy: 'Name' – jbrown yesterday
1  
We have a winner on the very first comment. Ali Baig (and jbrown) got it right. I just tried it and it worked. So simple. Thanks. My list looks so nice now! – Steve Gaines yesterday
    
up vote 0 down vote accepted

Try to use orderBy: 'Name', just a property name without item. So your view script should now be:

<select ng-switch-when="select" ng-options="item.ID as item.Name for item in x.COLUMN_OPTIONS | orderBy: 'Name'" ng-model="x.COLUMN_VALUE" ng-required="x.REQUIRED" required></select>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.