在Laravel DataTable中显示数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Laravel DataTable中显示数据相关的知识,希望对你有一定的参考价值。

这是我的普通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>

但现在,我是数据表的新手,我正在尝试将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表中的工作方式一样工作?

很感谢任何形式的帮助。谢谢

答案

如果我是对的,你实际上是想把你的团队表放在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); 
}

*当我运行我的项目时,它只获取每个国家的第一个团队并将它们放在表格中显示的选项卡中的第一个国家* - 这是因为在你的addColumn上你循环通过一个国家的团队并立即返回队名。

->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中显示数据的主要内容,如果未能解决你的问题,请参考以下文章

Datatable中基于ID的信息颜色状态高亮显示(Laravel 8)

如何在yajra datatable列中显示图像,laravel5.3

如何通过 Laravel DataTable 显示 HTML?

如何在 Laravel 的 DataTable 列表页面中更新状态

编辑列日期格式后不显示过滤日期 - Laravel Yajra Datatable

在 Laravel 8 中使用 yajra 数据表加载 DataTable where select