Sortable table
by dandoyon
HTML
<script type="text/javascript" ng:autobind
src="http://code.angularjs.org/0.10.5/angular-0.10.5.js"></script>
<table ng:controller="SortableTableCtrl">
<thead>
<tr>
<th ng:click="chgOrder(i)" ng:class="selectColumn(i)" ng:repeat="(i,th) in head">{{th}}</th>
</tr>
</thead>
<tbody>
<tr ng:repeat="row in body.$orderBy(selectedColumn,descending)">
<td>{{row.a}}</td>
<td>{{row.b}}</td>
<td>{{row.c}}</td>
</tr>
</tbody>
</table>
CSS
td { padding: 0.2em 1em; }
th { text-align: center; }
th span { padding-right: 2px; } /* gap so asc/desc images are not flush */
thead {
border-bottom: 2px solid black;
cursor: pointer;
}
/* http://www.greywyvern.com/code/php/binary2base64 */
th.selectedColumnDesc:after {
content:...
JavaScript
function SortableTableCtrl() {
var scope = this;
scope.selectedColumn = 'b';
scope.descending = false;
scope.head = {
a: "Name",
b: "Surname",
c: "City"
};
scope.body = [
{
a: "Hans",
b: "Mueller",
c: "Leipzig"},
{
a: "Dieter",
b: "Zumpe",
c: "Berlin"},
{
a: "Bernd",
b: "Danau",
c: "Muenchen"}
];
scope.chgOrder = function(i) {
scope.selectedColumn = i;
scope.descending = !scope.descending;
};
scope.selectColumn = function(i) {
if(scope.selectedColumn === i) {
if(scope.descending) {
return "selectedColumnDesc";
} else {
return "selectedColumnAsc";
}
}
return '';
}
};