我正在使用select2 v4我将 select2 连接到 <select>
使用ajax方法的元素。一切正常,除了我不能在加载时默认选定的值。
If you need to provide default selections, you just need to include an option for each selection that contains the value and text that should be displayed.
在此代码中,我想将值默认为“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 View (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 生成的标记中看到该跨度有 title
属性填充了我的值,但实际的跨度文本为空:
<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
已折旧,取而代之的是 current
DataAdapter的方法应该使用,但我不知道如何将其应用到上面的代码中?
最佳答案
这不是正确的答案,但我必须应用一种解决方法 - 通过删除 templateSelection
方法来解决问题。但这不是一个好的解决方案,因为我想要 templateSelection 方法的好处。我暂时不用它就可以了。
$(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*/
});
});
关于javascript - jquery select2 v4 - 选择选择元素上的默认选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32134179/