Select2:为什么预选的 OPTION 标签只显示叉号,没有文字?
Select2: Why does a preselected OPTION tag show only a cross, without the text?
我正在做一个 SELECT 这样的:
<select class="select form-control js-example-basic-multiple" multiple="multiple" id="id_tags" name="tags">
{% for tag in photo.tags.all %}
<option selected value="http://localhost:8001/api/tags/{{ tag.id }}/">{{ tag.name }}</option>
{% endfor %}
</select>
然后我启动我的 select2 实例:
$(".js-example-basic-multiple").select2({
multiple : true,
ajax : { ..... }
});
我看到了这个:
AJAX 确实有效,可以添加新项目:
Select2 实例也有正确的数据,即使对于只有十字的项目也是如此:
IN >>> $(".js-example-basic-multiple").val()
OUT >>> ["http://localhost:8001/api/tags/4142/", "http://localhost:8001/api/tags/4145/", "http://localhost:8001/api/tags/4160/", "http://localhost:8001/api/tags/4213/", "http://localhost:8001/api/tags/4344/", "http://localhost:8001/api/tags/6602/"]
如果它对其他人有帮助,这就是我必须做的来解决这个问题:
function make_select2() {
$(".js-example-basic-multiple").select2({
multiple : true,
id : function(repo) {
//console.log("repo");
//console.log(repo);
return repo.url;
},
ajax : {
url : "/api/tags/",
dataType : 'json',
delay : 100,
placeholder : "Tag your photos",
data : function(params) {
//console.log("params");
//console.log(params);
return {
q : params.term, // search term
page : params.page
};
},
processResults : function(data, params) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
//console.log("data");
//console.log(data);
//console.log("params");
//console.log(params);
var select2Data = $.map(data.results, function(obj) {
obj.id = obj.url;
obj.text = obj.name;
return obj;
});
return {
results : select2Data,
pagination : {
more : data.next
}
};
},
cache : true,
},
escapeMarkup : function(markup) {
return markup;
},
minimumInputLength : 1,
templateResult : formatRepo, // omitted for brevity, see the source of this page
// templateSelection : formatRepoSelection // omitted for brevity, see the source of this page
});
}
这是我的代码 - 我所要做的就是注释掉 templateSelection
- 事实证明这是没有必要的。
我正在做一个 SELECT 这样的:
<select class="select form-control js-example-basic-multiple" multiple="multiple" id="id_tags" name="tags">
{% for tag in photo.tags.all %}
<option selected value="http://localhost:8001/api/tags/{{ tag.id }}/">{{ tag.name }}</option>
{% endfor %}
</select>
然后我启动我的 select2 实例:
$(".js-example-basic-multiple").select2({
multiple : true,
ajax : { ..... }
});
我看到了这个:
AJAX 确实有效,可以添加新项目:
Select2 实例也有正确的数据,即使对于只有十字的项目也是如此:
IN >>> $(".js-example-basic-multiple").val()
OUT >>> ["http://localhost:8001/api/tags/4142/", "http://localhost:8001/api/tags/4145/", "http://localhost:8001/api/tags/4160/", "http://localhost:8001/api/tags/4213/", "http://localhost:8001/api/tags/4344/", "http://localhost:8001/api/tags/6602/"]
如果它对其他人有帮助,这就是我必须做的来解决这个问题:
function make_select2() {
$(".js-example-basic-multiple").select2({
multiple : true,
id : function(repo) {
//console.log("repo");
//console.log(repo);
return repo.url;
},
ajax : {
url : "/api/tags/",
dataType : 'json',
delay : 100,
placeholder : "Tag your photos",
data : function(params) {
//console.log("params");
//console.log(params);
return {
q : params.term, // search term
page : params.page
};
},
processResults : function(data, params) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
//console.log("data");
//console.log(data);
//console.log("params");
//console.log(params);
var select2Data = $.map(data.results, function(obj) {
obj.id = obj.url;
obj.text = obj.name;
return obj;
});
return {
results : select2Data,
pagination : {
more : data.next
}
};
},
cache : true,
},
escapeMarkup : function(markup) {
return markup;
},
minimumInputLength : 1,
templateResult : formatRepo, // omitted for brevity, see the source of this page
// templateSelection : formatRepoSelection // omitted for brevity, see the source of this page
});
}
这是我的代码 - 我所要做的就是注释掉 templateSelection
- 事实证明这是没有必要的。