2
2
3
3
var directiveModule = angular . module ( 'angularjs-dropdown-multiselect' , [ ] ) ;
4
4
5
- directiveModule . run ( [ '$templateCache' , function ( $templateCache )
6
- {
7
- var template = '<div class="multiselect-parent btn-group dropdown-multiselect" data-ng-class="{open: open}">' ;
8
- template += '<button type="button" class="btn btn-default dropdown-toggle" data-ng-click="open=!open;">{{getButtonText()}}<span class="caret"></span></button>' ;
9
- template += '<ul class="dropdown-menu">' ;
10
- template += '<li><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> Check All</a>' ;
11
- template += '<li><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> Uncheck All</a></li>' ;
12
- template += '<li class="divider"></li>' ;
13
- template += '<li data-ng-repeat="option in options"><a data-ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))"><span data-ng-class="isChecked(getPropertyForObject(option,settings.idProp))"></span>{{getPropertyForObject(option, settings.displayProp)}}</a></li>' ;
14
- template += '</ul>' ;
15
- template += '</div>' ;
16
-
17
- $templateCache . put ( 'dropdown-multiselect-template.html' , template ) ;
18
- } ] ) ;
19
-
20
- directiveModule . directive ( 'ngDropdownMultiselect' , [ '$filter' , '$document' , function ( $filter , $document ) {
5
+ directiveModule . directive ( 'ngDropdownMultiselect' , [ '$filter' , '$document' , '$compile' , function ( $filter , $document , $compile ) {
21
6
22
7
return {
23
8
restrict : 'AE' ,
@@ -26,8 +11,42 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', func
26
11
options : '=' ,
27
12
extraSettings : '='
28
13
} ,
29
- templateUrl : 'dropdown-multiselect-template.html' ,
30
- link : function ( $scope , $element ) {
14
+ template : function ( element , attrs )
15
+ {
16
+ var checkboxes = attrs . checkboxes ? true : false ;
17
+
18
+ var template = '<div class="multiselect-parent btn-group dropdown-multiselect" data-ng-class="{open: open}">' ;
19
+ template += '<button type="button" class="btn btn-default dropdown-toggle" ng-click="toggleDropdown()">{{getButtonText()}}<span class="caret"></span></button>' ;
20
+ template += '<ul class="dropdown-menu dropdown-menu-form">' ;
21
+ template += '<li><a data-ng-click="selectAll()"><span class="glyphicon glyphicon-ok"></span> Check All</a>' ;
22
+ template += '<li><a data-ng-click="deselectAll();"><span class="glyphicon glyphicon-remove"></span> Uncheck All</a></li>' ;
23
+ template += '<li class="divider"></li>' ;
24
+
25
+ if ( checkboxes )
26
+ {
27
+ template += '<li data-ng-repeat="option in options"><a ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))"><div class="checkbox"><label><input class="checkboxInput" type="checkbox" ng-click="checkboxClick($event, getPropertyForObject(option,settings.idProp))" ng-checked="isChecked(getPropertyForObject(option,settings.idProp))" /> {{getPropertyForObject(option, settings.displayProp)}}</label></div></a></li>' ;
28
+ }
29
+ else {
30
+ template += '<li data-ng-repeat="option in options"><a ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))"><span data-ng-class="{\'glyphicon glyphicon-ok\': isChecked(getPropertyForObject(option,settings.idProp))}"></span> {{getPropertyForObject(option, settings.displayProp)}}</a></li>' ;
31
+ }
32
+
33
+ template += '</ul>' ;
34
+ template += '</div>' ;
35
+
36
+ element . html ( template ) ;
37
+ } ,
38
+ link : function ( $scope , $element , $attrs ) {
39
+ $scope . toggleDropdown = function ( )
40
+ {
41
+ $scope . open = ! $scope . open ;
42
+ } ;
43
+
44
+ $scope . checkboxClick = function ( $event , id )
45
+ {
46
+ $scope . setSelectedItem ( id ) ;
47
+ $event . stopImmediatePropagation ( ) ;
48
+ } ;
49
+
31
50
$scope . settings = {
32
51
dynamicTitle : true ,
33
52
defaultText : 'Select' ,
@@ -143,9 +162,10 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', func
143
162
144
163
$scope . isChecked = function ( id ) {
145
164
if ( _ . findIndex ( $scope . selectedModel , getFindObj ( id ) ) !== - 1 ) {
146
- return 'glyphicon glyphicon-ok' ;
165
+ return true ;
147
166
}
148
- return '' ;
167
+
168
+ return false ;
149
169
} ;
150
170
}
151
171
} ;
0 commit comments