I'm trying to add a softdeletes warning message for my datatable, enter image description here
But the examples are for buttons, I use h ref link for access to the softdeletes function in controller
this is my datatable made with yajra
MailMessageDataTable.php
public function dataTable($query)
{
return datatables()
->eloquent($query)
->addColumn('action', function ($MailMessage) {
return '<a href="' . route('MailMessage.show', $MailMessage->id) . '"class="btn btn-outline btn-primary btn-xs dim"
data-toggle="tooltip" tittle="' . __('messages.index.show') . '"><i class="fa fa-eye"></i></a>'
.($MailMessage->deleted_at ? '' :
'<a href="' . route('MailMessage.edit', $MailMessage->id) . '"class="btn btn-outline btn-primary btn-xs dim"
data-toggle="tooltip" tittle="' . __('messages.index.edit') . '"><i class="fa fa-edit"></i></a>')
.'<a href="' . route('MailMessage.delete_message', $MailMessage->id) . '"class="btn btn-outline btn-primary btn-xs dim"
data-toggle="tooltip" tittle="' . __('messages.index.delete') . '"><i class="fa fa-trash"></i></a>';
});
}
the last href is the important to use the softdeletes
I have solved it as follows.
I have added just before the route a data-remote that captures the link
.'<a href="" data-name="' . $MailMessage->id .'" data-remote="' . route('mail-message.destroy', $MailMessage->id) . '"
class="btn btn-outline btn-primary btn-xs dim btn-action" style="margin-left:2px" data-toggle="tooltip" title="' . __('messages.index.delete') . '">
<i class="fa fa-trash"></i>
</a>';
and In the view I added the script
<script>
$(function () {
let taskTable = $('#task-table');
taskTable.on('click', '.btn-action[data-remote]', function (e) {
e.preventDefault();
let url = $(this).data('remote');
let name = $(this).data("name");
Swal.fire({
title: $('#swal-title').val() + ' ' + name,
text: $('#swal-message').val(),
icon: "question",
showConfirmButton: true,
showCancelButton: true,
}).then(function (value) {
if (value.value) {
axios.delete(url)
.then((response) => {
$('#task-table').DataTable().draw(false);
showAlert(response.data.message)
});
}
})
});
});
</script>
Related
I've create datatatable with Yajra on Laravel, here I want to display data on the table, here is my source code :
$model = SaranaPrasaranaRuang::with('jenis_ruang')->where('dibuat_oleh',$this->user->id)->get();
$dTable = Datatables()->of($model);
$dTable = $dTable->addIndexColumn()
->editColumn('jenis_ruang',function($ruang){
$jenis_ruang = $ruang->jenis_ruang->jenis_ruang;
return $jenis_ruang;
})
->editColumn('nama_ruang',function($data){
return $data->nama_ruang;
})
->addColumn('standard_prasarana',function($data){
$btn = '<button class="btn btn-warning detail_prasarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Prasarana</button>';
return $btn;
})
->addColumn('standard_sarana',function($data){
$btn = '<button class="btn btn-warning detail_sarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Sarana</button>';
return $btn;
})
->addColumn('action',function($data){
$btn = "";
$btn .= '<i class="fa fa-pencil"></i> Edit ';
$btn .= '</i> Hapus ';
$btn .= '<button class="btn btn-info" onclick=detailRuang('.$data->id.')><i class="fa fa-eye"></i> Detail</button>';
return $btn;
})
->rawColumns(['standard_prasarana','standard_sarana','action']);
return $dTable->make(true);
My code above is working fine if my record only 1, but when I added one more record on my table , Datable give me response json like this :
"error": "Exception Message:\n\nUndefined index: "
I've try with eloquent(),query() but still didn't work
Anyone can help me out ?
Try this
$model = SaranaPrasaranaRuang::with('jenis_ruang')->where('dibuat_oleh',$this->user->id)->get();
return DataTables::of($model)
->editColumn('jenis_ruang',function($ruang){
$jenis_ruang = $ruang->jenis_ruang->jenis_ruang;
return $jenis_ruang;
})
->editColumn('nama_ruang',function($data){
return $data->nama_ruang;
})
->addColumn('standard_prasarana',function($data){
$btn = '<button class="btn btn-warning detail_prasarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Prasarana</button>';
return $btn;
})
->addColumn('standard_sarana',function($data){
$btn = '<button class="btn btn-warning detail_sarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Sarana</button>';
return $btn;
})
->addColumn('action',function($data){
$btn = '';
$btn .= '<i class="fa fa-pencil"></i> Edit ';
$btn .= '</i> Hapus ';
$btn .= '<button class="btn btn-info" onclick=detailRuang('.$data->id.')><i class="fa fa-eye"></i> Detail</button>';
return $btn;
})
->rawColumns(['standard_prasarana','standard_sarana','nama_ruang','jenis_ruang','action']);
->addIndexColumn()
->make(true);
I'm working with role based permission using Zizaco Entrust package with yajra datatables.
when i'm giving permission to some users i have to touch datatables also.
This is my code ,
Controller.php
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', '#permission('user-edit')
View#endrole
Edit')
->make(true);
when i use permission inside datatables it is getting error , any one having idea to solve this ??same question in
yajra datatables and entrust role permission laravel
Entrust::can() checks if the user is logged in, and then if user has the permission. If the user is not logged the return will also be false.
Check below code:
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', function($company){
$action = '';
if (!Entrust::can('user-edit')) {
$action = 'View';
}
$action .= 'Edit';
return $action;
})
->make(true);
Made correction into code for {{}} and quotes issue.
You can also follow this technique
->addColumn('actions', function($admin) {
return view('admin.user.partials.admin_action', compact('admin'))->render();
})
And in your blade admin_action, you can write your view code as follows
#permission("admin-edit")
<a href="#" title="Edit"
class="btn-sm btn-primary editData"
data-id="{{ $admin->id }}"
data-fname="{{ $admin->f_name }}"
data-lname="{{ $admin->l_name }}"
data-email="{{ $admin->email }}"
data-redeem_manager="{{ $admin->redeem_manager }}"
data-mobile="{{ $admin->mobile }}"
data-role_id="{{ $admin->role_id }}"
><i class="fa fa-edit"></i> </a>
#endpermission
Entrust::can() not working with me causes an error.
but \Auth::user()->can('user-edit') works well with me
return datatables()->of($data)
->addColumn('actions', function ($data) {
$button = '<a class="btn btn-sm btn-info" href="' . route('users.show', $data->id) . '" >Show <i class="fa fa-eye"></i></a>';
if (\Auth::user()->can('user-edit')) {
$button .= ' <a class="btn btn-sm btn-primary" href="' . route('users.edit', $data->id) . '" >Edit <i class="fa fa-edit"></i></a>';
}
if (\Auth::user()->can('user-delete')) {
$button .= ' <a id="' . $data->id . '" class="delete btn btn-sm btn-danger" href="#" >Delete <i class="fa fa-trash"></i></a>';
}
return $button;
})
->rawColumns(['actions'])
->make(true);
It shows 500 internal server error when retrieving data through ajax. But the whole code works well in my localhost. And i am facing this error for the first time, so i am not sure whether this error is caused due to fetching data through AJAX. If it is not the correct reason please get me the correct reason.
this is my coding
$.ajax({
type:"POST",
url:"api_fle/get_post",
data:{u_id:u_id,type:'all13'},
success:function(response){
if(response!=0){
var parsed = $.parseJSON(response);
date = new Array();
events = new Array();
$.each(parsed,function(i,parsed){
if(parsed.shred.length>15){var shred=jQuery.trim(parsed.shred).substring(0, 14) + '...';} else{var shred=parsed.shred;}
if(parsed.cmpny_name == parsed.shred){var sharedd=parsed.cmpny_name; var sha=""; var pic=parsed.pro_pic;}else{var sharedd=shred; var sha=' shared <input type="hidden" id="who_hid_id" value="'+parsed.id+'">'+parsed.cmpny_name+"'s Event"; var pic=parsed.pic;}
date[i]=parsed.SharedDate;
events[i]='<div class="col-md-10 post" style="background:#FFF"><span class="company-logo-small"><img src="'+pic+'" style=" width: 60px; height: 60px;"></span>'
+'<span class="fullhead"><span class="posted-name"><a id="who_shred" style="color:#fff;cursor:pointer;"><input type="hidden" id="who_hid_id" value="'+parsed.id+'"><span itemprop="hiringOrganization">'+sharedd+'</span></a>'+sha+'</span></span>'
+'<span class="post-status" style="color:#fff;">'+prettyDate(parsed.SharedDate)+'</span><div class="post-inner"><div class="col-md-12"><div class="panel panel-default event">'
+'<div class="panel-heading title">'+parsed.name+'</div><ul class="list-group"><li class="list-group-item"><i class="fa fa-globe"></i>'+parsed.location+'</li>'
+'<li class="list-group-item"><i class="fa fa-calendar-o"></i>'+parsed.date+'</li><li class="list-group-item"><i class="fa fa-clock-o"></i>'+parsed.time+'</li>'
+'<li class="list-group-item"><i class="fa fa-users"></i>Attendees '+parsed.attendies+'</li></ul><ul class="list-group"><div class="panel-body"><p>'+parsed.decs+'</p>'
+'<a class="btn btn-xs btn-info pull-left" target="_blank" href="eventview?evnt_id='+parsed.evnt_id+'">View</a>  '
+'<i class="fa fa-fw fa-facebook-square" style="font-size:20px;"></i>'
+'<a class="twitter popup" href="pagelink?evnt_id='+parsed.evnt_id+'" target="_blank"><i class="fa fa-fw fa-twitter-square " style="font-size:20px;"></i></a>'
+'<a class="twitter popup" href="pagelink?evnt_id='+parsed.evnt_id+'" target="_blank">'
+'<i class="fa fa-fw fa-linkedin-square" style="font-size:20px;"></i></a><a class="twitter popup" href="pagelink?evnt_id='+parsed.evnt_id+'" target="_blank">'
+'<i class="fa fa-fw fa-google-plus" style="font-size:20px;"></i></a></div></ul><div id="img"></div><div class="clearfix"></div></div></div></div></div>';
});
}
});
-------------------------
page : get_post
--------------------
if($_POST['type']=='all13'){
$update_time=mysql_query("UPDATE `share_post` SET `sharedDate`='".$_POST['time']."' WHERE `frm_id` = 'U005114608238'");
$sql=select_query("SELECT s.id,e.u_id,e.cmpny_name,n.pro_pic as pic,l.pro_pic,l.level as lv,m.evnt_id,m.name,m.location,m.decs,m.time,
m.date,m.attendies,s.frm_id,s.is_important,s.shred,s.SharedDate,s.lvl FROM employer_info e,login l,login n,`event` m, share_post s WHERE n.u_id=s.frm_id and e.u_id=l.u_id and m.u_id=l.u_id and m.evnt_id=s.post_id and s.to_id='".$_POST['u_id']."' order by s.id desc");
$count=count($sql);
$response=array();
for($i=0;$i<$count;$i++){
array_push($response,$sql[$i]);
}
echo json_encode($response);
}
when i have inspected the error , i got something like in this screenshot
I'm using Yajra datatable, I had an issue as what title said.
here's my controller so far :
public function makeActionButtonsForDatatable($model)
{
if ($model->employed_type == DriverEmployedType::PARTTIME) {
return '<a href="' . route('parttimeavailable.create', ['driver' => $model->id]) . '"
class="btn btn-default btn-xs"></span>Set Parttime
</a>
<a href="' . route('driver.edit', ['driver' => $model->id]) . '"
class="btn btn-primary btn-xs"><span class="fa fa-pencil"></span> Edit
</a>
<a id="delBtn" data-url="'.route('driver.destroy', ['driver' => $model->id]).'"
data-toggle="modal" data-target=" #modalDelete" data-title="Confirmation" data-table-name="#datatable" data-message="Do you want to delete this record?"
class="btn btn-danger btn-xs delete" >
<span class="fa fa-trash-o"></span> Delete
</a>';
}
switch ($model->status) {
default:
return'
<a href="' . route('driver.edit', ['driver' => $model->id]) . '"
class="btn btn-primary btn-xs"><span class="fa fa-pencil"></span> Edit
</a>
<a id="delBtn" data-url="'.route('driver.destroy', ['driver' => $model->id]).'"
data-toggle="modal" data-target=" #modalDelete" data-title="Confirmation" data-table-name="#datatable" data-message="Do you want to delete this record?"
class="btn btn-danger btn-xs delete" >
<span class="fa fa-trash-o"></span> Delete
</a>
';
break;
}
}
public function makeDatatable($obj) {
return Datatables::of($obj)
->addColumn('action', function ($model) {
return $this->makeActionButtonsForDatatable($model);
})
->editColumn('full_name', function($model) {
return '<a href="' . route('driver.edit', ['driver' => $model->full_name]) . '"</a>';
})
->editColumn('employed_type', function($model){
return DriverEmployedType::getString($model->employed_type);
})
->make(true);
}
and here's my datatable code so far :
$(document).ready(function(){
var datatable = $('#datatable').DataTable(
{
dom: "lrtip",
responsive: true,
processing: true,
serverSide: true,
bSortCellsTop: true,
ajax: {
url: "{{ route('driver.list') }}",
data: { '_token' : '{{csrf_token() }}'},
type: 'POST',
},
columns: [
{ data: 'full_name', name: 'full_name', className: 'text-center', },
{ data: 'mobile', name: 'mobile', className: 'text-center', },
{ data: 'nric', name: 'nric', className: 'text-center', },
{ data: 'license', name: 'license', className: 'text-center', },
{ data: 'employed_type', name: 'employed_type', className: 'text-center', },
{ data: 'action', name: 'action', className: 'text-center', orderable: false, searchable: false },
],
});
});
It didn't works, and didn't display my fullname column. I want is once I click the name it goes to edit form with that driver Id.
any idea ?
I think you missed to close <a> for fullName column, try below code:
public function makeDatatable($obj) {
return Datatables::of($obj)
->addColumn('action', function ($model) {
return $this->makeActionButtonsForDatatable($model);
})
->editColumn('full_name', function($model) {
return ''. $model->full_name .'';
})
->editColumn('employed_type', function($model){
return DriverEmployedType::getString($model->employed_type);
})
->make(true);
}
Let me know if you still facing the same!
This is my Controller:
public function anyData()
{
$myID = Auth::guard('cashier')->user()->id;
$players = User::where('parent_id','=', $myID)
->where('delete', '=', 1)
->orderBy(DB::raw('LENGTH(name),name'))
->get(['id', 'name', 'score', 'alarm', 'bonus_from_percentage']);
return Datatables::of($players)
->addColumn('actions4', function ($players) {
return
'
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#actions-modal" class="open-playerID btn btn-warning" data-toggle="modal"
data-target="#actions-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.SETTINGS').'"><span class="glyphicon glyphicon-cog"></span></a>
';
})
->addColumn('actions3', function ($players) {
return
'
<button type="button" class="'.changeClass($players->alarm).'"
value="OK" onclick="Enable('.$players->id.','.$players->alarm.')" id="enable"/><span class="glyphicon glyphicon-ok-circle"></span></button>
';
})
->addColumn('actions2', function ($players) {
return
'
<a href="#" data-toggle="modal" data-id="'.$players->id.'" data-target="#changeplayerpassword-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.CHANGEPLAYERPASS').'" class="playeridchangepass btn btn-warning">
<span class="glyphicon glyphicon-edit"></span></a>
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#delete-modal" class="open-delete btn btn-warning" data-toggle="modal"
data-target="#delete-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.DELETE').'" ><span class="glyphicon glyphicon-remove"></span></a>
';
})
->addColumn('actions', function ($players) {
return
'<div class="btn-group text-center">
<button onclick="changeValueIn(this);" class="btn btn-primary btn-sm" style="width: 50px;" data-score-id="' . AppHelper::ToEuroC($players->score) . '" data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#In">IN</button>
<button onclick="changeValue(this)"class="btn btn-danger btn-sm" data-score-id="' . AppHelper::ToEuroC($players->score) . '"data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#Out">OUT</button>
</div>
';
})
->addColumn('score', function ($players) {
return AppHelper::ToEuroC($players->score);
})
->addColumn('bonus_from_percentage', function ($players) {
return AppHelper::ToEuroC($players->bonus_from_percentage);
})
->addColumn('players', function ($players) {
return ($players->name) . ' ' .dikOn($players->id);
})
->make(true);
}
This script i run to refresh table each 5 seconds:
// REFRESH TABLE
function refreshptable() {
if (table != null)
table.ajax.reload(null, false);
setTimeout(refreshplayers
, 5000);
}
wen i upload on server my laravel project (i have active around 300 Users online)
CPU load going 200+
On my old PHP code (No Laravel) CPU load is 10-15
its something wrong with datatables or is laravel ?
i check the request is same no loops and all querys is same the only difference is laravel..
on TOP -C laravel use 3% my old code use 0.3% ..