Skip to content

Instantly share code, notes, and snippets.

@DouglasHennrich
Last active August 29, 2015 14:13
Show Gist options
  • Save DouglasHennrich/11bc1ef72cbca654f0dd to your computer and use it in GitHub Desktop.
Save DouglasHennrich/11bc1ef72cbca654f0dd to your computer and use it in GitHub Desktop.
<!doctype html>
<html data-ng-app="workshopBeMEAN">
<head>
<title>{{ workshop }}</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
<div data-ng-controller='BeerController'>
{{ cervejas }}
<pre>Ordenando por predicate = {{predicate}}; reverse = {{reverse}}</pre>
<table>
<thead>
<tr>
<th data-ng-click="ordenar('name')">Name</th>
<th data-ng-click="ordenar('price')">Price</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat='cerveja in cervejas | orderBy:predicate:reverse'>
<td>{{ cerveja.name }}</td>
<td>{{ cerveja.price }}</td>
</tr>
</tbody>
</table>
</div>
<script src="angular.min.js"></script>
<script>
angular.module('workshopBeMEAN', ['workshopFilters'])
.controller('BeerController', BeerController);
function BeerController ($scope){
$scope.reverse = false;
$scope.predicate = 'name';
$scope.ordenar = function(predicate) {
$scope.predicate = predicate;
$scope.reverse = !$scope.reverse;
}
// criamos um array de cervejas
var cervejas = [{
name: 'Kaiser', price: 2
}, {
name: 'Skol', price: 3
}, {
name: 'Glacial', price: 4
}, {
name: 'Polar', price: 6
}, {
name: 'Heineken', price: 10
}
];
// instanciamos nosso array no nosso scope
// para que tenhamos acesso à esse array na View
$scope.cervejas = cervejas;
};
BeerController['$inject'] = ['$scope'];
angular.module('workshopFilters', [])
.filter('reverseName', function () {
return function (text) {
if(text)
return text.split("").reverse().join("");
};
})
.filter('truncate', function () {
return function (text, length, end) {
if (isNaN(length))
length = 10;
if (end === undefined)
end = "...";
if (text.length <= length || text.length - end.length <= length) {
return text;
}
else {
return String(text).substring(0, length-end.length) + end;
}
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment