I'm trying to sort a table of data which is populated from a JSON source. The code I have is as follows:
HTML:
<div ng-app="myApp">
<div ng-controller="PurchasesCtrl">
<table cellspacing="0">
<tr class="first">
<th class="first" ng:click="changeSorting(purchases.date)">Date</th>
<th ng:click="changeSorting(purchases.text)">Description</th>
<th ng:click="changeSorting(purchases.price)">Amount</th>
<th ng:click="changeSorting(purchases.availability)">Status</th>
</tr>
<tr ng-repeat="purchase in purchases.data">
<td class="first">{{purchase.date}}</td>
<td>{{purchase.text}}</td>
<td>{{purchase.price}}</td>
<td>{{purchase.availability}}</td>
</tr>
</table>
</div>
</div>
JS:
var myApp = angular.module("myApp",[]);
myApp.factory("Purchases", function(){
var Purchases = {};
Purchases.data = [
{
date: "10/05/2012",
text: "1 Lorem ipsum dolor sit amet ipsum dolor",
price: "£123.45",
availability: "1 Available until 10th Dec 2013"
},
{
date: "24/05/2012",
text: "2 Lorem ipsum dolor sit amet ipsum dolor",
price: "£234.56",
availability: "2 Available until 10th Dec 2013"
},
{
date: "20/05/2012",
text: "3 Lorem ipsum dolor sit amet ipsum dolor",
price: "£345.67",
availability: "3 Available until 10th Dec 2013"
}
];
return Purchases;
});
function PurchasesCtrl($scope, Purchases){
$scope.purchases = Purchases;
$scope.changeSorting = function(column) {
var sort = $scope.sort;
if (sort.column == column) {
sort.descending = !sort.descending;
} else {
sort.column = column;
sort.descending = false;
}
};
}
Fiddle: http://jsfiddle.net/7czsM/1/
As you can see I've tried to add a click function to the table headers to call a function that sorts the data, but it's not working.
I've seen an example of this kind of thing which does work, here: http://jsfiddle.net/vojtajina/js64b/14/, but when I try to apply the same kind of thing to my scenario it breaks very quickly; for example, I tried adding the table headers programatically in JSON by adding the following:
var Purchases = {};
Purchases.head = [
{
date: "Date",
text: "Text column",
price: "Price column",
availability: "Availability column"
}
Purchases.data = [
{
date: "10/05/2012",
text: "1 Lorem ipsum dolor sit amet ipsum dolor",
price: "£123.45",
availability: "1 Available until 10th Dec 2013"
},
This just prevents anything from working, but I thought it would be possible to add multiple sets of data to an Angular variable?
I'm a total new-comer to Angular so I'm really stuck with this. Any pointers would be much appreciated, thanks.
Updated jsfiddle: http://jsfiddle.net/gweur/
sza is right, you did forget the $scope.sort object, but you are also missing the orderBy filter in your ng-repeat
|orderBy:sort.column:sort.descending
Additionally, you'll need to explicitly pass the column name to the changeSorting() function, like
ng-click="changeSorting('text')"
not sure if there is a different way you can handle this.
Finally, ng-click is the correct syntax for the version of AngularJS you are using.
Another very good example of making table sortable
http://jsfiddle.net/vojtajina/js64b/14/
<th ng:repeat="(i,th) in head" ng:class="selectedCls(i)" ng:click="changeSorting(i)">{{th}}</th>
scope.changeSorting = function(column) {
var sort = scope.sort;
if (sort.column == column) {
sort.descending = !sort.descending;
} else {
sort.column = column;
sort.descending = false;
}
};
Here is my solution. I also wrap the whole thing to a directive. The only dependency is UI.Bootstrap.pagination, which did a great job on pagination.
Here is the plunker
Here is the github source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With