我正在使用select2 v4<select>
,其中我使用 ajax 方法将 select2 连接到一个元素。一切正常,除了我不能默认加载时选择的值。
示例页面说:
如果您需要提供默认选择,您只需为每个包含应显示的值和文本的选择包含一个选项。
在这段代码中,我想将值默认为“foo”:
标记:
<label class="input">
<select aria-hidden="true" tabindex="-1"
class="form-control user-school-select select2-hidden-accessible"
name="user[school_id]" id="user_school_id">
<option selected="selected" value="1">foo</option>
</select>
<span style="width: 372px;" dir="ltr"
class="select2 select2-container select2-container--default">
<span class="selection">
<span aria-labelledby="select2-user_school_id-container"
tabindex="0" class="select2-selection select2-selection--single"
role="combobox" aria-autocomplete="list" aria-haspopup="true"
aria-expanded="false">
<span title="foo" id="select2-user_school_id-container"
class="select2-selection__rendered"></span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b></span></span></span>
<span class="dropdown-wrapper" aria-hidden="true"></span></span></span>
</label>
这个标记是由我的 rails 视图(users/edit.html.erb)生成的:
<%= label_tag nil, nil, :class => "input" do %>
<% if @user.school_id %>
<%= f.select :school_id,
options_for_select({ @user.user_school => @user.school_id }, @user.school_id ), {},
{ class: "form-control user-school-select" } %>
<% else %>
<%= f.select :school_id, {}, {},
{class: "form-control user-school-select"} %>
<% end %>
<% end %>
users.js.erb 中的 javascript:
$(document).ready(function() {
$(".user-school-select").select2({
ajax: {
url: "/schools",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 2,
templateResult: formatSchool,
templateSelection: formatSchoolSelection
});
});
function formatSchool (school) {
var markup = '<div class="clearfix">' +
'<div class="col-sm-1">' +
(school.logo_url ? '<img src="' + school.logo_url + '" style="max-width: 100%" />' : "") +
'</div>' +
'<div class="col-sm-10">' +
'<div class="clearfix">' +
'<div class="col-sm-9">' + school.name + '</div>' +
'</div>';
markup += '</div></div>';
return markup;
}
function formatSchoolSelection (school) {
return school.name;
}
我可以在 select2 生成的标记中看到 span 有一个title
填充了我的值的属性,但实际的 span 文本为空:
<span class="selection">
<span aria-labelledby="select2-user_school_id-container" tabindex="0"
class="select2-selection select2-selection--single"
role="combobox" aria-autocomplete="list" aria-haspopup="true"
aria-expanded="false">
<span title="foo" id="select2-user_school_id-container"
class="select2-selection__rendered"></span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b></span></span></span>
我还尝试了其他不推荐使用的方法initSelection
,但无济于事:
$(".user-school-select").select2({
ajax: {
url: "/schools",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 2,
templateResult: formatSchool,
templateSelection: formatSchoolSelection,
initSelection : function (element, callback) {
var data = {"id":"1","text":'foo'};
callback(data);
}
});
$(".user-school-select").select2("data", {"id": 1, "text": "foo"});
我也尝试过添加这一行,但也无济于事:
$(".user-school-select").select2("val", "1");
我看过一堆类似问题的答案,但它们不适用于我的场景。我想是因为我使用的是select2 v4。我读过的大多数其他答案都说您要么需要initSelection
结合使用val
- 但那些其他示例总是使用<input>
元素而不是<select>
. 我在最新的文档中确实注意到了initSelection
折旧,而是应该使用DataAdaptercurrent
的方法,但我不知道如何将它应用到上面的代码中?