Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save WebReflection/dad237e79f26cff47131 to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/dad237e79f26cff47131 to your computer and use it in GitHub Desktop.

My table orderer ... instead of the filter

<!DOCTYPE html>
<html ng-app="tasteAngularJS">
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
      <link rel="stylesheet" 
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
      <link rel="stylesheet" 
      href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
      <link rel="stylesheet" href="style.css">
    </head>
    <body class="container">
      <div class="content" ng-controller="coffeeController">
          <table class="table table-condensed">
            <tr>
              <th ng-click="orderBy('name')">Name</th>
              <th ng-click="orderBy('sfLocation')">SF Location</th>
            </tr>
            <tr ng-repeat="coffee in coffeeShopsList">
              <td>{{ coffee.name }}</td>
              <td>{{ coffee.sfLocation }}</td>
            </tr>
          </table>
        </div>
        <script>
            angular
              .module('tasteAngularJS', [])
              .controller('coffeeController', function ($scope) {
                var order;
                $scope.orderBy = function (key) {
                  order[key] *= -1;
                  $scope.coffeeShopsList.sort(function (a, b) {
                    return (a[key] < b[key] ? -1 : 1) * order[key];
                  });
                };
                $scope.coffeeShopsList = [
                  { 'name': 'Ritual Coffee', 'sfLocation': 'Hayes Valley' },
                  { 'name': 'Blue Bottle', 'sfLocation': 'Hayes Valley' },
                  { 'name': 'Coffee Bar', 'sfLocation': 'FiDi' }
                ];
                order = Object.keys($scope.coffeeShopsList[0]).reduce(function (order, key) {
                  order[key] = -1;
                  return order;
                }, {});
              })
            ;
        </script>
    </body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment