【问题标题】:how to create Loading remote data search in Laravel 5.2?如何在 Laravel 5.2 中创建加载远程数据搜索?
【发布时间】:2016-10-25 00:02:50
【问题描述】:

这个网站是Book Hotel,用户搜索后可以看到城市和酒店。

我想在 laravel 5.2 中使用 Loading remote data select2。

Github 示例:https://select2.github.io/examples.html#data-ajax

型号City

protected $fillable = [
    'name', 'state', 'country', 'status'
];

型号Hotel

protected $fillable =[
    'name', 'address', 'phone', 'status', ...
];

HTML:

<select class="js-data-example-ajax">
   <option value="3620194" selected="selected">select2/select2</option>
</select>

脚本:

$(".js-data-example-ajax").select2({
ajax: {
url: "https://api.github.com/search/repositories",
dataType: 'json',
delay: 250,
data: function (params) {
  return {
    q: params.term, // search term
    page: params.page
  };
},
processResults: function (data, params) {
  // 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, except to indicate that infinite
  // scrolling can be used
  params.page = params.page || 1;

  return {
    results: data.items,
    pagination: {
      more: (params.page * 30) < data.total_count
    }
  };
},
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
});

ajax发送数据后如何写入Laravel controller

【问题讨论】:

    标签: php laravel laravel-5.2 jquery-select2


    【解决方案1】:

    HTML:

    <select id="hotel" name="hotel">
         <option value="hotel1">Hotel1</option>
         <option value="hotel2">Hotel2</option>
    </select>
    

    Jquery 和 Ajax:

    $(document).on('click','#hotel',function(e)
    {
        e.preventDefault();
        var name=$(this).val('hotel');
    
        $.ajax({
            type:"POST",
            url: "{{url('/controller/search')}}",
            data: {
            "_token": "{{ csrf_token() }}"
            },
            success: function (data) {
                var res = $.parseJSON(data);
                if(res == true)
                {   
                      alert('ok');
                }
            }
        });
    });
    

    Laravel 控制器:

    public function search(Request $request)
    {
       $hotel=Hotel::where(['name',$request->name])->first();
       return Response::json(array('status'=>TRUE,'hotel'=>$hotel)); 
    }
    

    【讨论】:

    • 非常感谢,但我想在一起搜索城市和酒店后看看
    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2017-05-27
    • 2017-03-04
    • 2016-09-15
    相关资源
    最近更新 更多