Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tntstudio's avatar

Using views for columns in Datatables

Hello everybody..

I'm using yajra/laravel-datatables in my project and need to have an additional column for view/edit/delete actions.. Currently I'm displaying it this way:

<script id="options" type="text/html">
<div class="text-right">
<a class="btn btn-xs btn-primary" href="/pages/id"><i class="glyphicon glyphicon-eye-open"></i> View</a>
<a class="btn btn-xs btn-warning" href="/pages/id/edit"><i class="glyphicon glyphicon-edit"></i> Edit</a>
<a class="btn btn-xs btn-danger"  href="/pages/id" delete><i class="glyphicon glyphicon-trash"></i> Delete</a>
</div>
</script>

<script>
$(function() {
    $('table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('pages.data') !!}',
        columns: [
        { data: 'id', name: 'id' },
        { data: 'title', name: 'title' },
        {
           sortable: false,
           render: function ( data, type, full, meta ) {
            return $('#options').html().replace(/id/g, full.id);                
            }
        }
    ]
});
});
</script>

According to the documentation of the yajra datatables package, it can be done in the controller:

http://datatables.yajrabox.com/eloquent/add-edit-remove-column

Is there a way to use a partial for this instead of writing the string in the addColumn method?

I'd like to do something like this in my controller:

public function data()
    {
        return Datatables::of(Page::select('*'))
        ->addColumn('action', function ($page) {
                return view('options', ['id' => $page->id]);
            })
        ->make(true);
    }

and I have a options.blade.php file also:

<a class="btn btn-xs btn-primary" href="{{ route('offers.show', $id) }}"><i class="glyphicon glyphicon-eye-open"></i> View</a>
<a class="btn btn-xs btn-warning" href="{{ route('offers.edit', $id) }}"><i class="glyphicon glyphicon-edit"></i> Edit</a>
<a class="btn btn-xs btn-danger"  href="{{ route('offers.destroy', $id) }}" delete><i class="glyphicon glyphicon-trash"></i> Delete</a>
0 likes
8 replies
tntstudio's avatar

I found one way to do this, I put (string) before the view method, now it looks like this:

my controller:

public function data()
    {
        return Datatables::of(Page::select('*'))
        ->addColumn('options', function ($page) {
                return (string) view('options', ['id' => $page->id]);
            })
        ->make(true);
    }

my view:

<script>
$(function() {
    $('table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('offers.data') !!}',
        columns: [
        { data: 'id', name: 'id' },
        { data: 'title', name: 'title' },
        { data: 'options', name: 'options', sortable: false, className: 'text-right' }
        ]
    });
});
</script>
2 likes
AdrianB's avatar

Thanks @tntstudio, my controller code is a mess at the moment, this'll clean it right up.

1 like
andylord565's avatar

View add this in javascript:

  {data: 'action', name: 'action', orderable: false, searchable: false}

Example of how i added edit/delete/view to one of my projects

public function anyData()
{
    //return Datatables::of(sales::select('*'))->make(true);

$sales = DB::table('sales')
            ->select(['id', 'name','source','phone','salesrep','contactname','status','notes','busname','secondname', 'callback']);


        return Datatables::of($sales)
            ->addColumn('action', function ($sales) {
                return '<a href="sale/'.$sales->id.'/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                <a href="sale/'.$sales->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-zoom-in"></i> View</a>

 <button class="btn btn-delete" data-remote="sale/' . $sales->id . '">Delete</button>
                ';
            })
            ->addColumn('phone', function ($sales) {
                return '<a href="tel:'.$sales->phone.'">'.$sales->phone.'</a>';
            })
            //->editColumn('id', 'ID: {{$id}}')
            ->make(true);
}

add column

        return Datatables::of($sales)
            ->addColumn('action', function ($sales) {
                return '<a href="sale/'.$sales->id.'/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                <a href="sale/'.$sales->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-zoom-in"></i> View</a>

 <button class="btn btn-delete" data-remote="sale/' . $sales->id . '">Delete</button>
                ';
            })

Example of my delete java in the view

$('#sales-table').on('click', '.btn-delete[data-remote]', function (e) { 
    e.preventDefault();
     $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
     
    var url = $(this).data('remote');
    // confirm then
    $.ajax({
        url: url,
        type: 'DELETE',
        dataType: 'json',
        data: {method: '_DELETE', submit: true}
    }).always(function (data) {
        $('#sales-table').DataTable().draw(false);
    });
});

Any questions i will try to answer later

Voyowsky's avatar

A bit late. Not sure which version you are using, but in the latest you can do:

->addColumn('actions',  'path.to.your.partial.view')
2 likes
jjudge's avatar

Just to follow up on the answer from @Voyowsky, this answer works very well and it pretty neat. It is not documented very well (like much of the Laravel DataTables package) but this will call up the view. It includes package-based views, such as 'MyPackage::view.path.in.package'.

The parameters passed to the view will be the columns selected in the query, so an App\User query would include {{ $id }} and {{ $name }} variables, for example.

delang's avatar

hi, would like to share my piece of code

in controller

$student = Student::select('id', 'name', 'email', 'age');
        return Datatables::eloquent($student)
            ->addColumn('action', function($student) {
                return view('student.action_student', compact('student'));
            })
            ->make(true);

in student.action_student.blade.php (not complete yet)

<a href="#edit" class="btn btn-xs btn-info"><i class="glyphicon glyphicon-edit"></i> view</a> 
<a href="#edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a> 
<a href="#edit" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-edit"></i> delete</a>
Cronix's avatar

@delang the forum uses markdown syntax. See the link under the reply box on how to use it. For code, it's 3 backticks (`) on it's own line, followed by code, followed by 3 more backticks on it's own line (with nothing else)

```

code

```

Please or to participate in this conversation.