Skip to content

Commit 4f8ef4b

Browse files
author
pkempenaers
committed
Add sortByDisplayProperty setting
1 parent 5422757 commit 4f8ef4b

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed

src/app/component/dropdown-menu/dropdown-menu.template.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ng-keydown="$ctrl.searchKeydown($event)" />
1010
</div>
1111
<ul>
12-
<dropdown-tree-option-row ng-repeat="option in $ctrl.options track by $index"
12+
<dropdown-tree-option-row ng-repeat="option in $ctrl.options | orderBy:$ctrl.settings.displayProperty:false:$ctrl.customCompare track by $index"
1313
option="option"
1414
settings="$ctrl.settings"
1515
option-clicked="$ctrl.optionClicked(option)"
@@ -18,7 +18,8 @@
1818
focus-previous="$ctrl.focusPrevious()"
1919
catch-keydown="$ctrl.catchKeydown(event)"
2020
reset-focus-counter="$ctrl.resetFocusCounter(element)"
21-
search-text="{{$ctrl.searchText}}">
21+
search-text="{{$ctrl.searchText}}"
22+
custom-compare="$ctrl.customCompare">
2223
</dropdown-tree-option-row>
2324
</ul>
2425
</div>

src/app/component/dropdownTree.controller.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,24 @@
5858
foldersOpen: true,
5959
openFolderWhenInnerSelected: false,
6060
closeOnSelectionLimitReached: false,
61+
sortByDisplayProperty: false,
6162
};
6263

6364
this.settings = angular.extend({}, this.defaultSettings);
65+
66+
const vm = this;
67+
function customCompare(a, b) {
68+
if (!vm.settings.sortByDisplayProperty) {
69+
return 0;
70+
}
71+
if (a.value < b.value) {
72+
return -1;
73+
} else if (a.value > b.value) {
74+
return 1;
75+
}
76+
return 0;
77+
}
78+
vm.customCompare = customCompare;
6479
}
6580

6681
$onChanges(changes) {

src/app/component/option-row/option-row.component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default function optionRowComponent() {
1414
catchKeydown: '&',
1515
resetFocusCounter: '&',
1616
searchText: '@',
17+
customCompare: '<',
1718
},
1819
};
1920

src/app/component/option-row/option-row.template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</div>
2121
<ul class="children"
2222
ng-if="$ctrl.isFolder() && ($ctrl.isOpen || ($ctrl.shouldBeVisible() && $ctrl.searchText.length !== 0))">
23-
<dropdown-tree-option-row ng-repeat="option in $ctrl.getChildOptions() track by $index"
23+
<dropdown-tree-option-row ng-repeat="option in $ctrl.getChildOptions() | orderBy:$ctrl.settings.displayProperty:false:$ctrl.customCompare track by $index"
2424
option="option"
2525
settings="$ctrl.settings"
2626
option-clicked="$ctrl.innerClicked(option)"

src/app/main/main.controller.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@
88
$onInit() {
99
this.options = [
1010
{
11-
name: 'testoption',
11+
name: 'b',
1212
label: 'testoption label',
1313
},
1414
{
15-
name: 'test folder',
15+
name: 'a',
1616
label: 'test folder label',
1717
children: [
1818
{
19-
name: 'test child option',
20-
label: 'test child option label',
19+
name: 'c',
20+
label: 'test child option c',
21+
},
22+
{
23+
name: 'f',
24+
label: 'test child option f',
25+
},
26+
{
27+
name: 'd',
28+
label: 'test child option d',
2129
},
2230
],
2331
childoptions: [

src/app/main/main.template.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ <h3>Example settings</h3>
129129
closeOnSelectionLimitReached
130130
</label>
131131
</div>
132+
<div>
133+
<label>
134+
<input type="checkbox"
135+
ng-model="$ctrl.settings.sortByDisplayProperty"
136+
ng-change="$ctrl.changeSettings()" />
137+
sortByDisplayProperty
138+
</label>
139+
</div>
132140
</div>
133141
</div>
134142
</div>
@@ -309,6 +317,14 @@ <h3>Settings</h3>
309317
Only applicable when selectionLimit different from 0. Will close the dropdown when selection reaches the specified selectionLimit
310318
</td>
311319
</tr>
320+
<tr>
321+
<td>sortByDisplayProperty</td>
322+
<td>false</td>
323+
<td>boolean</td>
324+
<td>
325+
Will sort the options by the displayProperty
326+
</td>
327+
</tr>
312328
</tbody>
313329
</table>
314330
</div>

0 commit comments

Comments
 (0)