簡體   English   中英

使用Select2的Laravel 5,使用ajax加載數據

[英]Laravel 5 with Select2, Loading data with ajax

首先,到目前為止,這是我的代碼。

我的路線:

Route::get('cities/citybylang/{lang_id}', [
        'uses'  => 'CitiesController@cityByLanguage',
        '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>

我的Select2代碼

$(".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;
            }

哦,基本問題是,我無法將正確的參數傳遞給控制器​​。 這就是現在的工作方式:我開始輸入選擇字段,它給出一個列表,其中包含lang_id = 1的城市。
所以ajax調用將此發送給控制器:
somthing.com/dashboard/cities/citybylang/1?q=[value,我輸入的選擇字段]
我有漂亮的網址,所以我想要這樣的東西:
somthing.com/dashboard/cities/citybylang/[value,我輸入的選擇字段]


所以問題是,如何以正確的方式將參數傳遞給控制器​​?

這個問題很老,但我一直在尋找同樣的東西,這就是我如何管理它。

url: function(params) {
    return '/some/url/' + params.term;
},

這是參考鏈接https://select2.github.io/options.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM