Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Option to have a custom sort function #466

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ angular.module('demoApp').controller('sampleController', ['$nutrition', '$scope'
| `mdOnReorder` | `mdHead` | `function` | A callback function for when the order changes. The callback will receive the new order. |
| `mdOrder` | `mdHead` | `string` | A variable to bind the sort order to. |
| `mdOrderBy` | `mdColumn` | `string` | The value to bind to the sort order. |
| `mdCustomSort` | `mdColumn` | `string` | The custom function which translates data into values which can be ordered |

When the user clicks the `md-column` element, the value of the `md-order-by` attribute will be bound to the variable provided to the `md-order` attribute on the `md-head` element. If the column is already sorted by that value, a minus sign `-` will be prefixed to the value. For most query languages, this is the universal symbol to sort descending.

If the `md-custom-sort` element is used, the value of the `md-order-by` attribute will be bound to the function provided to `md-custom-sort`which will translate raw values to orderable values

The variable can be used to send a query to the server or as the `orderBy` property of an `ng-repeat` expression.

**Example Using ngRepeat**
Expand Down
15 changes: 14 additions & 1 deletion src/scripts/mdColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function mdColumn($compile, $mdUtil) {
function isActive() {
return scope.orderBy && (headCtrl.order === scope.orderBy || headCtrl.order === '-' + scope.orderBy);
}

function hasCustomSortFunc() {
return scope.customSortFunc;
}

function isNumeric() {
return attrs.mdNumeric === '' || scope.numeric;
Expand All @@ -56,9 +60,17 @@ function mdColumn($compile, $mdUtil) {
function setOrder() {
scope.$applyAsync(function () {
if(isActive()) {
if (hasCustomSortFunc()) {
headCtrl.order = scope.customSortFunc;
} else {
headCtrl.order = scope.getDirection() === 'md-asc' ? '-' + scope.orderBy : scope.orderBy;
}
} else {
if (hasCustomSortFunc()) {
headCtrl.order = scope.customSortFunc;
} else {
headCtrl.order = scope.getDirection() === 'md-asc' ? scope.orderBy : '-' + scope.orderBy;
}
}

if(angular.isFunction(headCtrl.onReorder)) {
Expand Down Expand Up @@ -121,8 +133,9 @@ function mdColumn($compile, $mdUtil) {
scope: {
numeric: '=?mdNumeric',
orderBy: '@?mdOrderBy'
customSortFunc: '=?mdCustomSort'
}
};
}

mdColumn.$inject = ['$compile', '$mdUtil'];
mdColumn.$inject = ['$compile', '$mdUtil'];