如何在 laravel 数据表中使用 html 按钮添加操作 8.*
how to add action with html button in laravel datatables 8.*
我已经在我的项目上安装了 yajra laravel,我尝试使用带有 id 的 html 按钮添加操作列,我正在使用 yajra laravel,这些是我的 yajra laravel版本:
- "yajra/laravel-datatables-buttons": "3.*"
- "yajra/laravel-datatables-fractal": "1.*",
- "yajra/laravel-datatables-html": "3.*",
- "yajra/laravel-datatables-oracle": "8.*"
如何使用带有数据 ID 的 html 按钮添加操作列?
这里是我的数据表代码,用于在 laravel 中创建数据表:
public function html()
{
$brands = Brands::select(array('brands.id','brands.name','brands.slug','brands.description','brands.logo','brands.published','brands.created_at','brands.updated_at'));
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
// ->addAction(['width' => '80px'])
->removeColumn('id')
->addColumn($this->actionColumns())
->parameters($this->getBuilderParameters());
}
这是我在 laravel 中制作数据表的完整代码:
https://pastebin.com/v1qYGNjb
这里是结果数据表:
我想在操作列中添加按钮编辑和删除,如何使用 laravel datatables 8 来实现?
我正在使用 laravel 数据表作为服务。
public function html()
{
$brands = Brands::select(array('brands.id','brands.name','brands.slug','brands.description','brands.logo','brands.published','brands.created_at','brands.updated_at'));
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
// ->addAction(['width' => '80px'])
->removeColumn('id')
->addColumn('action',function ($data){
return $this->getActionColumn($data);
})
->parameters($this->getBuilderParameters());
}
/**
* @param $data
* @return string
*/
protected function getActionColumn($data): string
{
$showUrl = route('admin.brands.show', $data->id);
$editUrl = route('admin.brands.edit', $data->id);
return "<a class='waves-effect btn btn-success' data-value='$data->id' href='$showUrl'><i class='material-icons'>visibility</i>Details</a>
<a class='waves-effect btn btn-primary' data-value='$data->id' href='$editUrl'><i class='material-icons'>edit</i>Update</a>
<button class='waves-effect btn deepPink-bgcolor delete' data-value='$data->id' ><i class='material-icons'>delete</i>Delete</button>";
}
- 更改您的 addColumn() 方法并遵循此代码。
//UsersDataTable.php file
class UsersDataTable extends DataTable{
public function dataTable( $query ){
return datatables()
->eloquent( $query )
->addColumn( 'action', function( $data ){ // $data is data od current row in table
return view( 'admin.users.action', [ 'data' => $data ] ); // return view with objects (row) data parameters
//return '<a href="#">'.$data->id.'</a>'; // or simply return html here
} );
}
// some of class code here
protected function getColumns(){
return [
Column::make( 'id' ),
Column::make( 'name' )->title( 'Nazwa' ),
Column::make( 'email' ),
Column::make( 'created_at' ),
Column::make( 'updated_at' ),
Column::computed( 'action' )
->exportable( FALSE )
->printable( FALSE )
->width( 60 )
->addClass( 'text-center' ),
];
}
}
//UsersController.php file
use App\DataTables\UsersDataTable;
class UsersController extends Controller
{
public function index(UsersDataTable $dataTable)
{
return $dataTable->render('admin.users.index');
}
}
我已经在我的项目上安装了 yajra laravel,我尝试使用带有 id 的 html 按钮添加操作列,我正在使用 yajra laravel,这些是我的 yajra laravel版本:
- "yajra/laravel-datatables-buttons": "3.*"
- "yajra/laravel-datatables-fractal": "1.*",
- "yajra/laravel-datatables-html": "3.*",
- "yajra/laravel-datatables-oracle": "8.*"
如何使用带有数据 ID 的 html 按钮添加操作列?
这里是我的数据表代码,用于在 laravel 中创建数据表:
public function html()
{
$brands = Brands::select(array('brands.id','brands.name','brands.slug','brands.description','brands.logo','brands.published','brands.created_at','brands.updated_at'));
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
// ->addAction(['width' => '80px'])
->removeColumn('id')
->addColumn($this->actionColumns())
->parameters($this->getBuilderParameters());
}
这是我在 laravel 中制作数据表的完整代码: https://pastebin.com/v1qYGNjb
这里是结果数据表:
我想在操作列中添加按钮编辑和删除,如何使用 laravel datatables 8 来实现? 我正在使用 laravel 数据表作为服务。
public function html()
{
$brands = Brands::select(array('brands.id','brands.name','brands.slug','brands.description','brands.logo','brands.published','brands.created_at','brands.updated_at'));
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
// ->addAction(['width' => '80px'])
->removeColumn('id')
->addColumn('action',function ($data){
return $this->getActionColumn($data);
})
->parameters($this->getBuilderParameters());
}
/**
* @param $data
* @return string
*/
protected function getActionColumn($data): string
{
$showUrl = route('admin.brands.show', $data->id);
$editUrl = route('admin.brands.edit', $data->id);
return "<a class='waves-effect btn btn-success' data-value='$data->id' href='$showUrl'><i class='material-icons'>visibility</i>Details</a>
<a class='waves-effect btn btn-primary' data-value='$data->id' href='$editUrl'><i class='material-icons'>edit</i>Update</a>
<button class='waves-effect btn deepPink-bgcolor delete' data-value='$data->id' ><i class='material-icons'>delete</i>Delete</button>";
}
- 更改您的 addColumn() 方法并遵循此代码。
//UsersDataTable.php file
class UsersDataTable extends DataTable{
public function dataTable( $query ){
return datatables()
->eloquent( $query )
->addColumn( 'action', function( $data ){ // $data is data od current row in table
return view( 'admin.users.action', [ 'data' => $data ] ); // return view with objects (row) data parameters
//return '<a href="#">'.$data->id.'</a>'; // or simply return html here
} );
}
// some of class code here
protected function getColumns(){
return [
Column::make( 'id' ),
Column::make( 'name' )->title( 'Nazwa' ),
Column::make( 'email' ),
Column::make( 'created_at' ),
Column::make( 'updated_at' ),
Column::computed( 'action' )
->exportable( FALSE )
->printable( FALSE )
->width( 60 )
->addClass( 'text-center' ),
];
}
}
//UsersController.php file
use App\DataTables\UsersDataTable;
class UsersController extends Controller
{
public function index(UsersDataTable $dataTable)
{
return $dataTable->render('admin.users.index');
}
}