[英]Simple example to set initial value of select2 element with Ajax source?
I am trying to set the initial value of a Select2 element that is hooked up to an Ajax data source. 我试图设置连接到Ajax数据源的Select2元素的初始值。
I am using v4 of Select2, and following the docs , which explain that initSelection
has been replaced. 我使用的是Select2 v4,并遵循docs ,该文档解释了
initSelection
已被替换。
I have begun by trying to adapt the example in the "Loading remote data" section of the Select2 documentation . 我首先尝试改编Select2文档的“加载远程数据”部分中的示例 。 This is my code to set the initial value:
这是我设置初始值的代码:
var $gitElement = $('.select2');
var option = new Option("yosssi/ejr", '29424443', true, true);
$gitElement.append(option);
$gitElement.trigger('change');
I end up with a tag showing undefined (undefined)
. 我最后得到一个显示
undefined (undefined)
的标签。 What am I doing wrong? 我究竟做错了什么?
JSFiddle here: http://jsfiddle.net/t4Ltcm0f/4/ JSFiddle在这里: http : //jsfiddle.net/t4Ltcm0f/4/
The error is is your templateResult
and templateSelection
functions. 错误是您的
templateResult
和templateSelection
函数。 The 'repo' argument is an object not containing 'name' and 'full_name' properties, you should use the 'text' property instead. 'repo'参数是一个不包含'name'和'full_name'属性的对象,您应该使用'text'属性。
function(repo) {
return repo.text || repo.name + ' (' + repo.full_name + ')';
}
Working JSFiddle here: http://jsfiddle.net/45891w6v/1/ 在这里可以使用JSFiddle: http : //jsfiddle.net/45891w6v/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.