【问题标题】:Laravel 5 with Select2, Loading data with ajax带有 Select2 的 Laravel 5,使用 ajax 加载数据
【发布时间】:2015-10-20 13:34:08
【问题描述】:

首先,这是我目前的代码。

我的路线:

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=[值,我在选择字段中输入的内容]
我有漂亮的网址,所以我想要这样的内容:
somthing.com/dashboard/cities/citybylang/[值,我在选择字段中键入的内容]


所以问题是,我怎样才能以正确的方式将参数传递给控制器​​?

【问题讨论】:

    标签: php ajax url laravel-5 jquery-select2


    【解决方案1】:

    这个问题很老,但我一直在寻找与我管理它的方法相同的东西。

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

    这里是参考链接 https://select2.github.io/options.html

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 2013-08-31
      • 1970-01-01
      • 2021-12-22
      • 2013-09-13
      • 2017-06-25
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多