3
首先,這裏是我的代碼到目前爲止。Laravel 5 with Select2,使用ajax加載數據
我的路線:
Route::get('cities/citybylang/{lang_id}', [
'uses' => '[email protected]',
'as' => 'dashboard.cityByLanguage'
]);
我的控制器:
public function cityByLanguage($lang_id){
$cities = City::select('name','id')->where('lang_id',$lang_id)->get();
return $cities;
}
我的觀點選擇
<select class="js-data-example-ajax">
<option value="3620194" selected="selected">select2/select2</option>
</select>
我的選擇二代碼
$(".js-data-example-ajax").select2({
ajax: {
url: "/dashboard/cities/citybylang/1",
dataType: 'json',
delay: 250,
data: function (params) {
console.log(params.term);
return {
q: params.term, // search term
page: params.page,
name: params.name
};
},
beforeSend: function(jqXHR, settings) {
console.log(settings.url);
},
processResults: function (data, page) {
console.log(data);
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo, // omitted for brevity, see the source of this page
templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
function formatRepo (repo) {
if (repo.loading) return repo.text;
var markup = '<div class="clearfix">' +
'<div clas="col-sm-10">' +
'<div class="clearfix">' +
'<div class="col-sm-6">' + repo.name + '</div>' +
'<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.name + '</div>' +
'<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.name + '</div>' +
'</div>';
if (repo.name) {
markup += '<div>' + repo.name + '</div>';
}
markup += '</div></div>';
return markup;
}
function formatRepoSelection (repo) {
return repo.name || repo.text;
}
Okey,基本問題是,我無法將正確的參數傳遞給控制器。 這是如何工作的,現在我開始輸入到選擇欄,並給它與具有LANG_ID城市名單= 1
所以Ajax調用發送此控制器:
somthing.com/ ?儀表板/城市/ citybylang/1 q =價值,我鍵入選擇欄]
我有漂亮的網址,所以我想是這樣的:
somthing.com/dashboard/cities/citybylang/ [價值,我鍵入到選擇字段]
所以問題是,我怎麼能傳遞參數到控制器中 正確的方式?