Angular: multiple select
http://angularjs.org/
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://davidstutz.github.io/bootstrap-multiselect/js/bootstrap-multiselect.js"></script>
<script src="http://code.angularjs.org/1.1.5/angular.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.0/underscore-min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">
<div ng-controller="MyCtrl">
Hello, {{name}}!
<select class="multiselect" data-placeholder="Select Products"
ng-model="productSelection" ng-options="item as item for item in Products"
multiple="multiple" multiselect-dropdown >
</select>
<p>Selection: {{productSelection}}</p>
</div>
JavaScript
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
myApp.directive('multiselectDropdown', [function() {
return function(scope, element, attributes) {
element = $(element[0]); // Get the element as a jQuery element
// Below setup the dropdown:
element.multiselect({
buttonClass : 'btn btn-small',
buttonWidth : '200px',
buttonContainer : '<div class="btn-group" />',
maxHeight : 200,
enableFiltering : true,
enableCaseInsensitiveFiltering: true,
buttonText : function(options) {
if (options.length == 0) {
return element.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: function (optionElement, checked) {
optionElement.removeAttr('selected');
if (checked) {
optionElement.attr('selected', 'selected');
}
element.change();
}
});
// Watch for any changes to the length of our select element
scope.$watch(function () {
return element[0].length;
}, function () {
element.multiselect('rebuild');
});
// Watch for any changes from outside the directive and refresh
scope.$watch(attributes.ngModel, function () {
...