0

との 2 つがJson含まれていobjectsます。filefolder

sort、両方に共通の created、ip、または status プロパティのいずれかに基づいていますobjectssort使用するためにOrderby filter、適切なタイトルをクリックするとアクティブになります(各タイトルは異なるobject'sプロパティを表します)。

私のコード - プランカー

HTML

  <body ng-controller='VotesCtrl'>
    <div>
    <ul>
        <li class="check" ng-click=>
           <input type="checkbox" ng-model="master"></input>
        </li>
        <li class="created">
            <a href="#" ng-click="predicate = 'created'; reverse=!reverse">CREATED</a>
        </li>
        <li class="ip">
            <a href="#" ng-click="predicate = 'ip'; reverse=!reverse">IP ADDRESS</a>
        </li>
        <li class="status">
            <a href="#" ng-click="predicate = 'status'; reverse=!reverse">STATUS</a>
        </li>
    </ul>
    <ul ng-repeat="vote in votes | orderBy:predicate:reverse">
        <li ng-show="vote.folder" class="check">
          <input type="checkbox" ng-model="vote.cb"></input>
        </li>
        <li ng-show="vote.folder" class="created">
            {{vote.folder.created|date}}
        </li>
        <li ng-show="vote.folder" class="ip">
            {{vote.folder.ip}}
        </li>
        <li ng-show="vote.folder" class="status">
            {{vote.folder.status}}
        </li>
        <li ng-show="vote.file" class="check">
          <input type="checkbox" ng-model="vote.cb"></input>
        </li>
        <li ng-show="vote.file" class="created">
            {{vote.file.created|date}}
        </li>
        <li ng-show="vote.file" class="ip">
            {{vote.file.ip}}
        </li>
        <li ng-show="vote.file" class="status">
            {{vote.file.status}}
        </li>
    </ul>
   </div>
  </body>

コード:

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

//controllers
webApp.controller ('VotesCtrl', function ($scope, Votes) {
    $scope.votes  = Votes;

    $scope.statuses = ["Approved","Pending","Trash","Spam"];

    $scope.expand = function(vote) {
       console.log("show");
       $scope.vote = vote;
       $scope.ip = vote.ip;
       $scope.date = vote.created;

    };

    $scope.$watch('master', function(newVal, oldVal) {
      angular.forEach($scope.votes, function(vote) {
        vote.cb = newVal;
      });
    });

    $scope.change = function() {
      for(var i = 0; i < $scope.votes.length; i++) {
        if($scope.votes[i].cb) {
            $scope.votes[i].status = $scope.votes.status;
            $scope.votes[i].cb = false;
            $scope.master = false;
        }
          $scope.show = false;
      }
    };

});

//services
webApp.factory('Votes', [function() {

    //temporary repository till integration with DB this will be translated into restful get query
    var votes = [
        {
          folder: {
            id: 1,
            created: 1381583344653,
            updated: 1381583344653,
            ratingID: 4,
            rate: 5,
            ip: '198.168.0.0',
            status: 'APPROVED'
          }  
        },
        {
            file: {
                id: 111,
                created: 1381583344653,
                updated: 1381583344653,
                ratingID: 4,
                rate: 5,
                ip: '198.168.0.1',
                status: 'APPROVED'
            }
        },
        {
            file: {
                id: 2,
                created: 1382387322693,
                updated: 1381583344653,
                ratingID: 3,
                rate: 1,
                ip: '198.168.0.2',
                status: 'APPROVED'
            }
        },
        {
            file: {
                id: 22,
                created: 1382387322693,
                updated: 1382387322693,
                ratingID: 3,
                rate: 1,
                ip: '198.168.0.3',
                status: 'APPROVED'
            }
        },
        {
            ratingID: 3,
            file: {
                id: 222,
                created: 1382387327693,
                updated: 1382387327693,
                ratingID: 3,
                rate: 1,
                ip: '198.168.0.4',
                status: 'APPROVED'
            }
        },
        {
            ratingID: 2,
            file: {
                id: 3,
                created: 1383584342663,
                updated: 1383584342663,
                ratingID: 2,
                rate: 5,
                ip: '198.168.0.5',
                status: 'APPROVED'
            }
        },
        {
            ratingID: 2,
            file: {
            id: 4,
            created: 1384584432742,
            updated: 1384584432742,
            ratingID: 2,
            rate: 5,
            ip: '198.168.0.6',
            status: 'SPAM'
            }
        },
        {
          folder: {
            id: 44,
            created: 1384584532742,
            updated: 1384584532742,
            ratingID: 2,
            rate: 5,
            ip: '198.168.0.7',
            status: 'APPROVED'
          }
        },
        {
            ratingID:  2,
            file: {
                id: 444,
                created: 1384984232742,
                updated: 1384984232742,
                ratingID: 2,
                rate: 5,
                ip: '198.168.0.8',
                status: 'APPROVED'
            }
        },
        {
            ratingID: 1,
            file: {
                id: 5,
                created: 1385524346643,
                updated: 1385524346643,
                ratingID: 1,
                rate: 1,
                ip: '198.168.0.9',
                status: 'SPAM'
            }
        },
        {
            ratingID: 1,
            file: {
                id: 6,
                created: 1386585332621,
                updated: 1386585332621,
                ratingID: 1,
                rate: 1,
                ip: '198.168.0.10',
                status: 'APPROVED'
            }
        }
    ];
    return votes;
}]);

sortで適切に動作してlistいません。どのプロパティで を実行しているかは問題ではありませんsort

4

1 に答える 1

0

ドキュメントで示されているように、トリックは関数述語を使用することです:

$scope.getter = function(x) {
  if( x.file ) return x.file[$scope.predicate];
  else if( x.folder ) return x.folder[$scope.predicate];
  else return null;
};

そしてHTMLで:

<ul ng-repeat="vote in votes | orderBy:getter:reverse">

分岐したプランクを参照してください: http://plnkr.co/edit/sxKSegy9PgladAT8xu6I?p=preview

于 2013-10-08T12:14:05.977 に答える