I have the following directive:
我有以下指令:
angular.module('click-to-edit-select2', [])
.directive("clickToEditSelect2", function() {
var editorTemplate = '<td id="4" class="click-to-edit-select2">' +
'<div id="3" style="height:20px" ng-click="enableEditor()" ng-hide="view.editorEnabled">' +
'{{value}} ' +
'</div>' +
'<div id="2" ng-show="view.editorEnabled" style="padding:0px 10px 0px 0px";>' +
'<input id="1" type="hidden" ui-select2="select2Options" ng-model="view.editableValue" ng-change="changeText()" />' +
'</div>' +
'</td>';
return {
restrict: "A",
replace: true,
template: editorTemplate,
scope: {
value: "=clickToEditSelect2"
},
controller: function($scope, $element, $attrs) {
$scope.view = {
//editableValue: $scope.term.TermId.id,
editorEnabled: false
};
$scope.enableEditor = function() {
if ($scope.$parent.term.Status == "new") {
$scope.view.editorEnabled = true;
$scope.view.editableValue = $scope.value;
}
};
$scope.disableEditor = function() {
$scope.view.editorEnabled = false;
};
$scope.save = function() {
//alert($scope.term.TermId.id);
$scope.value = $scope.view.editableValue.id;
$scope.disableEditor();
};
$scope.$watch('view.editableValue', function(newVal, oldVal) {
if (newVal != undefined && newVal != "") {
if (oldVal == newVal) return;
$element.addClass('valueChangedTD');
var str = newVal.text;
var res = str.split(" - ");
//slice the termID
res.splice(res.length - 1, 1);
var termDescription = res.join(' - ');
}
}, true);
//select2 has its own blur event !
$element.on('select2-blur', function(event) {
$scope.save();
});
var initSelectionCb = function(item, callback) {
if (item != "") {
var id = item.val();
var data = { id: id, text: id };
callback(data);
}
};
$scope.select2Options = {
placeholder: "Select Terminal",
dropdownAutoWidth: 'true',
multiple: false,
width: "resolve",
selectOnBlur: true, //this does not seem to work
initSelection: function(item, callback) {
//selects the initial item
initSelectionCb.call(self, item, callback);
},
ajax: {
url: "/GetDataSelect2",
type: "POST",
contentType: "application/json",
data: function(term, page) {
var Json = {};
Json.term = term;
Json.page = page;
Json.limit = 10;
return Json;
},
results: function(data, page) {
return { results: data.options };
}
}
}
}
};
ang