Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sortable table columns with AngularJs

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.

like image 356
Dan Avatar asked Sep 13 '13 15:09

Dan


3 Answers

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.

like image 74
kmdsax Avatar answered Nov 16 '22 07:11

kmdsax


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;
    }
};
like image 32
Srisudhir T Avatar answered Nov 16 '22 08:11

Srisudhir T


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.

like image 35
maxisam Avatar answered Nov 16 '22 06:11

maxisam