yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 857 forks source link

Where to put columns.render options ? #3195

Open stepgr opened 1 day ago

stepgr commented 1 day ago

Summary of problem or feature request

In plain JS something like below is used to make Blade display an icon when a boolean value is set. Following your example from Quick Starter where am I supposed to add this code below ? Blade file ? ControllerDataTable.php ? Controller.php ? I'm lost !!

Code snippet of problem

"columnDefs": [{
  "targets": [5, 6],
  "render": function ( data, type, row ) {
    if ( data == "1" )
    {return '<i class="fas fa-check-circle"></i>';}
    else if ( data == "0" )
    { return ' ';}
  },
}],

System details

stepgr commented 20 hours ago

Ok continuing on my own can someone confirm I'm going on the right track ? this is expanding the Quick starter code but there is formatting issues when I put the render function after the targets :

public function html(): HtmlBuilder
    {
        return $this->builder()
                    ->setTableId('users-table')
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    //->dom('Bfrtip')
                    ->orderBy(2, 'asc')
                    ->selectStyleSingle()
                    ->columnDefs(
                        ['targets', [4]],
                    )
yajra commented 16 hours ago

You can do it via Column builder:

Column::make('amount')->renderRaw('function() {alert("test")}'),
Column::make('amount')->render('(data).toFixed(2).replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",")');

You can also use a renderer. Assuming you have this render function $.fn.dataTable.render.rendererFunction:

Column::make('amount')->renderJs('rendererFunction')