0

I am looking to order my list by a number of specific values on one property, and then some addtional properties once that is done.

The code I have is:

<tr ng-repeat-start="subcontractor in trade.SubContractors | orderBy:['Status':'PREF','Status':'APPR','Status':'LOGD','Status':'NIU','-AverageScores.AverageScore','-ProjectCount','SubcontractorName']">

Where the important bit (and the bit I can't get working) is:

'Status':'PREF','Status':'APPR','Status':'LOGD','Status':'NIU'

Is there a way of doing this in angular?

Simon
  • 1,966
  • 5
  • 21
  • 35

2 Answers2

2

I would instead implement a handler to process data. Process subcontractors before it's set, running each element through a handler and assigning "sortValue" property to each one.

Then simply call orderBy using the sortValue property. This way you would decouple sorting data from displaying data. Though don't use a filter to do so, as it would be quite expensive resource-vise.

Something like

        var statusSorting = ['PREF','APPR','LOGD','NIU'];

        function sortContractors(models) {
            var processed = [];

            angular.forEach(models, function(model){

                // logic to assign sortValue
                var statusIndex = statusSorting.indexOf(model.status);
                model.sortValue = statusIndex + 1;



            });

            return processed;
        }

        api.getData()
        .then(function(data){
            $scope.models = sortContractors(data);
        });

        // template
        <tr ng-repeat="model in models | orderBy:'sortValue'">

You can then control priority by changing status position in the array and ordering desc/asc.

Alternately: orderBy multiple fields in Angular

         <tr ng-repeat="model in models | orderBy:['sortValue', 'contractorName']">
Community
  • 1
  • 1
Evaldas Raisutis
  • 1,628
  • 4
  • 18
  • 32
0

Your answer had a few non-required parts so I've tweaked it to give me the following:

function getStatusSortOrder(subcontractors) {
    var statusSorting = ['PREF', 'APPR', 'LOGD', 'NIU'];

    angular.forEach(subcontractors, function (model) {                    
         statusIndex = statusSorting.indexOf(model.Status);
         model.StatusSortValue = statusIndex;
    });                
}
Simon
  • 1,966
  • 5
  • 21
  • 35