Skip to content

Commit

Permalink
Allow ordering by expression as well as string
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Valencik committed Mar 6, 2015
1 parent 7d9eac2 commit 9ca7f55
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Attributes of angular treecontrol
- `labelSelected` : inject classes into the div element around the label only when the node is selected
- `order-by` : value for ng-repeat to use for ordering sibling nodes
- `reverse-order` : whether or not to reverse the ordering of sibling nodes based on the value of `order-by`
- `order-by-expression` : Expression evaluated by ng-repeat to use for ordering sibling nodes
- `filter-expression` : value for ng-repeat to use for filtering the sibling nodes
- `filter-comparator` : value for ng-repeat to use for comparing nodes with the filter expression

Expand Down
8 changes: 7 additions & 1 deletion angular-tree-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
onNodeToggle: "&",
options: "=?",
orderBy: "@",
orderByExpression: "=?",
reverseOrder: "@",
filterExpression: "=?",
filterComparator: "=?"
Expand Down Expand Up @@ -197,7 +198,12 @@
};

//tree template
var orderBy = $scope.orderBy ? ' | orderBy:orderBy:reverseOrder' : '';
var orderBy = '';
if ($scope.orderByExpression) {
orderBy = ' | orderBy:orderByExpression:reverseOrder';
} else if ($scope.orderBy) {
orderBy = ' | orderBy:orderBy:reverseOrder';
}
var template =
'<ul '+classIfDefined($scope.options.injectClasses.ul, true)+'>' +
'<li ng-repeat="node in node.' + $scope.options.nodeChildren + ' | filter:filterExpression:filterComparator ' + orderBy + '" ng-class="headClass(node)" '+classIfDefined($scope.options.injectClasses.li, true)+'>' +
Expand Down
62 changes: 33 additions & 29 deletions test/angular-tree-control-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('treeControl', function() {
var $compile, $rootScope, element, num;
var $compile, $rootScope, orderBy, element, num;

beforeEach(function () {
module('treeControl');
Expand Down Expand Up @@ -357,34 +357,38 @@ describe('treeControl', function() {
expect(element.find('li:eq(2)').text()).toBe('b');
});

it('should order sibling nodes in normal order', function() {
$rootScope.treedata = [
{ label: "a", children: [] },
{ label: "c", children: [] },
{ label: "b", children: [] }
];
$rootScope.predicate = 'label';
$rootScope.reverse = false;
element = $compile('<treecontrol tree-model="treedata" order-by="{{predicate}}" reverse-order="{{reverse}}">{{node.label}}</treecontrol>')($rootScope);
$rootScope.$digest();
expect(element.find('li:eq(0)').text()).toBe('a');
expect(element.find('li:eq(1)').text()).toBe('b');
expect(element.find('li:eq(2)').text()).toBe('c');
});

it('should order sibling nodes in reverse order', function() {
$rootScope.treedata = [
{ label: "a", children: [] },
{ label: "c", children: [] },
{ label: "b", children: [] }
];
$rootScope.predicate = 'label';
$rootScope.reverse = true;
element = $compile('<treecontrol tree-model="treedata" order-by="{{predicate}}" reverse-order="{{reverse}}">{{node.label}}</treecontrol>')($rootScope);
$rootScope.$digest();
expect(element.find('li:eq(0)').text()).toBe('c');
expect(element.find('li:eq(1)').text()).toBe('b');
expect(element.find('li:eq(2)').text()).toBe('a');
[false, true].forEach(function (orderbyExpression) {
it('should order sibling nodes in normal order', function() {
$rootScope.treedata = [
{ label: "a", children: [] },
{ label: "c", children: [] },
{ label: "b", children: [] }
];
$rootScope.predicate = 'label';
$rootScope.reverse = false;
orderBy = orderbyExpression ? 'order-by-expression="predicate"' : 'order-by="{{predicate}}"';
element = $compile('<treecontrol tree-model="treedata" ' + orderBy + ' reverse-order="{{reverse}}">{{node.label}}</treecontrol>')($rootScope);
$rootScope.$digest();
expect(element.find('li:eq(0)').text()).toBe('a');
expect(element.find('li:eq(1)').text()).toBe('b');
expect(element.find('li:eq(2)').text()).toBe('c');
});

it('should order sibling nodes in reverse order', function() {
$rootScope.treedata = [
{ label: "a", children: [] },
{ label: "c", children: [] },
{ label: "b", children: [] }
];
$rootScope.predicate = 'label';
$rootScope.reverse = true;
orderBy = orderbyExpression ? 'order-by-expression="predicate"' : 'order-by="{{predicate}}"';
element = $compile('<treecontrol tree-model="treedata" ' + orderBy + ' reverse-order="{{reverse}}">{{node.label}}</treecontrol>')($rootScope);
$rootScope.$digest();
expect(element.find('li:eq(0)').text()).toBe('c');
expect(element.find('li:eq(1)').text()).toBe('b');
expect(element.find('li:eq(2)').text()).toBe('a');
});
});

it('should be able to accept alternative children variable name', function () {
Expand Down

0 comments on commit 9ca7f55

Please sign in to comment.