angularjs - select2 - tagging with objects
http://angularjs.org/
HTML
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.1/css/bootstrap.css" rel="stylesheet">
<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.0/select2.css" rel="stylesheet" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.1/bootstrap.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.0/select2.min.js"></script>
<script src="https://raw.github.com/angular-ui/ui-select2/master/src/select2.js"></script>
<div style="border: 1px solid red;margin-bottom:10px;">angular-ui + select2
<div ng-controller="MyCtrl">
<input ng-change="showDialog(tagsSelections)" type="text" ui-select2="tagAllOptions" ng-model="tagsSelections" style="width:300px;" /> <pre>
tagsSelection: {{tagsSelections | json}}
</pre>
</div>
</div>
</div>
</div>
JavaScript
var myApp = angular.module('myApp', ['ui.select2']);
function MyCtrl($scope, $timeout) {
// Initialize with Objects.
$scope.tagsSelection = [{
"id": "01",
"text": "Perl"
}, {
"id": "03",
"text": "JavaScript"
}];
$scope.showDialog = function (item) {
console.log(item);
};
$timeout(function () {
$scope.tagsSelection.push({
'id': '02',
'text': 'Java'
});
}, 3000);
$scope.tagData = [{
"id": "01",
"text": "Perl"
}, {
"id": "02",
"text": "Java"
}, {
"id": "03",
"text": "JavaScript"
}, {
"id": "04",
"text": "Scala"
}];
$scope.formatResult = function (data) {
var markup;
if (data.n === "new") markup = "<div> <button class='btn-success btn-margin'><i class='icon-plus icon-white'></i> Create :" + data.text + "</button></div>";
else markup = "<div>" + data.text + "</div>";
return markup;
};
$scope.formatSelection = function (data) {
return "<b>" + data.text + "</b></div>";
};
$scope.tagAllOptions = {
multiple: true,
data: $scope.tagData,
tokenSeparators: [","],
createSearchChoice: function (term, data) {
if ($(data).filter(function () {
return this.v.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text : term,
n: "new",
s: ""
};
}
},
formatResult: $scope.formatResult,
formatSelection: $scope.formatSelection,
dropdownCssClass: "bigdrop",
escapeMarkup: function (m) {
return m;
}
};
};