Yajra Ajax Datatables Including Dynamic URL with parameter - ajax

I am using Yarja datatables for quite a complex table set and also have the ajax part returning two buttons:
{
$user = Auth::user();
$cl = $user->client_id;
$jb = DB::table('job')
->join('job_status', 'job.jobStatus_id', '=', 'job_status.id')
->join('customers', 'job.customer_id', '=', 'customers.id')
->join('users', 'job.operative_id', 'users.id')
->where('job.client_id', $cl)
->select(['job.id as id', 'job_status.status as status', 'job.customer_id as customer_id', 'customers.customer as customer', 'users.name as operative','job.address as address','job.postcode as postcode','job.slug as slug','job_status.id as jobStatusID'])
->get();
return Datatables::of($jb)
->addColumn('action', function($pubs){
$btn = '<div style="float:right">
<i class="fas fa-book" ></i><i class="fas fa-edit" ></i></div>';
return $btn;
})
->make(true);
}
This works fine but now I want to add an action to the buttons, initially the edit, which is the route and the id of the row.
As you can see I have replaced the # with the route so I have
"admin\jobView"
but I cannot seem to work out a way of adding a field from the query (specifically jb->id) so that the action would be something like
admin\jobView\123
Just can't seem to get it and would greatly appreciate some help!

$pubs should contain your job, so you should be able to use $pubs->id, to get the job id.
What I would do though is replace this giant string with a view so it becomes easier to maintain:
return Datatables::of($jb)
->addColumn('action', function($job) {
return (string)view('admin.jobs.action', compact('job'));
})
->rawColumns(['action'])
->make(true);
Create a view, admin/jobs/action.blade.php for example:
<div style="float:right">
<a href="{{ route('admin.jobview', $job->id) }}" class="btn btn-outline-secondary btn-xs" title="show details" style="margin-right:.5em;font-size:.75em">
<i class="fas fa-book"></i></a>
<a href="{{ route('admin.jobview', $job->id) }}" class="btn btn-outline-secondary btn-xs" title="show details" style="margin-right:.5em;font-size:.75em">
<i class="fas fa-edit"></i>
</a>
</div>

Related

How to add button to open url with parameter in datatables Laravel 8

I have some data shown in my datatable view, I want to add button to each data to open detail page which can show more detail information
public function Task(Request $request)
{
if ($request->ajax()) {
$data = DB::table('posts')
->where('jabatan', Auth::user()->jabatan)
->select('user_id', 'name', DB::raw('count(user_id) as total'))
->selectRaw('SUM(status = "Selesai") as selesai')
->selectRaw('count(user_id) - SUM(status = "Selesai") as belum')
->groupBy('name')
->groupBy('user_id')->get();
return Datatables::of($data)
->addColumn('action', function ($row) {
$btn = ' <span class="fas fa-eye"></span>';
return $btn;
})
->rawColumns(['action'])
->addIndexColumn()
->make(true);
}
return view('task.Task');
}
the button can appear in my datatable, but it will open %7B%7Broute('detail.index',$row->user_id)%7D%7D ,
If in a html table I can use <a class="btn btn-info btn-sm" href="{{ route('detail.index',$post->id) }}">Show</a>
how to make the button to open /detail in url? thanks in advance
as your in already in php so don't use {{ }} blade syntax use
$btn = '<span class="fas fa-eye"></span>';

Yajra Datatables with custom buttons

I am using the Yajra Datatables with server side rendering in a Laravel project. But I want to add some custom buttons to the columns that will have Vue #click functions. I am trying to add like this but the click it's not working.
->addColumn('action', function($user){
$btn = '<a #click="delete_user('.$user->id.')"><i class="fa fa-times font-20 deleteUser"></i></a>';
return $btn;
})
Have you tried v-on:click in code. Can you please show the vue js code.
I have had the same problem, I do not want to reboot to the ease of working with Yajra or to the power of Vue. I have fixed it by calling the function directly from the Vue instantiated object.
<button id="btn_entrada_edit" class="btn btn-sm dropdown-item text-primary" onclick="modal_edit_entrada.edit_entrada(' . $entrada->id . ')" ><i class="fas fa-edit"></i> Editar</button>
This is on my controller, other js functions like copying the passw and loging as user are working fine. The only thing not working is Vue click:
public function getBasicData()
{
$user=User::all();
return Datatables::of(User::query())
->addIndexColumn()
->addColumn('view_user', function($user){
$btn =
''.$user->name.'';
return $btn;
})
->addColumn('action', function($user){
if(Cache::has('user-is-online-' . $user->id))
$btn3='<i class="fa fa-circle font-16 onlineStatus online"></i>';
else $btn3 ='<i class="far fa-circle font-16 onlineStatus offline"></i>';
$btn1 = '<a v-on:click="delete_user('.$user->id.')"><i class="fa fa-times font-20 deleteUser"></i></a>';
$btn2 = '<i class="fa fa-user font-20"></i><input id="myInput'.$user->id.'" class="copyPassInput" value="'.$user->real_password.'"><i onclick="myFunction('.$user->id.')" title="'.$user->real_password.'" class="fa fa-eye font-20 copyPassIcon"></i>';
$btn4='<div class="d-flex">'.$btn2.' '.$btn1.' '.$btn3.'</div>';
return $btn4;
})
->rawColumns(['view_user','action'])
->make();
}

How to update column in database in blade.php file Laravel

Hello I have row in a database table have an id and name and etc.. and active = 1
I want in blade.php file when someone click a button it change into 0 in 1 click
What I tried is {{$referral_detail->update('active', 1)}}
And
{{$referral_detail->IDT->where('active', 0)->update('active', 1)}}
Function in Controller is
public function DriverReferAll()
{
$query = DriverReferralDiscount::where([['referral_driver_id', '!=', 0],
['referral_sender_id', '!=', 0], ['active', '<>', 1]
]);
$referral_details = $query->paginate(25);
return view('merchant.driver.driver_referall', compact('referral_details', 'query'));
}
I got it thedit('{{$referral_detail->IDT->update(['active' => 1])}}')
and function
function thedit(a) {
$("input:submit").val(a);
}
But when I do it , it change the first 2 column not only 1 tho I put the specific id any idea?
its in onClick""
<span onClick=" thedit('{{$referral_detail->IDT->update(['active' => 1])}}')
" data-target="#addMoneyModel"
data-toggle="modal" id="{{ $referral_detail->GetterDriver->id }}"><a
data-original-title="Add Money"
data-toggle="tooltip"
id="{{ $referral_detail->GetterDriver->id }}" data-placement="top"
class="btn text-white btn-sm btn-success menu-icon btn_detail action_btn"> <i
class="fa fa-money-bill"></i> </a></span>
For that you must create a link in your blade template when that link is clicked It must have an href which point to the laravel route which will perform the update of your active state.
<a href="{{ route('your_route_name', ['id' => $referral_detail->id]) }}>Change</a>
the route can be define like this
Route::get('/route_path/{id}', 'ControllerName#action_name')->name('route_name');
Or If you want the browser to not refresh you can perform and Ajax

Having issue while sorting and pulling large data (yajra datatables)

I'm having an issue while pulling large data from yajra datatable server-side while applying the column sorting too.
For column sorting, I've used get() in the query builder. If I avoid it and use orderBy, it works fine and fetches records faster but the issue is column sorting doesn't work then.
$query = DB::table('clients');
if(Auth::user()->role_id==2)
{
$clients= $query->whereRaw('user_id',Auth::id());
}
else if(Auth::user()->role_id==3)
{
$getCompanyId = User::where('id',Auth::id())
->first();
$clients= $query->whereRaw('user_id',$getCompanyId['added_by']);
}
$clients= $query->orderBy('id');
$clients = $query->select();
$clients = $query->get();
$datatables = datatables()->of($clients)
->addColumn('action', function ($clients) {
return '<button class="btn btn-xs buttonhover" data-id='.$clients->id.' data-backdrop="static" onclick="editClient(this);"><i class="fa fa-edit" aria-hidden="true"></i> Edit</button>
<button class="btn btn-xs buttonhover2" data-id='.$clients->id.' onclick="viewDetails(this);"><i class="fa fa-eye" aria-hidden="true"></i> View</button>
<button class="btn btn-xs buttondeletehover"data-id='.$clients->id.' onclick="confirmAlertBox(this)"><i class="fa fa-trash" aria-hidden="true"></i> Delete</button>';
})
->make(true);
return $datatables;
Don't use of in datatables and don't get() data. Instead use eloquent to generate datatables object and query the sellect:
$users = User::query();
return DataTables::eloquent($users)
->toJson();
Or just use order by to generate eloquent object:
$users = User::orderBy('id');
Eloquent Data Source

Yajra DataTable addColumn

I use yajra datatable but i've get problem when I use the method "addColumn".
the one I use that method is work properly, but the other doesn't
this is my source code :
->addColumn('action', function($arrProduct){
return
'<center><a class="btn btn-success btn-sm" href="'.route('packaging.edit',['id' => $arrProduct['id'], 'product_id'=> $arrProduct['product_id']]).'">
<i class="fa fa-refresh"></i> Proses</a></center>';
})
->addColumn('status', function($arrProduct){
if($arrProduct['status_produksi']){
return ucwords($arrProduct['status_produksi']);
}else{
$tag = "<center><a class='btn btn-danger btn-sm'>
<i class='fa fa-refresh'></i>Belum Diproses</a></center>";
return $tag;
}
})
->make(true);
and this is the problem :
one of them (column action) is work properly, but why in the other (column status) the "addColumn" doesn't work?
pls somebody help me.. thank's anyway
Add ->rawColumns(['status', 'action'])
before ->make(true);

Resources