在Laravel DataTable中显示数据 [英] Displaying data in Laravel DataTable

查看:138
本文介绍了在Laravel DataTable中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的普通html表的外观.在表格中,我将国家/地区显示为选项卡,并且在用户单击的每个选项卡下,表格均显示属于特定国家/地区的团队(选项卡).这很好.

This is how my normal html table looked like. In the table, i display the countries as tabs and under every tab a user clicks, the tables displays teams that belong to the particular country(tab). This works fine.

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

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

But now, i am new to datatables and i am trying to convert the html to datatables as below

      <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表中那样工作?

Now my issue is, when i run my project, it only fetches the first team for each country and place them under the first country in the tab showing on the table. How can i make it work like the how it was working in the normal html table above?

非常感谢您的帮助.谢谢

Any help is very much appreciated. Thank you

推荐答案

如果我是对的,您实际上是在尝试将您的teams表放在DataTable上. 那应该对你有用

If I'm right you are actually trying to put your teams table on a DataTable. Then this should work for you

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); 
}

**当我运行我的项目时,它只会获取每个国家/地区的第一支球队,并将它们放置在表格中显示的标签中的第一国家/地区之下 *-这是因为在您的addColumn您循环浏览一个国家/地区的球队,并立即返回球队名称.

*when i run my project, it only fetches the first team for each country and place them under the first country in the tab showing on the table* - this is because on your addColumn you loop through the teams in a country and immediately return the team name.

->addColumn('team_name', function ($country) {
    foreach($country->teams as $team){
        return $team->name; // this team will immediately return as the
                            // value of team_name, that's why you only get
                            //  the first team
    }
}

这篇关于在Laravel DataTable中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆