Skip to content

Instantly share code, notes, and snippets.

@thinkjones
Created June 11, 2014 20:03
Show Gist options
  • Select an option

  • Save thinkjones/0e0b7d2260413b0697ca to your computer and use it in GitHub Desktop.

Select an option

Save thinkjones/0e0b7d2260413b0697ca to your computer and use it in GitHub Desktop.
Kendo Angular MultiSelect With Ability to Add New Items
'use strict';
angular.module('mainUI')
.directive('multiSelectDropdown', function () {
function validateAddingOfItem(value, scope) {
scope.errorMessage = "";
return true;
}
return {
restrict: 'E',
templateUrl : 'directives/multiSelectDropdown.html',
require: 'ngModel',
scope: {
ngModel: '=',
dropdownType: '=',
placeholder: '@'
},
replace: true,
transclude: false,
link: function(scope, elem, attr, ctrl) {
var tags = [];
var newitemtext;
scope.itemsOptions = {
placeholder: "The placeholder",
change: function() {
$('#tags_taglist li span:first-child').each(function() {
$(this).text($(this).text().replace(" (Add New)", ""));
});
},
dataBound: function() {
if((newitemtext || this._prev) && newitemtext != this._prev)
{
newitemtext = this._prev;
var dataitems = this.dataSource.data();
for (var i = 0; i < dataitems.length; i++) {
var dataItem = dataitems[i];
if(dataItem.value != dataItem.text) {
this.dataSource.remove(dataItem);
}
}
dataitems = this.dataSource.data();
var found = false;
for (var i = 0; i < dataitems.length; i++) {
var dataItem = dataitems[i];
if(dataItem.value == newitemtext) {
found = true;
}
}
if(!found)
{
this.dataSource.add({text: newitemtext + " (Add New)", value: newitemtext});
this.open();
}
}
},
dataTextField: "text",
dataValueField: "value",
animation: false
}
scope.itemsDataSource = new kendo.data.DataSource({data: []});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment