Laravel-datatables: Attributes in td cell

Created on 16 Mar 2016  ·  3Comments  ·  Source: yajra/laravel-datatables

Summary of problem or feature request

I can't create custom attributes with Collection data. Example: <td data-order="data"></td>. Create custom attributes for row works fine, but I need create attributes for a cell (<td>). It's possible? This is my code...

Code snippet of problem

return Datatables::of($users)
            ->addColumn('action', function ($user) {
                return '<a href="#edit-'.$user->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
            })
            ->editColumn('id', '{{$id}}')
            ->removeColumn('password')
            ->setRowId('id')
            ->setRowClass(function ($user) {
                return $user->id % 2 == 0 ? 'alert-success' : 'alert-warning';
            })
            ->setRowData([
                'id' => 'test',
            ])
            ->setRowAttr([
                'color' => function($user){
                        return $user->color;
                }
            ])
            ->make(true);

System details

  • Ubuntu
  • PHP 5.9
  • Laravel Version 5.1

Most helpful comment

Adding attribute on td cannot be done on server-side. But it can be done on client-side via js script using createdRow callback. See example snippet below. Source: http://datatables.net/forums/discussion/31139/how-to-add-data-attribute-in-a-cell-server-side

$('#data-table').DataTable({
    ajax: 'some_url',
    createdRow: function( row, data, dataIndex ) {
        $( row ).find('td:eq(2)').attr('data-validate', '1');
    }
});

All 3 comments

Adding attribute on td cannot be done on server-side. But it can be done on client-side via js script using createdRow callback. See example snippet below. Source: http://datatables.net/forums/discussion/31139/how-to-add-data-attribute-in-a-cell-server-side

$('#data-table').DataTable({
    ajax: 'some_url',
    createdRow: function( row, data, dataIndex ) {
        $( row ).find('td:eq(2)').attr('data-validate', '1');
    }
});

Thanks!! That's code works fine ;)

@yajra is this serverside supported now ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mopster picture Mopster  ·  3Comments

jgatringer picture jgatringer  ·  3Comments

NidhiDesai11 picture NidhiDesai11  ·  3Comments

nasirkhan picture nasirkhan  ·  3Comments

hohuuhau picture hohuuhau  ·  3Comments