如何使用数据表“ yajra”

时间:2019-04-07 01:12:30

标签: laravel datatables

我遵循了本教程:How to route in Laravel DataTables

它是如此简单,但我做不到。在我设置好控制器,路线和视图后,它不显示数据和表。在表中,我有一个类似“操作”的按钮,我如何才能将此命令带到该按钮上?

您可以检查我的故障查询

 Route::get('user/show1', 'userController@show')->name('usershow1');
 Route::get('user/show1-dt', 'userController@indexDataTables')->name('usershow1dt');

控制器

 public function show()
{

    $pemeliharaan = Pemeliharaan::with(['user','alat'])->where('status','harian')->get();

    return view('users.view_harian',['pemeliharaan' => $pemeliharaan]);

}
public function indexDataTables()
{
    $pemeliharaan = Pemeliharaan::query();

// return DataTables::eloquent($pemeliharaan)->toJson();
    return Datatables::of($pemeliharaan)->make(true);  
}

我有这样的看法。页数,搜索和分页显示在视图中,但此数据未显示。您可以更正此视图吗?

 <div class="box-body table-responsive no-padding">
            <table class="table table-hover" id="table">
              <tbody><tr>
                <th>No</th>
                <th>Nama Alat</th>
                <th>status</th>
                <th>User Input</th>
                <th>Action</th>
                <th>Tanggal</th>
              </tr>

              {{--  @php ---> before i suing datatables my view like that
              $no=0;
              @endphp

              @foreach ($pemeliharaan as $i)
              <tr>
                <td>{{ ++$no }} </td>
                <td>{{ $i->alat->nama_alat}}</td>
                <td>{{ $i->status}}</td>
                <td>{{ $i->user->name}}</td>
                <td> <a href="/user/show/question/{{ $i->id }}" > <span class="label label-primary">Lihat Data</span> </a></td>
                <td>{{ $i->created_at}}</td>
              </tr>
              @endforeach  --}}
            </tbody></table>

          </div>
  .

  .

       @endsection
  @push('scripts')
  <script>
    $(function() {
        $('#table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('usershow1dt') !!}',
            columns: [

                { data: 'nama_alat', name: 'nama_alat'},
                { data: 'status', name: 'status'},
                { data: 'User Input', name: 'nama'},
                { data: 'Action', name: 'name'},//here my button 
                { data: 'Tanggal', name: 'created_at'},

            ],
        });
    })
</script>
      @endpush

1 个答案:

答案 0 :(得分:0)

尝试一下:

public function show() { 
    $pemeliharaan = Pemeliharaan::where('user','alat')->where('status','harian')->get(); 
    return view('users.view_harian',['pemeliharaan' => $pemeliharaan]); 
}