问题描述
我在Laravel项目中有一个Select2js多个标签输入字段,该字段使用api在标签数据库中搜索术语并向用户提供建议。用户可以选择数据库中存在的标签之一,也可以输入一个新标签。
为此刀片:
<select class="form-control selectmultiple" id="tags" name="tags[]" multiple="multiple">
@if (old('tags') !== null)
@foreach (old('tags') as $tags)
<option value="{{ $tag }}">{{ $tag }}</option>
@endforeach
@endif
</select>
javascript
$('#tags').select2(
{
tags: true,placeholder: 'placeholder text',minimumInputLength: 1,ajax: {
url: 'url',//obfuscated for the purpose of sharing on StackOverflow
dataType: 'json',delay: 700,processResults: function (data) {
return {
results: [{
text: "Database Suggestions:",children: $.map(data,function (item) {
return {
text: item.name,id: item.id,fromDatabase: true
}
})
}]
};
},cache: true,},templateResult: formatState,// a function which formats the results from the database
cloSEOnSelect: false
}
)
当Laravel在整个表单上执行验证时,如果其他某个字段验证失败,我希望select2js字段重新填充旧字段。但是Laravel仅返回标签的ID,因为这是传递给表单提交的内容。
我尝试使用上述刀片式语法重新填充字段,以读取old('tags')
,但它仅在select2下拉列表中重新填充ID号。
如何用实际标签重新填充Select2js字段?我是否需要做一个单独的api和api调用来检索标记名称,或者还有其他方法可以使用laravel或可以使用的select2js的某些功能吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)