在Laravel DataTable中显示数据

时间:2017-12-17 07:54:12

标签: php laravel datatables

这是我的普通html表格的样子。在表格中,我将国家/地区显示为选项卡,在用户单击的每个选项卡下,表格显示属于特定国家/地区的选项卡(选项卡)。这很好用。

      <div  class="nav-tabs-custom" id="tabs">

        <ul id="myUL" class="nav nav-tabs">
        @foreach($countries as $country)

           <li ><a href="#tab_{{ $country->id }}" data-toggle="tab" >{!!$country->name!!}</a></li>
           @endforeach


        </ul>
        <div class="tab-content">
        @foreach($countries as $key => $country)

          <div class="tab-pane" id="tab_{{ $country->id }}">
          <table class="table" id="tables">
                            <thead>
                                <tr> 
                                    <th></th>
                                    <th colspan="5"></th>
                                    <th></th>
                                </tr>
                           </thead>
                           <tbody id="list">
           @foreach($country->teams as $team) 
                           <td><input onclick="return show(this)" data-team="{{$team->toJson()}}" type="checkbox" id="{!! $team->id !!}" name="{!! $team->name !!}" value="{!! $team->value !!}" /> </td>
                                    <td width="600">{{$team->name }}</td>
                                     <td>{{ $team->value}}</td>
                                </tr>
          @endforeach        
       </tbody>
       </table>
         </div>
        @endforeach     
          </div>    

        </div>

但是现在,我是datatables的新手,我正在尝试将html转换为数据表,如下所示

      <div  class="nav-tabs-custom" id="tabs">

        <ul id="myUL" class="nav nav-tabs">
        @foreach($countries as $country)

           <li ><a href="#tab_{{ $country->id }}" data-toggle="tab" >{!!$country->name!!}</a></li>
           @endforeach


        </ul>
        <div class="tab-content">
        @foreach($countries as $key => $country)

          <div class="tab-pane" id="tab_{{ $country->id }}">
          <table class="table" id="tables">
                            <thead>
                                <tr> 
                                    <th></th>
                                    <th colspan="5"></th>
                                    <th></th>
                                </tr>
                           </thead>

       </table>
         </div>
        @endforeach     
          </div>    

        </div>


<script type="text/javascript">
$(document).ready(function() {

        oTable = $('#tables').DataTable({
            "processing": true,
            "serverSide": true,
            "bProcessing":false,
            "bSort":false,
            "ajax": "{{ route('datatable.findteam') }}",
            "columns": [
                {data: 'check', name: 'check', orderable: false, searchable: false},    
                {data: 'team_name', name: 'team_name'},  
                {data: 'team_value', name: 'team_value'},
          ],        
        });

});
</script>

控制器

 public function create()
     {
         $countries = Country::where('id', Auth::user()->id)->get();
         return view('create',compact('countries'));
     }

     public function getTeams()
     {
        $countries = Country::where('id', Auth::user()->id)->get();    

        return Datatables::of($countries)->addColumn('check', function ($country) {
            foreach($country->teams as $team){

            return '
            <input onclick="return show(this)" data-team="{{$team->toJson()}}" type="checkbox" id="{!! $team->id !!}" name="{!! $team->name !!}" value="{!! $team->value !!}" />
            ';
            } 
            })->addColumn('team_name', function ($country) {
                foreach($country->teams as $team){
                 return $team->name;
                }
            })->addColumn('team_value', function ($country) {
                foreach($country->teams as $team){                    
                return  $team->value;

                }
            })

->make(true); 
     }

现在我的问题是,当我运行我的项目时,它只获取每个国家的第一个团队,并将它们放在桌面上显示的选项卡中的第一个国家/地区。我怎样才能使它像在上面的普通html表中工作一样工作?

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

如果我是对的,那么你实际上是想把你的团队表放在DataTable上。 那么这应该对你有用

public function getTeams($country_id) {
    $teams = Team::where('country_id', $country_id);

    return Datatables::of($teams)
        ->addColumn('check', function (Team $team) {
            return '<input onclick="return show(this)" data-team="'.$team->toJson().'" type="checkbox" id="'.$team->id.'" name="'.$team->name.'" value="'.$team->value.'" />
                ';
        })->addColumn('team_name', function (Team $team) {
            return $team->name;
        })->addColumn('team_value', function (Team $team) {                  
            return  $team->value;
        })
        ->rawColumns([x])// x the index for your check column,
        ->make(true); 
}

*当我运行我的项目时,它只获取每个国家/地区的第一个团队,并将它们放在表格中显示的选项卡中的第一个国家/地区 * - 这是因为在您的{{1你遍历一个国家的团队并立即返回团队名称。

addColumn