我想用select2(4.0.0)和jquery(2.1.4)实现以下场景:
实现这一目标
我的代码如下所示:
$("select[name='serie']").select2({
ajax: {
url: "http://javaendpoit/serie/filter",
dataType: 'json',
delay: 250,
data: function (params) {
return {
name: params.term,
page: params.page
};
},
processResults: function (data, page) {
return {
results: $.map(data, function (item) {
return {
text: item.name,
name: item.name,
id: item.name
}
})
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 2,
templateResult: formatRepo,
templateSelection: formatRepoSelection,
tags: true,
createTag: function (tag) {
return {
id: tag.term,
text: tag.term,
name: tag.term,
isNew : true
};
},
/* below part is rendered by jsp so that it has the value from previous form submission; if it is initial form render the below part is not included */
initSelection : function (element, callback) {
callback({id:"Name",name:"Name","text":"Name"});
}
});
function formatRepo (serie) {
if (serie.loading) return serie.text;
var markup = '<div class="clearfix">' +
'<div clas="col-sm-10">' +
'<div class="clearfix">' +
'<div class="col-xs-12">' + serie.name + '</div>' +
'</div>';
markup += '</div></div>';
return markup;
}
function formatRepoSelection (serie) {
return serie.name;
}
这很好用。这意味着在提交后我可以在选择框中看到值Name,它是所选的值。然而,如果我再次尝试提交此表单,则请求中缺少该值。
正如在这里的主题数量中所建议的那样,我添加了以下行(参见最后一行)。
/* [...] */
initSelection : function (element, callback) {
callback({id:"Name",name:"Name","text":"Name"});
}
}).select2("val","Name");
但是在添加之后, Name 的值在select2框中甚至不可见。
我一直试图将它修复几个小时,但不知何故找不到合适的解决方案。我打赌我错过了一些基本的东西。请帮助!
--- [编辑]
添加数据后,即使使用 val 设置值,我也可以看到值Name如果我重新提交表单,此值仍然不在请求中
.select2('data',{id:"Name",name:"Name","text":"Name"})
.select2('val', "Name")
--- [编辑2]
在html中,我使用选择来创建此元素。
<select class="form-control" name="serie"></select>
答案 0 :(得分:2)
我设法用select2版本4.0.2这样做:
$("select[name='serie']").data('select2')
.dataAdapter.select({
id:"Name",
name:"Name",
"text":"Name"
});
答案 1 :(得分:1)
Thanks to Kevin I was able to solve the puzzle. So ultimately I have given up on using initSelection. Instead I just filled select tag with options that should be selected.
<select class="form-control" id="author" name="author" multiple="multiple">
<option value="name" selected="selected">name</option>
</select>
Removed initSelection from select2 definition
$("select[name='author']").select2({
ajax: {
url: "http://javaendpoit/serie/filter",
dataType: 'json',
delay: 250,
data: function (params) {
return {
name: params.term,
page: params.page
};
},
processResults: function (data, page) {
return {
results: $.map(data, function (item) {
return {
text: item.name,
name: item.name,
id: item.name
}
})
};
},
cache: true
},
escapeMarkup: function (markup) {
return markup;
},
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection,
tags: true,
createTag: function (tag) {
return {
id: tag.term,
text: tag.term,
name: tag.term,
isNew: true
};
}
});
And changed formatRepoSelection to work with values without name attribute.
function formatRepoSelection(serie) {
if (serie.name === undefined) {
return serie.text;
} else {
return serie.name;
}
}
I guess I could not use name attribute in the formatRepo and could remove the if clause for formatRepoSelection. But this is just a minor fix.