@@ -22,7 +22,8 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
2222 events : '=' ,
2323 searchFilter : '=?' ,
2424 translationTexts : '=' ,
25- groupBy : '@'
25+ groupBy : '@' ,
26+ groups : '='
2627 } ,
2728 template : function ( element , attrs ) {
2829 var checkboxes = attrs . checkboxes ? true : false ;
@@ -33,6 +34,8 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
3334 template += '<ul class="dropdown-menu dropdown-menu-form" ng-if="open" ng-style="{display: open ? \'block\' : \'none\', height : settings.scrollable ? settings.scrollableHeight : \'auto\', overflow: \'auto\' }" >' ;
3435 template += '<li ng-if="settings.showCheckAll && settings.selectionLimit !== 1"><a ng-keydown="keyDownLink($event)" data-ng-click="selectAll()" tabindex="-1" id="selectAll"><span class="glyphicon glyphicon-ok"></span> {{texts.checkAll}}</a>' ;
3536 template += '<li ng-if="settings.showUncheckAll"><a ng-keydown="keyDownLink($event)" data-ng-click="deselectAll();" tabindex="-1" id="deselectAll"><span class="glyphicon glyphicon-remove"></span> {{texts.uncheckAll}}</a></li>' ;
37+ template += '<li ng-if="currentGroups" class="divider"></li>' ;
38+ template += '<li ng-repeat="currentGroup in currentGroups track by $index"><a ng-bind="::currentGroup.title" ng-click="selectCurrentGroup(currentGroup)" ng-class="{\'dropdown-selected-group\': selectedGroup == currentGroup.value}" tabindex="-1"></a></li>' ;
3639 template += '<li ng-if="settings.showEnableSearchButton && settings.enableSearch"><a ng-keydown="keyDownLink($event); keyDownToggleSearch();" ng-click="toggleSearch($event);" tabindex="-1">{{texts.disableSearch}}</a></li>' ;
3740 template += '<li ng-if="settings.showEnableSearchButton && !settings.enableSearch"><a ng-keydown="keyDownLink($event); keyDownToggleSearch();" ng-click="toggleSearch($event);" tabindex="-1">{{texts.enableSearch}}</a></li>' ;
3841 template += '<li ng-if="(settings.showCheckAll && settings.selectionLimit > 0) || settings.showUncheckAll || settings.showEnableSearchButton" class="divider"></li>' ;
@@ -80,7 +83,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
8083 angular . element ( $element ) [ 0 ] . querySelector ( '.option' ) . focus ( ) ;
8184 } , 0 ) ;
8285 }
83- }
86+ }
8487 }
8588 } ;
8689
@@ -157,6 +160,34 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
157160 }
158161 } ) ;
159162
163+ $scope . $watch ( 'groups' , function ( newVal ) {
164+ if ( newVal ) {
165+ $scope . currentGroups = [ ] ;
166+ for ( var i = 0 ; i < $scope . groups . length ; ++ i ) {
167+ $scope . currentGroups . push ( { value : $scope . groups [ i ] , title : 'All ' + $scope . groups [ i ] } ) ;
168+ }
169+ }
170+ } ) ;
171+
172+ $scope . selectCurrentGroup = function ( currentGroup ) {
173+ $scope . selectedGroup = currentGroup . value ;
174+ if ( $scope . orderedItems ) {
175+ for ( var i = 0 ; i < $scope . orderedItems . length ; i ++ ) {
176+ var item = $scope . orderedItems [ i ] ;
177+ var indexInSelected = $scope . selectedModel . map ( function ( it ) { return it . id ; } ) . indexOf ( item . id ) ;
178+ if ( item [ $scope . groupBy ] == currentGroup . value ) {
179+ if ( indexInSelected == - 1 ) {
180+ $scope . selectedModel . push ( { id : item . id } ) ;
181+ }
182+ } else {
183+ if ( indexInSelected != - 1 ) {
184+ $scope . selectedModel . splice ( indexInSelected , 1 ) ;
185+ }
186+ }
187+ }
188+ }
189+ } ;
190+
160191 angular . extend ( $scope . settings , $scope . extraSettings || [ ] ) ;
161192 angular . extend ( $scope . externalEvents , $scope . events || [ ] ) ;
162193 angular . extend ( $scope . texts , $scope . translationTexts ) ;
@@ -277,6 +308,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
277308 $scope . setSelectedItem ( value [ $scope . settings . idProp ] , true , false ) ;
278309 } ) ;
279310 $scope . externalEvents . onSelectionChanged ( ) ;
311+ $scope . selectedGroup = null ;
280312 } ;
281313
282314 $scope . deselectAll = function ( dontSendEvent ) {
@@ -294,6 +326,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
294326 if ( ! dontSendEvent ) {
295327 $scope . externalEvents . onSelectionChanged ( ) ;
296328 }
329+ $scope . selectedGroup = null ;
297330 } ;
298331
299332 $scope . setSelectedItem = function ( id , dontRemove , fireSelectionChange ) {
@@ -332,6 +365,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
332365 if ( fireSelectionChange ) {
333366 $scope . externalEvents . onSelectionChanged ( ) ;
334367 }
368+ $scope . selectedGroup = null ;
335369 } ;
336370
337371 $scope . isChecked = function ( id ) {
@@ -443,13 +477,13 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
443477 }
444478 }
445479 } ;
446-
480+
447481 $scope . getFilter = function ( searchFilter ) {
448482 var filter = { } ;
449483 filter [ $scope . settings . searchField ] = searchFilter ;
450484 return filter ;
451485 } ;
452-
486+
453487 $scope . toggleSearch = function ( $event ) {
454488 if ( $event ) {
455489 $event . stopPropagation ( ) ;
@@ -459,7 +493,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
459493 $scope . input . searchFilter = '' ;
460494 }
461495 } ;
462-
496+
463497 $scope . keyDownToggleSearch = function ( ) {
464498 if ( ! $scope . settings . keyboardControls ) {
465499 return ;
0 commit comments