Laravel Datatable

1-Önce composer ile kurulumu yapalım
"yajra/laravel-datatables-oracle": "~3.0",

2-Tablomuzu oluşturalım
<table class="table mb-none" cellspacing="0" width="100%">
<thead>
<tr>
                                                             
<th> Birimi</th>
<th>Türü</th>
<th> Tipi</th>
<th>Oluşturulma Tarihi</th>
<th>Güncellenme Tarihi</th>
<th>Düzenle</th>
<th>Sil</th>
</tr>
</thead>
</table>



<script type="text/javascript">
$(document).ready(function() {
var dt = $('table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('training-unit.index') }}",
columns: [
{data: 'name', name: 'name'},
                                {data: 'etiket', name: 'etiket',visible:false},
{data: 'type', name: 'type'},
{data: 'training_type', name: 'training_type'},
{data: 'created_at', name: 'created_at'},
{data: 'updated_at', name: 'updated_at'},
{data: 'edit', name: 'edit', searchable: false, orderable: false},
{data: 'destroy', name: 'destroy', searchable: false, orderable: false}
],
aaSorting: [[4, 'desc']]
});

dt.on('draw', function () {
window.ekampus.delete();
});
});
</script>

Controller tarafında

$trainings = Training::select(['id','etiket', 'name', 'type', 'training_type', 'created_at', 'updated_at']);

            $datatable = \Datatables::of($trainings)
                ->editColumn('name', function($training) {
                    return $training->name.$training->etiket;
                })
                ->editColumn('type', function($training) {
                    return $training->name;
                })
                ->editColumn('training_type', function($training) {
                    return $training->training_type_text;
                })
                ->editColumn('created_at', function($training) {
                    return $training->created_at->format('H:i d/m/Y');
                })
                ->editColumn('updated_at', function($training) {
                    return $training->updated_at->format('H:i d/m/Y');
                })
                ->addColumn('edit', function($training) {
                    return \HTML::linkRoute('training-unit.edit', 'Düzenle', $training->id, ['class' => 'btn btn-primary btn-xs']);
                })
                ->addColumn('destroy', function($training) {
                    return '<button type="button" data-toggle="delete-button" class="btn btn-primary btn-xs" data-url="' . route('training-unit.destroy', $training->id) . '">Sil</button>';
                })->removeColumn('id');

            return $datatable->make(true);
        }
        
        return View::make('Training::index');

Comments

Popular posts from this blog