Last active
January 17, 2018 01:47
-
-
Save anestan/c95517f167bac8b47da0314bb21d630a to your computer and use it in GitHub Desktop.
datatable ajax laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```php | |
<?php | |
// Untuk Index SPK using Ajax | |
public function allSpk(Request $request) | |
{ | |
$columns = array( | |
0 => 'start_date', | |
1 => 'end_date', | |
2 => 'spkclosed_date', | |
3 => 'spk_no', | |
4 => 'spk_status', | |
5 => 'customer', | |
6 => 'co_number', | |
7 => 'tr_process', | |
8 => 'tr_status', | |
9 => 'fg_name', | |
10 => 'fg_qty', | |
11 => 'created_at', | |
12 => 'id', | |
); | |
$totalData = Spk::count(); | |
$totalFiltered = $totalData; | |
$limit = $request->input('length'); | |
$start = $request->input('start'); | |
$order = $columns[$request->input('order.0.column')]; | |
$dir = $request->input('order.0.dir'); | |
if(empty($request->input('search.value'))) | |
{ | |
$spks = Spk::offset($start) | |
->limit($limit) | |
->orderBy($order,$dir) | |
->get(); | |
} | |
else { | |
$search = $request->input('search.value'); | |
$spks = Spk::whereHas('finishgood', function($q) use ($search) { | |
$s = "%" . $search . "%"; | |
$q->where('fg_name', 'LIKE', $s); | |
}) | |
->orWhereHas('customer', function($q) use ($search) { | |
$s = "%" . $search . "%"; | |
$q->where('customer_name', 'LIKE', $s); // in this scope, `$q` refers to the MediaProfile object, not the User. | |
}) | |
->orWhere('spk_no','LIKE',"%{$search}%") | |
->orWhere('co_number', 'LIKE',"%{$search}%") | |
->offset($start) | |
->limit($limit) | |
->orderBy($order,$dir) | |
->get(); | |
$totalFiltered = Spk::whereHas('finishgood', function($q) use ($search) { | |
$s = "%" . $search . "%"; | |
$q->where('fg_name', 'LIKE', $s); | |
}) | |
->orWhereHas('customer', function($q) use ($search) { | |
$s = "%" . $search . "%"; | |
$q->where('customer_name', 'LIKE', $s); // in this scope, `$q` refers to the MediaProfile object, not the User. | |
}) | |
->orWhere('spk_no','LIKE',"%{$search}%") | |
->orWhere('co_number', 'LIKE',"%{$search}%") | |
->count(); | |
} | |
$data = array(); | |
if(!empty($spks)) | |
{ | |
foreach ($spks as $spk) | |
{ | |
$nestedData['start_date'] = $spk->formattedStartDate(); | |
$nestedData['end_date'] = $spk->formattedEndDate(); | |
$nestedData['spkclosed_date'] = $spk->formattedSpkclosedDate(); | |
$nestedData['spk_no'] = $spk->spk_no; | |
if (isset($spk)) { | |
$arr_spk_status = []; | |
if ( $spk->spk_status == 0 ) { | |
$arr_spk_status[0] = 'Open'; | |
$arr_spk_status[1] = 'info'; | |
} else if ( $spk->spk_status == 1 ) { | |
$arr_spk_status[0] = 'In Process'; | |
$arr_spk_status[1] = 'warning'; | |
} else { | |
$arr_spk_status[0] = 'Closed'; | |
$arr_spk_status[1] = 'danger'; | |
} | |
} else { | |
$arr_spk_status[0] = ''; | |
$arr_spk_status[1] = ''; | |
} | |
$nestedData['spk_status'] = '<span class="label label-'. $arr_spk_status[1] .'">'. $arr_spk_status[0] .'</span>'; | |
$nestedData['customer'] = $spk->customer->customer_name; | |
$nestedData['co_number'] = $spk->co_number; | |
if (isset($spk)) { | |
if( $spk->transition->count() > 0 ) { | |
$op_id = $spk->transition->last()->current_operation_id; | |
$op_name = \App\Operation::find($op_id)->operation_name; | |
$op_status = $spk->transition->last()->status; | |
$arr_op_status = []; | |
if ( $op_status == 0 ) { | |
$arr_op_status[0] = 'Open'; | |
$arr_op_status[1] = 'info'; | |
} else if ( $op_status == 1 ) { | |
$arr_op_status[0] = 'In Process'; | |
$arr_op_status[1] = 'warning'; | |
} else { | |
$arr_op_status[0] = 'Closed'; | |
$arr_op_status[1] = 'danger'; | |
} | |
} else { | |
$op_name = ''; | |
$arr_op_status[0] = ''; | |
$arr_op_status[1] = ''; | |
} | |
} else { | |
$op_name = ''; | |
$arr_op_status[0] = ''; | |
$arr_op_status[1] = ''; | |
} | |
$nestedData['tr_process'] = $op_name; | |
$nestedData['tr_status'] = '<span class="label label-'. $arr_op_status[1] .'">'. $arr_op_status[0] .'</span>'; | |
$nestedData['fg_name'] = $spk->finishgood->fg_name; | |
$nestedData['fg_qty'] = $spk->spkUnit->where('type', 'pcs_qty')->first()->qty; | |
$show = route('spk.show',$spk->id); | |
$edit = route('spk.edit',$spk->id); | |
$btn = 'btndelete_' . $spk->id; | |
$id = $spk->id; | |
$delete = route('spk.get.destroy', $spk->id); | |
// $nestedData['options'] = " <a href='{$show}' title='SHOW' ><span class='glyphicon glyphicon-eye-open'></span></a> | |
//  <a href='{$edit}' title='EDIT' ><span class='glyphicon glyphicon-edit'></span></a> | |
//  <a href='{$delete}' title='DELETE' onclick='return confirm('Are you sure?')''><span class='glyphicon glyphicon-remove'></span></a>"; | |
$nestedData['options'] = " <a href='{$show}' title='SHOW' ><span class='glyphicon glyphicon-eye-open'></span></a>"; | |
$data[] = $nestedData; | |
} | |
} | |
$json_data = array( | |
"draw" => intval($request->input('draw')), | |
"recordsTotal" => intval($totalData), | |
"recordsFiltered" => intval($totalFiltered), | |
"data" => $data | |
); | |
echo json_encode($json_data); | |
} | |
``` |
ini untuk js nya
<script>
$(document).ready(function () {
$('#dt-spk').DataTable({
"processing": true,
"serverSide": true,
// "searching": false,
"ajax":{
"url": "{{ url('allspk') }}",
"dataType": "json",
"type": "POST",
"data":{ _token: "{{csrf_token()}}"}
},
"columns": [
{ "data": "start_date" },
{ "data": "end_date" },
{ "data": "spkclosed_date" },
{ "data": "spk_no" },
{ "data": "spk_status" },
{ "data": "customer", "orderable": false },
{ "data": "co_number" },
{ "data": "tr_process" , "orderable": false },
{ "data": "tr_status" , "orderable": false },
{ "data": "fg_name" , "orderable": false },
{ "data": "fg_qty", "sClass": "text-right" , "orderable": false },
{ "data": "options" , "orderable": false },
],
dom: "Blfrtip",
buttons: [
{
extend: "copy",
className: "btn-sm hidden-print"
},
{
extend: "csv",
className: "btn-sm hidden-print"
},
{
extend: "excel",
className: "btn-sm hidden-print"
},
{
extend: "print",
className: "btn-sm hidden-print"
},
],
responsive: true,
destroy: true,
});
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ini untuk index.html