簡體   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