【发布时间】:2016-11-18 15:33:27
【问题描述】:
Controller@Update
//Update
$course = Course::findOrFail($id);
$course->name = Input::get('name');
$course->code = Input::get('code');
$course->credits = Input::get('credits');
$course->description = Input::get('description');
$course->tags()->sync(Input::get('tags')); // Use of sync method
//return [$course];
$course->save();
//Redirect
Session::flash('message', 'Successfully edited the course : '.$course->name);
return $this->show($course->code);
HTML
<pre>
<div class="form-group">
{{Form::label('tags', 'Tag')}}
{{Form::select('tags[]',[],null,array('multiple'=>'multiple','name'=>'tags[]','id'=>'tag_list','class'=>'form-control'))}}
</div>
</pre>
Select2 脚本
function tagResultTemplater(tag) {
return tag.name + " : " + tag.type;
}
function tagSelectionTemplater(tag) {
return tag.id + " "+tag.name + " : " + tag.type;
}
$("#tag_list").select2({
ajax: {
url: "{!! route('tags.json') !!}",
dataType: 'json',
delay: 250,
tags: true,
data: function (params) {
return {
q: params.term, // search term
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
minimumInputLength: 1,
placeholder: function(){
$(this).data('placeholder');
},
templateResult: tagResultTemplater,
templateSelection: tagSelectionTemplater
});
我正在将课程管理系统作为一个研究生项目,简而言之,课程和标签之间存在多对多的关系,我想使用带有 Select2 包的同步方法来附加和分离课程中的标签一种方便的方法,但我找不到在使用 ajax 数据时设置 Select2 的选定值的方法。如果有人可以提供有关如何执行此操作的简单指南,将不胜感激:D。
【问题讨论】:
标签: jquery ajax laravel tags select2