Last active
March 1, 2017 22:17
-
-
Save ethanph5/6347265 to your computer and use it in GitHub Desktop.
AngularJS directive using bootstrap-multiselect that written in Coffeescript and with underscore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('appModule') | |
.directive 'appDirective', -> | |
($scope, elem, attr) -> | |
elem.multiselect | |
buttonClass : 'btn btn-small' | |
buttonWidth : '200px' | |
buttonContainer : '<div class="btn-group" />' | |
maxHeight : 200 | |
includeSelectAllOption: true | |
buttonText: (options) -> | |
if options.length is 0 | |
return elem.data()['placeholder'] + ' <b class="caret"></b>' | |
else if options.length > 1 | |
return _.first(options).text + ' + ' + (options.length - 1) + ' more selected <b class="caret"></b>' | |
else | |
return _.first(options).text + ' <b class="caret"></b>' | |
# Replicate the native functionality on the elements so | |
# that angular can handle the changes for us. | |
onChange: (optionElement, checked) -> | |
optionElement.removeAttr('selected') | |
if checked | |
optionElement.attr('selected', 'selected') | |
elem.change() | |
# Watch for any changes to the length of our select element | |
$scope.$watch () -> | |
return elem[0].length | |
, () -> | |
elem.multiselect('rebuild') | |
# Watch for any changes from outside the directive and refresh | |
$scope.$watch(attr.ngModel, () -> | |
elem.multiselect('refresh')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment