Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mehedithedue/c5645e3ff0fc046357835689fba4b1ac to your computer and use it in GitHub Desktop.
Save mehedithedue/c5645e3ff0fc046357835689fba4b1ac to your computer and use it in GitHub Desktop.
Datatable Reload on Select Option or other and Server side code with Laravel Yajra
function getDatatable(parameter = null) {
var token = $('input[name="_token"]').val();
$('#datatable').DataTable({
iDisplayLength: 25,
processing: true,
serverSide: true,
searching: true,
ajax: {
url: '{{url("server/get-datatable-data")}}',
method: 'post',
data: function(data) {
data._token = token;
data.param = parameter;
data.status = 1;
}
},
columns: [{
data: 'column_one',
name: 'column_one'
},
{
data: 'column_two',
name: 'column_two'
},
{
data: 'details_text',
name: 'details_text'
},
{
data: 'status',
name: 'status'
},
{
data: 'action',
name: 'action',
orderable: false,
searchable: false
}
],
"aaSorting": []
});
getDatatable();
$('.search').on('click', function() {
var type = $("#search_option").val();
$("#datatable").dataTable().fnDestroy();
getDatatable(type);
});
use yajra\Datatables\Datatables;
//https://github.com/yajra/laravel-datatables
public function getDatatable(Request $request) {
$type = $request->param;
$status = $request->status;
$datatableData = Modal::leftJoin('table_two', 'table_one.id', '=', 'table_two.foreign_key')
->where('table_one.is_deleted', '0')
->where(function ($query) {
$users = Auth::user()->id;
if ($users == '1') {
->where('table_one.created_by', '=', $users)
->where('table_one.status', 1);
} else if (in_array($users, [2,3,5])) {
$query->whereIn('pilgrims.created_by', [2,3,5]);
} else {
$query->where('table_one.status', $status)
->where('table_one.created_by', Auth::user()->id);
}
});
if ($data == 'conditioned') {
$datatableData = $datatableData->whereRaw('(table_two.status is null and table_one.status is null)');
}
$datatableData = $datatableData->orderBy('table_one.created_at', 'desc')->get(['column_one', 'column_two']);
return Datatables::of($datatableData)
->addColumn('action', function ($data) use ($type) {
if ($type == 'enable') {
return '<a href="' . url('some-edit-link/' .$data->id ) . '/edit" class="btn btn-xs btn-primary">Edit</a>';
} else return '';
})
->addColumn('status', function ($data) {
if ($data->status == 0) {
return '<span class="text-warning">Draft</span>';
} else {
return $data->status == '1' ? 'Submit' : 'Not Yet';
}
})
->editColumn('details_text', function ($data) {
return substr($data->details_text, 0, 25);
})
->removeColumn('id')
->make(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment