Yajra DataTable addColumn - laravel-5

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);

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();
}

Yajra Ajax Datatables Including Dynamic URL with parameter

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>

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 with join not working in laravel 5?

relation of customer with job
public function customer() {
return $this->belongsTo('App\Customer','customerid');
}
public function jobs(){
return $this->hasMany('App\Job','customerid');
}
in controller
protected function getJobs(){
$jobs = Job::Join('customer','jobs.customerid','=','customer.id')
->select(array('jobs.id','customer.firstname','customer.lastname','jobs.jobstatus','jobs.trialdate','jobs.deliverydate'));
return Datatables::of($jobs)
->addColumn('action', '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" title="Edit" href="{{ URL::to(\'updatejob/\'.$id) }}"><i class="fa fa-pencil"></i></a>')
->make();
}
it throw following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' (SQL: select `jobs`.`id`, `customer`.`firstname`, `customer`.`lastname`, `jobs`.`jobstatus`, `jobs`.`trialdate`, `jobs`.`deliverydate` from `jobs` inner join `customer` on `jobs`.`customerid` = `customer`.`id` order by `0` asc limit 10 offset 0)
i'm stuck in this issue from 2 day please help me to get out from this
I think you missed "order by" in your query, try this :
protected function getJobs(){
$jobs = Job::Join('customer','jobs.customerid','=','customer.id')
->select(array('jobs.id','customer.firstname','customer.lastname','jobs.jobstatus','jobs.trialdate','jobs.deliverydate'))
->orderBy('customer.lastname')->get();
return Datatables::of($jobs)
->addColumn('action', '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" title="Edit" href="{{ URL::to(\'updatejob/\'.$id) }}"><i class="fa fa-pencil"></i></a>')
->make();
}
I simply update the composer ->php composer.phar update
Its working fine now
Thanks

Resources