简体   繁体   English

在局部视图中动态加载内容

[英]Dynamically loading content within a partial view

Basically, I am trying to implement tabs on a main page.基本上,我正在尝试在主页上实现选项卡。 The tabs will consist of components on other pages for quick access.这些选项卡将由其他页面上的组件组成,以便快速访问。 I am confused on how I can import a layout while sending data to it in a "lazy" way.我对如何在以“惰性”方式向布局发送数据的同时导入布局感到困惑。

I now use the @section tag to bring in the layout, which is fine.我现在使用@section标签来引入布局,这很好。 But I only want to request the data it needs when the tab is clicked.但是我只想在单击选项卡时请求它需要的数据。

in my main.blade.php:在我的 main.blade.php 中:

<li>
      <a href="#tab_1" data-controller="tab_1" data-target="#tab_1" data-toggle="tab" aria-expanded="true">
      tab 1 </a>
</li>


<div class="tab-pane" id="tab_1" >
     @yield('tab_1')
</div>

in my tab1:在我的 tab1 中:

@extends('main')
@section('tab_1')
@foreach ($activities as $activity)
{{--implementation--}}
@endforeach
@endsection

in my TabController@showtab1:在我的 TabController@showtab1 中:

public function showtab1(Request $request) {
   //Logic here
   return view('tab1', ['activities' => $activities]);
}

The optimal scenario is loading the content for each tab only when the tab is clicked by calling a controller function.最佳方案是仅在通过调用控制器函数单击选项卡时才加载每个选项卡的内容。 I have gotten it to work by using routes to change the whole page, but it kind of ruins the advantage of tabs.我已经通过使用路由来更改整个页面来让它工作,但这有点破坏了标签的优势。

You would typically implement this in such a way that you use the same layout, then pass a different parameter through your URL -> controller -> blade include parameter -> blade view.您通常会以使用相同布局的方式实现这一点,然后通过您的 URL -> 控制器 ->刀片包含参数-> 刀片视图传递不同的参数。

The example below demonstrates how you can do 'lazy'-load (load only 1 tab at the same time) across files in Laravel:下面的示例演示了如何在 Laravel 中跨文件执行“延迟”加载(同时仅加载 1 个选项卡):

// /routes/web.php
Route::get('/tabbed-page', 'TabbedPageController@index');
Route::get('/tabbed-page/:tab', 'TabbedPageController@show');
// /App/Http/Controllers/TabbedPageController.php

public function show($request, $tab) {
  $data = ['title'=> 'No tab'];
  if ($tab === 'tab1') { 
    $data = ['title' => 'I am tab 1'];
  }

  return view('page', [
    'tab' => $tab,
    'data'=> $data
  ]);
}
public function index($request) {
  // open tab1 by default
  redirect('/tabbed-page/tab1');
}
{{-- /resources/views/page.php, requested at for example /tabbed-page/tab1 --}}
<nav>
  <a href="tab1" class="@if($tab === 'tab1') active @endif">Tab 1</a>
  <a href="tab2" class="@if($tab === 'tab2') active @endif">Tab 2</a>
  <a href="tab3" class="@if($tab === 'tab3') active @endif">Tab 3</a>
  <a href="tab4" class="@if($tab === 'tab4') active @endif">Tab 4</a>
</nav>
<main>
  @include($tab, $data)
</main>
{{-- /resources/views/tab1.php, tab2.php, tab3.php, etc.. --}}
<div id="tab1">
  <h1>{{ $data['title] }}</h1>
  Content ....
</div>

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

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