Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
  2. <script>
  3.  
  4. function TreeController($scope) {
  5.  
  6.   //initial state
  7.   $scope.trees = [
  8.     {name:'Larch', category:'Conifer'},
  9.     {name:'Doug Fir', category:'Conifer'},
  10.     {name:'Cedar', category:'Conifer'},
  11.     {name:'Birch', category:'Deciduous'},
  12.     {name:'Maple', category:'Deciduous'},
  13.     {name:'Oak', category:'Deciduous'}];
  14.  
  15.   $scope.orderByField = 'name';
  16.  
  17.   // add tree button
  18.   $scope.add = function() {
  19.     var newTree = {name:$scope.name, category: $scope.category};
  20.     $scope.trees.push(newTree);
  21.    
  22.     // clear the filter so the user will see the new tree
  23.     $scope.nameFilter = '';
  24.    
  25.     // reset the form and its validation
  26.     $scope.name = '';
  27.     $scope.category = '';
  28.     $scope.form.$setPristine(true);
  29.   };
  30.  
  31.   // changes sorting, triggered by clicking <th> tags in table
  32.   $scope.updateOrdering = function(newOrderByField) {
  33.    
  34.     // reverse the order if the click the same column again
  35.     if($scope.orderByField == newOrderByField) {
  36.         $scope.orderReversed = !$scope.orderReversed;
  37.     } else {
  38.         $scope.orderReversed = false;
  39.     }
  40.  
  41.     $scope.orderByField = newOrderByField;
  42.    
  43.   };
  44.  
  45. }
  46. </script>