daniel-nagy / md-data-table

Material Design Data Table for Angular Material
MIT License
1.9k stars 520 forks source link

Change the order "name" with "name ASC" or order "-name" with name DESC #528

Open sumit-khajanchi opened 7 years ago

sumit-khajanchi commented 7 years ago

Hi, I have to change the value of query.order from"name" to "name ASC" or"-name" to "name DESC". What should i do, please help me to resolve this problem.

surfjedi commented 7 years ago

I think what you need is to just change in your controller from : $scope.query = { order: 'name', limit: 5, page: 1 };

To: $scope.query = { order: '-name', limit: 5, page: 1 };

Note the "-" in name to make descending

sumit-khajanchi commented 7 years ago

Hello Sir,

Actually i fired query in $http get request to fetch results but when i use order: -name wrong results fetched and when i used order: name DESC correct results were fetched but the sorting up/down direction arrow in data-table were not changing.

daniel-nagy commented 7 years ago

You'll want to use mdOnReorder to update your $http request. Let the table use name and -name so it knows the sort direction and just map the values to name ASC and name DESC.

mbenedettini commented 7 years ago

A bit late but anyway. I've set up the following method in my controller (this.order being what is changed by md-order and this.query from where the database query is built):

        this.onReorder = () => {
            let m = this.order.match(/(-?)(.*)/);
            let attr = m[2];
            let direction = m[1] ? 'DESC' : 'ASC';
            this.query.order = `${attr} ${direction}`;
            this.refresh();
        };