2016-01-08 44 views
0

所以我試圖使用數據表,如我在下面寫的代碼中看到的。不幸的是,由於某些未知的原因,用於排序,搜索和分頁的功能不起作用。我可以知道我錯過了什麼或者錯誤原因嗎?搜索,排序和分頁不能用於數據表laravel

@extends('app') 
@section('header') 
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> 
<link href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css" rel="stylesheet"> 
@stop 
@section('content') 

<h1 class="page-header">View Log Information</h1> 
<br/> 

<div class="container"> 
    <div class="col-xs-12 col-md-12" style="overflow:auto"> 
    <table class="table table-hover" id="logTable"> 
     <thead> 
     <tr> 
      <th> 
       User 
      </th> 

      <th> 
       Log Description 
      </th> 
      <th> 
       Time Stamp 
      </th> 
     </tr> 
     </thead> 
     @foreach ($logs as $log) 
     <tbody> 
      <tr> 
       <td> 
        {{ App\User::find($log->user_id)->name }} 
       </td> 

       <td> 
        {{ $log->log_description }} 
       </td> 
       <td> 
        {{ date("F d Y - g:i a",strtotime("$log->created_at")) }} 
       </td> 
      </tr> 
     </tbody> 
     @endforeach 
    </table> 
    </div> 
</div> 

@stop 

@section('scripts') 
    <!-- Bootstrap Based Data Table Plugin Script--> 
    <script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script> 
    <script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script> 
    <script src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js" type="text/javascript"></script> 
    <script> 
    $(document).ready(function() { 
     $('#logTable').DataTable(); 
    }); 
    </script> 
@stop 

回答

2

試試這個:

<table class="table table-hover" id="logTable"> 
    <thead> 
    <tr> 
     <th> 
      User 
     </th> 

     <th> 
      Log Description 
     </th> 
     <th> 
      Time Stamp 
     </th> 
    </tr> 
    </thead> 

    <tbody> 
     @foreach ($logs as $log) 
     <tr> 
      <td> 
       {{ App\User::find($log->user_id)->name }} 
      </td> 

      <td> 
       {{ $log->log_description }} 
      </td> 
      <td> 
       {{ date("F d Y - g:i a",strtotime("$log->created_at")) }} 
      </td> 
     </tr> 
     @endforeach 
    </tbody> 
</table> 

移動@foreach聲明下TBODY。這是行不通的,因爲您在<tbody>內繼續複製tbody而不是<tr>

這只是一個建議。不要看query。這不是一個好習慣。執行控制器內的所有查詢。您可以使用Laravel Relationship來代替App\User::find($log->user_id)->name

+0

非常感謝! =)啊,是的,謝謝提醒有關查詢部分。是的控制器..謝謝=) – EunJi

相關問題