19

Having issues with getting this filter to work.

$scope.imgCollection.then(function (images) {
    $scope.images = images.thisGal_images;

    if ($scope.images[0].order == '0') {
        console.log('orgName');
        $scope.images = $filter('orderBy')($scope.images, 'orgName');
    } else {
        console.log('sort order');
        $scope.images = $filter('orderBy')($scope.images, 'sortOrder');
        console.log($scope.images);
    }
});

$scope.images returns a list of images from the database. On the initial upload, the sortOrder column is populated with '0' as they can be sorted via ui:sortable. So on the initial view I base the sort order on the file name. After the initial view the DB is written and the first image is given the sortOrder of 1 and increments from there.

This could be my misunderstanding of $filter, but $scope.images = $filter('orderBy')($scope.images,'sortOrder'); is not ordering my $scope.images based on sortOrder.

Thanks

2
  • How is it ordering your images? What exactly is happening and what do you want to happen? Can you give a small example? Commented Aug 30, 2013 at 20:44
  • What is the type of sortOrder? string or int? Commented Aug 30, 2013 at 20:47

1 Answer 1

41

I created a demo for you and hopefully you can compare the code and figure out the issue.

I guess you might forget to inject $filter module. Please take a look a the demo.

<div ng-app="myApp" ng-controller="ctrl">
    <div ng-repeat="image in images">{{image}}</div>
    <button ng-click="order('0')">By orgName</button>
    <button ng-click="order('1')">By sortOrder</button>
</div>

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

function ctrl($scope, $filter) {
    $scope.images = [{
        orgName: 'B',
        sortOrder: 111
    }, {
        orgName: 'A',
        sortOrder: 12
    }, {
        orgName: 'D',
        sortOrder: 13
    }, {
        orgName: 'C',
        sortOrder: 14
    }];

    $scope.order = function (order) {
        if (order == '0') {
            $scope.images = $filter('orderBy')($scope.images, 'orgName');
        } else {
            $scope.images = $filter('orderBy')($scope.images, 'sortOrder');
        }
    }
}

Working Demo

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

6 Comments

taking a closer look, it appears that when I sort by sortOrder, which is set to INT in DB. The images are ordered: 1,10,11,12,2,3,4,5,6 etc. So probably getting converted to json string in my PHP
@Bungdaddy Yep, then you need to convert it to int.
thanks, that was the key. Example helped me figure that one out
thanks for the JSFiddle, I was curious how does the orderBy filter work on a boolean column, so I extended it: jsfiddle.net/M8nDQ/149
How can i add ascending/descending order ?
|

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.