繁体   English   中英

Laravel @include通过ajax的刀片视图

[英]Laravel @include a blade view via ajax

我有一个@include一些内容的页面,我想@include使用ajax请求的刀片视图文件。 我该怎么办

基本上,视图文件将从服务器获取项目,

**price.blade.php**
@foreach ($items as $item)
<div class="item-post">
  <div class="priceofitem">{{ $item->price }} </div>

我想在我的标签部分中包含@include('price.blade.php')

<ul class="tabs">
<li><a href="#tab1">Prices </li>
<div id="tab1">@include('price.blade.php')</div>

我不希望在加载时自动包含该视图文件,因为我不想加载该选项卡的内容,除非用户点击它,如果用户想要价格而不是用户点击该选项卡,以及AJAX请求将被发送以包含该文件。

希望我明确表示,如果你不理解我,请告诉我。

手指交叉

你想要的东西

$(document).ready(function() {
    $("#tab1").click(function() {
        $.ajax({
            type: 'POST', 
            url : "/yourrouteview", 
            success : function (data) {
                $("#tab1").html(data);
            }
        });
    });
}); 

您的控制器和路由必须配置/ yourrouteview以获取正确的视图(即@include('price.blade.php')

发出ajax请求并从控制器函数返回视图,如:

return view('your_view');

在ajax success函数中,将其附加到您想要的位置:

success: function(response){
    $('#Id').html(response);
}

流程如下:

$('#tab').click(function(){
    // ajax call here
    ...
    success: function(response){
        $('#Id').html(response);
    }
});

控制器:

function funcName()
{
    // Do what ever you want
    return view('your_view');
}

使用刀片引擎与id进行ajax调用,并在这样的laravel中获取请求!! 尝试这个。

$(document).ready(function(){
  var id = $(this).data("id");
  $.ajax({
     type: "GET",
     url:"{{ url('your url') }}/"+id,
     cache: false,
     contentType: false,
     processData: false,
     success: function (data) {
       $("#id").html(data);
     },
     error: function (xhr, textStatus, errorThrown) {
       console.log("XHR",xhr);
       console.log("status",textStatus);
       console.log("Error in",errorThrown);
     }
  });

通过ajax请求,您可以从服务器获取编译视图,并将该值输出到某个父根,例如在promise。然后调用。

在服务器端,您可以在路由中使用HTTP请求的简单处理程序:

Route::get('/', function () {
    return view('price'); // here you can pass params for compiling blade template
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM