【问题标题】:Laravel datatable row detailsLaravel 数据表行详细信息
【发布时间】:2017-04-15 15:11:55
【问题描述】:

我在 Laravel 5.4 中使用单个数据表,因为它已经在 Laravel 框架中实现。我的数据表正在使用查询生成器来获取数据,例如使用这样的东西:

@section('content')
<div class="container">
  <table id="services" class="table table-hover table-condensed" style="width:100%">
    <thead>
        <tr>
            <th> </th>
            <th>Id</th>
            <th>Plate</th>
            <th>Brand</th>
            <th>Year</th>

        </tr>
    </thead>
  </table>
</div>

<script type="text/javascript">
$(document).ready(function() {
    table = $('#services').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('datatable_ser.getdata') }}",
        "columns": [
            {data: 'id', name: 'id'},
            {data: 'plate', name: 'plate'},
            {data: 'brand', name: 'vehicle.brand'},
            {data: 'year', name: 'vehicle.year'},


        ]
    });    
});  
</script>
@stop

为了让它正常工作,我必须包含这个文件:

<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

由于 Laravel 中包含数据表,它从 bootstrap 中获取 css,从 laravel 中安装的数据表包中获取 javascript。

现在,阅读row detail documentation 有点不清楚,在我的情况下如何实现行详细信息。我必须在代码中添加什么?我必须包含更多文件吗?

与每行的EditRemove 按钮的其他功能相同。 Laravel 本身没有数据表的文档,所以现在做什么有点令人困惑。感谢您的帮助。

【问题讨论】:

  • Row Detail 在您即将显示自定义行详细信息时使用。但是还有另一个可用的 api DataTable Responsive 做同样的事情,显示行详细信息 w.r.t 屏幕尺寸。
  • 我很确定有很多功能可以用来做行细节,但我想用 API 做一个但没有运气。

标签: laravel datatables


【解决方案1】:

所以在我看来,我放了一个ajax函数来调用数据。

 $('#pesquisarFinal').submit(function(e){
      e.preventDefault();
      url = '{{ route("disciplina.criar.ano.novo",["curso" => ":id"]) }}';
      url = url.replace(":id", $("#curso").val());

      $.ajax({
        method: "POST",
        url: url,
        data: { 
           '_token': '{{ csrf_token() }}', 
           curso: $('#curso').val(),
           nivel: $('#nivel').val(),
           semestre: $('#semestre').val()
        }
      }).done(function(msg){
         oTable.clear().draw();
         oTable.rows.add(msg.data).draw();          
      });
})

在我的控制器中,我确实喜欢这样调用要插入到表中的行

public function criarNovoAno(Request $request, Curso $curso){
        
        $curso = Curso::find($request->curso);
        $dados = Array();
        
        foreach($curso->disciplina as $item){
            $check = '<input type="checkbox" value="'.$item->pivot->id.'" class="flat atribuir"/>';
            if($request->semestre == $item->pivot->semestre->idsemestre && $request->semestre == $item->pivot->nivel->idnivel){
                array_push($dados,[$check,$item->codigo, $item->nome, $item->pivot->semestre->descricao, $item->pivot->nivel->descrico, $curso->nome]);
            }
        }      

        return response()->json(["data" => $dados]);
    }

在控制器中,我创建一个数组,当循环开始时,我为第一个数组中的每个位置创建一个新数组。

Final result

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多