我正在使用带有html builder的yajra datatebles来显示服务器端。在这里,我必须从特定角色中隐藏动作栏中的删除按钮。所以我正在使用spatie许可包并添加@can()来隐藏删除按钮。如下。
UserDatatable.php
namespace App\DataTables;
use App\User;
use DB;
use Builder;
use Auth;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\Html\Column;
class UsersDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
return datatables($query)
->addIndexColumn()
->editColumn('status', function(User $user){
if($user->status == 1)
return '<label class="label label-success">Active</label>';
else return '<label class="label label-danger">Deactive</label>';
})
->editColumn('created_at', function(User $user){
return date('d-m-Y H:i:s',strtotime($user->created_at));
})
->editColumn('checkbox', function(User $user){
return '<input type="checkbox" data-id="'.$user->id.'" class="sub_chk" name="ids[]" />';
})
->addColumn('action', function(User $user) {
return view('backend.users.usersaction', compact('user'))->render();
})
->rawColumns(['checkbox','status','action']);
}
/**
* Get query source of dataTable.
*
* @param \App\User $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(User $model)
{
$users = User::select('id','name','email','status', 'created_at', 'updated_at');
if($this->request()->get('startDate')) {
$st = $this->request()->get('startDate');
$dt = ($this->request()->get('endDate') == '') ? date('Y-m-d') : $this->request()->get('endDate');
$users->whereDate('created_at','<=', "$dt");
$users->whereDate('created_at','>=', "$st");
}
if($this->request()->get('status') == '0' || $this->request()->get('status') == '1') {
$users->where('status', $this->request()->get('status'));
}
// $users->orderBy('created_at','DESC')
$users->get();
return $this->applyScopes($users);
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addAction(['width' => '10%'])
->addCheckbox(['width'=> '10px'],true)
->parameters($this->getBuilderParameters())
->parameters([
'order' => [
5, // here is the column number
'desc'
],
'scrollX' => true,
'extend' => 'collection',
'text' => 'Export',
'dom' => 'Bfrtipl',
// 'buttons' => ['csv', 'excel', 'pdf' , 'print'],
'initComplete' => "function () {
this.api().columns().every(function () {
var column = this;
var input = document.createElement(\"input\");
$(input).appendTo($(column.footer()).empty())
.on('change', function () {
column.search($(this).val(), false, false, true).draw();
});
});
}",
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'DT_RowIndex'=>['width'=>'10px','title'=>'S.No','searchable'=>false,'orderable'=>false],
'name',
'email',
'status',
'created_at'
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Users_' . date('YmdHis');
}
}
下面是我的操作视图,其中添加了条件。
usersaction.blade.php
@can('View User')
<a href="javascript:void(0);" data-user_id="{{ encrypt($user->id) }}" class="btn btn-sm btn-default viewUser"><i class="fa fa-eye"></i></a>
@endcan
@can('Edit User')
<a href="{!! url('admin/editUser/'.encrypt($user->id)) !!}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></a>
@endcan
@can('Delete User')
<a href="javascript:void(0);" data-user_id="{{ encrypt($user->id) }}" class="btn btn-sm btn-danger deleteUser"><i class="fa fa-trash"></i></a>
@endcan
这里的视图用户,编辑用户,删除用户是由laravel spatie权限包创建的权限。它的工作正常,唯一的问题是在数据表视图操作列中是向左移动并在右侧留有太多空白请看下面这张桌子的图片。
我尝试了以下另一种方法
->addColumn('action', function(User $user){
$actionBtn='';
if(Auth::user()->can('View User')){
$actionBtn .= '<a href="javascript:void(0);" data-user_id="'.encrypt($user->id).'" class="btn btn-sm btn-default viewUser"><i class="fa fa-eye"></i></a>';
}
if(Auth::user()->can('Edit User')){
$actionBtn .= '<a href="'.url('admin/editUser/'.encrypt($user->id)).'" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></a>';
}
if(Auth::user()->can('Delete User')){
$actionBtn .= '<a href="javascript:void(0);" data-user_id="'.encrypt($user->id).'" class="btn btn-sm btn-danger deleteUser"><i class="fa fa-trash"></i></a>';
}
return $actionBtn;
})
但是没有得到相同的结果。请先谢谢您的帮助。
答案 0 :(得分:0)
$action = "";
if(Auth::user()->can('floor-edit')){
$action .= '<a class="btn btn-info btn-sm" href="'.route("floor.edit",Crypt::encrypt($floor->id)).'"><i class="fa fa-edit"></i></a>';
}
if(Auth::user()->can('floor-delete')){
$action .= ' <button type="button" name="delete" id="'.Crypt::encrypt($floor->id).'" class="delete btn btn-danger btn-sm"><i class="fa fa-trash"></i></button>';
}
它为我工作