Search code examples
laravelyajra-datatable

add route to yajra button


I need to make this record button as link to the route to display another blade file. also i need to pass variable with that route.

this is controller

$data = DB::table('vehicleregister')->orderBy('id')->get();

return datatables()->of($data)
->addIndexColumn()
->addColumn('action', function($data){

 

  $btn = '<div class="btn-group" role="group" >
  <button href="javascript:void(0)" type="button" data-id="'.$data->id.'" class="btn details btn-primary">Details</button>
  
  <button href="javascript:void(0)" type="button" data-id="'.$data->id.'" class="btn edit btn-secondary">Edit</button>
  <button href=""    type="button" data-id="'.$data->id.'" class="btn records btn-success">Records</button>
</div>';

return $btn;



})
->rawColumns(['action'])
->make(true);

i also try to make it with javascript .

$('body').on('click','.records',function(){

var record_id = $(this).data('id');

});


Solution

  • Try This, You need to use an anchor tag instead of a button tag.

    $data = DB::table('vehicleregister')->orderBy('id')->get();
    
    return datatables()->of($data)
        ->addIndexColumn()
        ->addColumn('action', function($data) {
     
        $btn = '<div class="btn-group" role="group" >
            <a href="' . route('route-name') . '" data-id="'.$data->id.'" class="btn details btn-primary">Details</a>
            <a href="' . route('route-name') . '" data-id="'.$data->id.'" class="btn edit btn-secondary">Edit</a>
            <a href="' . route('route-name') . '" data-id="'.$data->id.'" class="btn records btn-success">Records</a>
           </div>';
    
        return $btn;
    })
    ->rawColumns(['action'])
    ->make(true);