Last active
August 29, 2015 14:23
-
-
Save vinioliveira/b6c116c3a67f09ae309f to your computer and use it in GitHub Desktop.
Directive Para Sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive('nsSort', function() { | |
return { | |
restrict : 'A', | |
require: '^stTable', | |
link : function(scope, element, attr, ctrl) { | |
var index = 0; | |
var sortDesc = [,'DESC','ASC']; | |
var classAscent = 'ns-sort-ascent'; | |
var classDescent = 'ns-sort-descent'; | |
var stateClasses = [,classDescent, classAscent]; | |
var sortFn = scope.$eval(element.closest('table').attr('st-table-sort')); | |
function clearOtherSelectionsClass(){ | |
$('th.'+classAscent).removeClass(classAscent); | |
$('th.'+classDescent).removeClass(classDescent); | |
} | |
function sort(){ | |
index++; | |
var orderPostition = index % 3; | |
var classElement = element.addClass(stateClasses[orderPostition]) | |
var order = sortDesc[orderPostition]; | |
var property = attr.stProperty; | |
sortFn.apply(scope,[property, order]); | |
clearOtherSelectionsClass(); | |
if(orderPostition === 1){ | |
element | |
.removeClass(stateClasses[orderPostition + 1]) | |
.addClass(stateClasses[orderPostition]); | |
}else if(orderPostition == 2){ | |
element | |
.removeClass(stateClasses[orderPostition - 1]) | |
.addClass(stateClasses[orderPostition]); | |
}else{ | |
element | |
.removeClass(classAscent) | |
.removeClass(classDescent); | |
} | |
} | |
if(angular.isFunction(sortFn)){ | |
element.bind('click', function () { | |
sort(); | |
}); | |
} | |
} | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table st-table-sort="ctrl.sort"> | |
<thead> | |
<th st-property="nome">Nome Property</th> | |
</thead> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment