Skip to content

Instantly share code, notes, and snippets.

@geraldhumphries
Last active November 11, 2015 15:46
Show Gist options
  • Save geraldhumphries/bd7190387ca711f1bb20 to your computer and use it in GitHub Desktop.
Save geraldhumphries/bd7190387ca711f1bb20 to your computer and use it in GitHub Desktop.
sort directive
<tr jh-sort="predicate" ascending="reverse" callback="loadAll">
<th jh-sort-by="id" translate="global.field.id">ID</th>
<th jh-sort-by="testField" translate="jhipsterApp.test123.testField">TestField</th>
<th jh-sort-by="point.id" translate="jhipsterApp.test123.point">point</th>
<th jh-sort-by="address.id" translate="jhipsterApp.test123.address">address</th>
<th></th>
</tr>
'use strict';
angular.module('jhipsterApp')
.directive('jhSort', function () {
return {
restrict: 'A',
scope: {
predicate: '=jhSort',
ascending: '=',
callback: '&'
},
controller: ['$scope', function ($scope) {
this.sort = function (field) {
if (field !== $scope.predicate) {
$scope.ascending = true;
} else {
$scope.ascending = !$scope.ascending;
}
$scope.predicate = field;
$scope.$apply();
$scope.callback();
}
}]
}
}).directive('jhSortBy', function () {
return {
restrict: 'A',
scope: false,
require: '^jhSort',
link: function (scope, element, attrs, parentCtrl) {
element.bind('click', function () {
parentCtrl.sort(attrs.jhSortBy);
});
}
};
});
@deepu105
Copy link

  1. I would name it jhSort and jhSortBy so that its in line for our future plugin lib roadmap
  2. may be you can map the predicate to main directive attribute itself and avoid another attribute
  3. For variable reverse may be we can use attribute name direction? I dont think directive attributes have to match scope var names
  4. We can put theglyphicon logic in the directive as well, i can help with that once you PR this
  5. We need to name the attribute func to something better, maybe callback or load or something, also i think ot need not be two way bound
  6. Instead of passing the function we can also emit a custom event and call the function by listening to that event, but it will be tricky if there are two tables on same page

Any way the directive def looks better than current logic so we can go ahead and improve on the way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment