1

I have installed yajra laravel on my project, I try to add action column with html button with id, I am using yajra laravel, these are my yajra laravel version:

  • "yajra/laravel-datatables-buttons": "3.*"
  • "yajra/laravel-datatables-fractal": "1.*",
  • "yajra/laravel-datatables-html": "3.*",
  • "yajra/laravel-datatables-oracle": "8.*"

How to add Action column with html button with id of data ?

here my datatables code to make datatables in 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());
    }

this is my complete code for make datatables in laravel: https://pastebin.com/v1qYGNjb

here the result datatable: enter image description here

I want to add button edit and delete in column Actions, how to do that with laravel datatables 8? I am using laravel datatables as a service.

2 Answers 2

4
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. Change your addColumn() method and follow this code.
Sign up to request clarification or add additional context in comments.

Comments

2
//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');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.