0

I have two groups of arrays. First contains header fields =>

$scope.fields=[field0, field1, field2,...,field20];

Second has multiple arrays of data with respect to above field =>

$scope.data=[
    [A,B,C,...,someVal20],
    [Q,W,E,...,someVal20],
    [R,T,Y,...,someVal20],
    ...
    [B,N,M,...,someVal20]
];

Values of fields are shown in a drop down

<select ng-model="filteredOption" ng-options="option.label for option in fields"></select>

The same field values are used as table header.

<tr>
    <th ng-repeat="f in fields">{{f.label}}</th>
</tr>

And the data as

<tr ng-repeat="d in data">
    <td ng-repeat="i in d track by $index">{{i}}</td>
</tr>

Question is How do I sort (ascending) the data based on the 'nth' field (which user selects from drop down)? Its confusing because header and data are two distinct arrays, and data is not in key,value format.

Plunkr here

3
  • try providing a plnkr so others can help more easily Commented Jan 13, 2016 at 10:30
  • I think you should make a custom filter though Commented Jan 13, 2016 at 11:02
  • You may use some library method, e.g. underscore.js's sortBy Commented Jan 14, 2016 at 5:45

2 Answers 2

1

Column Header - ColumnName in you case it is field0

<th>
  <a href="#" ng-click="sortType = 'field0'; sortReverse = !sortReverse">
          field0
  </a>
</th>

On Column header click it sets the properties of sortType and sortReverse

<tr ng-repeat="d in data | orderBy:sortType:sortReverse"">
    <td ng-repeat="i in d track by $index">{{i}}</td>
</tr>

useful link for sorting https://scotch.io/tutorials/sort-and-filter-a-table-using-angular

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

Comments

0

Thanks to this answer, I've found working solution.

I tracked the selected field's position ($scope.sorter set to that $index). And used it as orderBy:sorter

Still, keeping this question open for others to suggest more acceptable solution.

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.