如何在laravel数据表8中使用html按钮添加动作。*

时间:2018-12-17 03:40:05

标签: datatable datatables laravel-5.7

我已经在项目中安装了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

这里的结果数据表: enter image description here

我想在“操作”列中添加按钮编辑和删除,如何使用laravel datatables 8做到这一点? 我正在使用laravel数据表作为服务。

2 个答案:

答案 0 :(得分:1)

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>";
    }
  1. 更改您的addColumn()方法并遵循以下代码。

答案 1 :(得分:1)

main_repository
|-- .gitmodules
|-- .git
|   `-- modules
|       `-- submodule_xy .git files
|-- submodule_xy
  |-- .gitmodules
  `-- submodule_files