我正在使用select2搜索人员,然后从列表中选择您想要的人。一切正常:
Task.ConfigureAssignee = function (objId) {
var sectionID = SubSectionID || 0;
$(objId).select2({
placeholder: 'Type names of people...',
minimumInputLength: 3,
multiple: true,
width: '520px',
id: function (obj) {
return obj.ID;
},
ajax: {
type: "POST",
datatype: 'json',
url: "/webservices/globalwebservice.asmx/TaskAssignSearch",
data: function (term) {
return JSON.stringify({ q: term, sectionId: sectionID, taskType: TaskPageType });
},
contentType: 'application/json; charset=utf-8',
results: function (data, page) {
var ids = $(objId).select2("val");
data.d = $(data.d).filter(function () {
var id = $(this)[0].ID;
return (ids.indexOf(id) == -1);
});
return { results: data.d };
}
},
formatResult: Task.FormatPersonSearchResult,
formatSelection: Task.PersonForSelection,
initSelection: function (element, callback) {
var data = [];
var ids = []
if ($(element).val().length > 0) {
var items = JSON.parse($(element).val())
$(items).each(function () {
var id = this.ID;
var name = this.Name;
data.push({ ID: id, Name: name });
ids.push(id);
});
}
$(objId).select2("val", ids.join(","));
callback(data);
}
})
}
有时按下按钮我想通过javascript将特定的人添加到select2:
$("#txtTaskAssignee").select2('destroy');
$('#txtTaskAssignee').val("[{\"ID\":265,\"Name\":\"Kate Evans\"}]");
Task.ConfigureAssignee('#txtTaskAssignee');
再次,这很好。我不想允许多个选择,但当我切换到单一模式时,所选的值不会被设置。
有什么想法吗?