如何在链接点击上显示/隐藏字段?

时间:2017-02-16 10:19:03

标签: php laravel navigation blade

我正在使用laravel 5.3 我有一个响应的导航菜单,当我点击域名时,它会显示我在DB中已有的所有域名,然后当我点击其中一个域名时,我会得到相应的项目。

现在,我希望当我点击项目时,它会在表格中显示所有相应的数据。但是,这就是我得到的:

这是ProjectController:

public function index(Request $request)
{
  $projects1=DB::table('projects')->where('domain_id', '=', 1)->get();
  $projects2=DB::table('projects')->where('domain_id', '=', 2)->get();
  $projects3=DB::table('projects')->where('domain_id', '=', 3)->get();

  return view('projects.index',compact('projects', 'projects1', 'projects2', 'projects3'));
}

这是index.blade.php:

<div class="nav-side-menu">
  <div class="brand">Menu</div>
  <i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
  <div class="menu-list">
    <ul id="menu-content" class="menu-content collapse out">           
      <li  data-toggle="collapse" data-target="#products" class="collapsed active">
        <a href="#"><i class="fa fa-gift fa-lg"></i> Domains <span class="arrow"></span>  </a>
      </li>
      <ul class="sub-menu collapse" id="products">
        <li  data-toggle="collapse" data-target="#domain1_projet" class="collapsed active">
          <a href="#"><i class="fa fa-gift fa-lg"></i> Domain 1 <span class="arrow"></span></a>
        </li>
        <ul class="sub-menu collapse" id="domain1_projet">
           @foreach ($projects1 as $key => $project)
             <li><a href="#">{{$project->title}}</a></li>                    
           @endforeach
           <table class="table table-bordered">
            <tr>
             <th>No</th>
             <th>title</th>
             <th>code</th>
             <th>domain_id</th>  
            </tr>
            @foreach ($projects1 as $key => $project1)
             <tr>
              <td>{{ ++$i }}</td>
              <td>{{ $project1->title }}</td>
              <td>{{ $project1->code }}</td> 
              <td>{{ $project1->domain_id}}</td>
             </tr>
           @endforeach
          </table>
         </ul>                             
         <li  data-toggle="collapse" data-target="#domain2_projet" class="collapsed active">
          <a href="#"><i class="fa fa-gift fa-lg"></i> Domain 2 <span class="arrow"></span></a>
         </li>
         <ul class="sub-menu collapse" id="domaine2_projet">
           @foreach ($projects2 as $key => $project)
            <li><a href="#">{{$project->title}}</a></li>                    
           @endforeach                    
         </ul>                  
       </ul>
     </ul>
   </div>
</div>

我在表之前添加的@if子句只是为了测试,但我需要改变它。我希望能够以手动方式显示每个项目的数据。我还想知道在点击链接时如何显示和隐藏每个项目的信息。 正如您在index.blade.php中所看到的那样,因为我已经知道我所做的项目的名称if (($project->intitule)=="Title")elseif(($project->intitule)=="Project2")但实际上我在数据库中有很多项目。我怎样才能显示所有项目,然后当我点击其中一个项目时,如何获得包含该特定项目信息的表格

1 个答案:

答案 0 :(得分:1)

<强> CONTROLLER

public function index(Request $request)
{
  $projects=DB::table('projects')->get();
  return view('projects.index',compact('projects'));
}

查看

<div class="nav-side-menu">
  <div class="brand">Menu</div>
  <i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
  <div class="menu-list">
    <ul id="menu-content" class="menu-content collapse out">           
      <li  data-toggle="collapse" data-target="#products" class="collapsed active">
        <a href="#"><i class="fa fa-gift fa-lg"></i> Domains <span class="arrow"></span>  </a>
      </li>


      <ul class="sub-menu collapse" id="products">
        @for ($i = 0; $i < count($projects); $i++)
        <li  data-toggle="collapse" data-target="#domain1_projet{{$i}}" class="collapsed active">
          <a href="#"><i class="fa fa-gift fa-lg"></i> Domain {{$projects[$i]->domain_id}} <span class="arrow"></span></a>
        </li>


        <ul class="sub-menu collapse" id="domain1_projet{{$i}}">

             <li><a href="#">{{$projects[$i]->title}}</a></li>                    

           <table class="table table-bordered">
            <tr>
             <th>No</th>
             <th>title</th>
             <th>code</th>
             <th>domain_id</th>  
            </tr>

             <tr>
              <td>{{ $i }}</td>
              <td>{{ $projects[$i]->title }}</td>
              <td>{{ $projects[$i]->code }}</td> 
              <td>{{ $projects[$i]->domain_id}}</td>
             </tr>

          </table>
        </ul>                             
        @endfor



       </ul>


     </ul>
   </div>
</div>

试试吧!如果返回错误,请告诉我!