Skip to content

Instantly share code, notes, and snippets.

@davidecavaliere
Created January 6, 2014 15:47
Show Gist options
  • Save davidecavaliere/dcb18e0ae07d60a2e2d5 to your computer and use it in GitHub Desktop.
Save davidecavaliere/dcb18e0ae07d60a2e2d5 to your computer and use it in GitHub Desktop.
wijmo combobox directive for angularjs
// ** wij-combobox directive
// - Input element with auto-complete (shows list as the user types)
// - Implemented using the Wijmo wijcombobox widget.
// ** example
// <wij-combobox
// value="trip.from"
// source="comboItems" >
// </wij-combobox>
// ** see
// - Wijmo wijcombobox: http://wijmo.com/wiki/index.php/Combobox#API
.directive("wijCombobox", ["wijUtil", function (wijUtil) {
return {
restrict: "AE",
replace: true,
template: "<input/>",
scope: {
value: "=", // Current value.
source: "=" // List of valid choices (array of <code>{ value, label }</code> objects).
},
link: function (scope, element, attrs) {
// update the control when scope parameters are updated
scope.$watch("source", function () {
updateControl();
});
// update element value when scope changes (no need to re-create widget)
scope.$watch("value", function (newValue) {
element.wijcombobox({ text: newValue });
//element.val(newValue); // also works...
});
// update control
function updateControl() {
// build options object
var options = {
text: scope.value,
data: scope.source,
changed: function (event, args) {
if (args.selectedItem) {
wijUtil.apply(scope, "value", args.selectedItem.label);
}
},
dropdownHeight : 250
/*showingAnimation : {
animated: "blind",
duration: 300
},
hidingAnimation : {
animated: "clip",
duration: 300
}*/
};
// create combobox widget with given options
element.wijcombobox(options);
}
$(element).click(function(ev){
element.val("");
});
$(element).blur(function(ev){
element.val(scope.value);
});
$(window).resize(function() {
console.log("resizing to: ");
console.log($(element).parent().parent().parent().width());
var parent = $(element).parent().parent().parent();
element.parent().parent().width(parent.width()+"px");
element.wijcombobox({dropdownWidth : parent.width() + 4});
});
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment