0

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>

6
  • 2
    Try to use orderBy: 'Name', just a property name without item. Commented Jan 20, 2017 at 21:32
  • You should just write code with the array of data and the select loading it, then we can analyze and test it concretely. Commented Jan 20, 2017 at 21:32
  • orderBy: 'item.Name' should be orderBy: 'Name' Commented Jan 20, 2017 at 21:37
  • 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! Commented Jan 20, 2017 at 21:49
  • @SteveGaines Please do read the help center article on what to do when someone answers your question. Commented Jan 20, 2017 at 23:21

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

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.