Laravel-datatables: HTML in cells

Created on 27 Feb 2017  路  14Comments  路  Source: yajra/laravel-datatables

Hello,
I am using laravel-datatable in laravel version 5.4.
When I use:

    public function getJSONUserDataTables()
    {
        return Datatables::of(User::orderBy('created_at', 'desc'))
        ->addColumn('actions',function($user) {
            return '<a href="" class="btn btn-warning">賵蹖乇丕蹖卮</a>'.'<a href="" class="btn btn-danger">丨匕賮</a>';
        })
        ->make(true);
    }

in column I get: html code and the buttons not displayed.

It looks :http://dt54.yajrabox.com/eloquent/array has problem too.

question

Most helpful comment

Use ->rawColumns(['test', 'action']).

All 14 comments

@aghict please see upgrade guide for breaking changes and how to fix this. Thanks!

Hi,
I have the same problem here, i make the upgrade and i steel have th problem ( i can add only one column with a HTML code, the last one )
When i try to add other column with HTML code it display in balises

->addColumn('test', function ($user) {
                return '<a href="#edit-'.$user->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
            })
->addColumn('action', function($role) {
                return '<a href="aze">
                            <button type="button" class="btn btn-purble waves-effect waves-light" data-toggle="tooltip" data-placement="top" data-original-title="Voir permisions de role">
                               <i class="icofont icofont-eye-alt"></i>
                            </button>
                        </a>
                        <a href="'.route('companys.edit', array('id' => $role->id)).'">
                            <button type="button" class="btn btn-warning waves-effect waves-light" data-toggle="tooltip" data-placement="top" data-original-title="Modifier ce role">
                               <i class="icofont icofont-pencil-alt-5"></i>
                            </button>
                        </a>';
            })

Use ->rawColumns(['test', 'action']).

Thank you, rawColumns(['test', 'action']) resolve the problem.

Thanks
Now I want to use blade like ->addColumn('action', 'admin.user.action') now how can I pass variables to blade template?

Full code that I want to use is:
return $datatables->collection($users) ->addColumn('action', function ($user) { return view('admin.user.action' ,compact($user)); })->make(true);

I have same problem too.

Use ->rawColumns(['test', 'action']) Solved the problem thanks @yajra

if we use rawColumns the html and the value will be rendered as HTML,
how about if I want to just escape the "value" but not escaping the entire cell?

just like
return '<a>'.e($user->name).'</a>';

any solution?

worked for me !

                  ->addColumn('magment', function(MainPages $MainPages) {

                    if($MainPages->status === 0 ){
                      $color = 'btn-success';
                      $btnicon ='<i class="fa fa-check-circle-o" aria-hidden="true"></i>';
                      $btnword= trans("app.enable");
                    }else{
                      $color = 'btn-warning';
                      $btnicon ='<i class="fa fa-ban" aria-hidden="true"></i>';
                      $btnword= trans("app.disabled");
                    }

                 return
                 '<a href="#" data-id="' . $MainPages->id . '" class="btn edititem btn-primary">
                 <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                 '.trans("app.edit").'</a>
                </a>
                <a data-id="' . $MainPages->id . '" href="#"  class="tooglestatus btn '.$color.' ">
                '.$btnicon.'
                '.$btnword.'
                </a>
                 <a data-id="' . $MainPages->id . '" href="/mebel_site_admin/managepage/' . $MainPages->id . '"  class=" btn btn-primary">
                  <i class="fa fa-plus" aria-hidden="true"></i>
                 '.trans("app.addsubpage").'
                  </a>  ';
                                })


                 ->escapeColumns([])
                    ->make(true);

i can't understand how to use ->rawColumns(['test', 'action']). can u describe it how it works on any column

There is no "rawColumns" method in the source.

@rokde that depends on the version. See docs for ref: https://yajrabox.com/docs/laravel-datatables/master/raw-columns

Ah, that worked. Thanks.

Maybe someone has the same problem, here is my working code.

$dataTable = new EloquentDataTable($query);
$dataTable->addColumn('icon', function (Widget $widget) {
    return sprintf('<i class="%s"></i>', $widget->icon);
});
$dataTable->addColumn('action', 'reporting.widgets.datatables_actions');
return $dataTable->rawColumns(['icon', 'action']);

it works thanks

Funciona muchas gracias

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FaZeRs picture FaZeRs  路  18Comments

baig772 picture baig772  路  14Comments

aliworkshop picture aliworkshop  路  14Comments

faisalhilmi picture faisalhilmi  路  18Comments

marioene picture marioene  路  22Comments