public
Last active

This is the Angular.js directive that converts html select boxes with Angularjs bindings into the Twitter Bootstrap dropboxes. See examples on jsfiddle: http://jsfiddle.net/M32pj/28/

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
angular.module('bsselect', [])
.directive('bsSelectbox', function ($parse) {
return {
restrict: 'A',
priority: 100,
transclude: true,
scope: {
themodel: '=ngModel',
thearray: '@ngOptions',
thechange: '&ngChange',
defaultval: '@bsSelectbox'
},
template:
'<div class="bs-selectbox btn-group">' +
'<button class="btn dropdown-toggle" data-toggle="dropdown" type="button">' +
'{{display}} ' +
'<span class="caret"></span>' +
'</button>' +
'<ul class="dropdown-menu">' +
'<li ng-show="defaultval">' +
'<a href="javascript:" ng-click="change(false)"> <span>{{defaultval}}</span> </a>' +
'</li>' +
'<li ng-show="defaultval" class="divider"></li>' +
'<li ng-repeat="itm in elements" ng-class="{active:itm.value==themodel}">' +
'<a href="javascript:" ng-click="change(itm)">' +
'<span>{{itm.label}}</span>' +
'</a>' +
'</li>' +
'</ul>' +
'<div style="display:none;" class="bs-selectbox-transclude" ng-transclude></div>' +
'</div>',
link: function (scope, element, attrs) {
scope.display = '--';
scope.elements = [];
attrs.$observe('bsSelectbox', function (value) {
if (value) scope.display = value;
});
attrs.$observe('ngOptions', function (value, element) {
if (angular.isDefined(value)) {
var match, loc = {};
var NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w\d]*)|(?:\(\s*([\$\w][\$\w\d]*)\s*,\s*([\$\w][\$\w\d]*)\s*\)))\s+in\s+(.*)$/;
if (match = value.match(NG_OPTIONS_REGEXP)) {
var displayFn = $parse(match[2] || match[1]),
valueName = match[4] || match[6],
valueFn = $parse(match[2] ? match[1] : valueName),
valuesFn = $parse(match[7]);
scope.$watch(function(){ return valuesFn(scope.$parent); }, function(newVal) {
var collection = newVal || [];
scope.elements = [];
angular.forEach(collection, function (value, key) {
loc[valueName] = collection[key];
scope.elements.push({
'label': displayFn(scope.$parent, loc),
'value': valueFn(scope.$parent, loc)
});
});
scope.setdefault();
});
}
}
});
scope.$watch('themodel', function (newval, oldval) {
scope.setdefault();
if(angular.isFunction(scope.thechange) && (newval != oldval)) {
scope.thechange();
}
});
scope.setdefault = function () {
angular.forEach(scope.elements, function (value, key) {
if (value.value == scope.themodel) scope.display = value.label;
});
}
scope.change = function (itm) {
if (!itm) {
scope.display = scope.defaultval;
scope.themodel = "";
} else {
scope.display = itm.label;
scope.themodel = itm.value;
}
}
var elements = element.find(".bs-selectbox-transclude").children();
if (angular.isObject(elements) && elements.length) {
angular.forEach(elements, function (value, key) {
scope.elements.push({
'label': value.innerText,
'value': value.value
});
});
scope.setdefault();
}
},
replace: true
};
});

Excellent Gist. Any change you can update it to work with angular 1.2.16? I found it referenced here - http://stackoverflow.com/questions/15090185/angularjs-bootstrap-dropdown-cant-do-ng-click-in-ng-repeat but when I change the fiddle to an updated version of angular, it breaks. It can't seem to populate the ul on line 19 with the updated values.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.