简体   繁体   English

Laravel @include通过ajax的刀片视图

[英]Laravel @include a blade view via ajax

I have a page which will @include some content, I want to @include that blade view file using ajax request. 我有一个@include一些内容的页面,我想@include使用ajax请求的刀片视图文件。 How should I do it. 我该怎么办

Basically, the view file will get the items from server, 基本上,视图文件将从服务器获取项目,

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

I want to include @include('price.blade.php') in my tab section 我想在我的标签部分中包含@include('price.blade.php')

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

I don't want to include that view file automatically upon load, as I don't want to load that tab's content unless the user clicks on it, if the user wants the prices than the user clicks on that tab, and an AJAX request will be sent to include that file. 我不希望在加载时自动包含该视图文件,因为我不想加载该选项卡的内容,除非用户点击它,如果用户想要价格而不是用户点击该选项卡,以及AJAX请求将被发送以包含该文件。

Hope I made myself clear, please let me know if you did not understand me. 希望我明确表示,如果你不理解我,请告诉我。

Fingers crossed 手指交叉

You want something like 你想要的东西

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

Your controller and route must configure /yourrouteview to get the correct view (that is @include('price.blade.php') 您的控制器和路由必须配置/ yourrouteview以获取正确的视图(即@include('price.blade.php')

Make your ajax request and return the view from controller function like: 发出ajax请求并从控制器函数返回视图,如:

return view('your_view');

in ajax success function, append it to where you want like: 在ajax success函数中,将其附加到您想要的位置:

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

The flow is something like: 流程如下:

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

controller: 控制器:

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

Make ajax call with id with using blade engine with get request in laravel like this!! 使用刀片引擎与id进行ajax调用,并在这样的laravel中获取请求!! Try this. 尝试这个。

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

Via ajax request you can get compiled view from server and yield that value into some parent root, eg in promise .then call. 通过ajax请求,您可以从服务器获取编译视图,并将该值输出到某个父根,例如在promise。然后调用。

On the server side you can use simple handler of HTTP request in your routes: 在服务器端,您可以在路由中使用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