0

I'm doing server side procesing and I'm adding columns with some css styles, but for some reason it isn't interpreted in html, it gets it only as text.

This is my php code.

    public function getCompras()
{
    $compra = Compra::with('empresas');
    return DataTables::of($compra)
        ->addColumn('empresas', function ($compra) {
            return $compra->empresas->first()->nombre;
        })
        ->addColumn('estado', function ($compra) {
            if ($compra->estado == 0) {
                return '<span class="label label-warning" >Pendiente</span>';
            } else {
                return '<span class="label label-success">Cobrado</span>';
            }
        })
        ->make(true);
}

My js

$(document).ready(function () {
  $('#tb_por_pagar').DataTable({
    processing: true,
    serverSide: true,
    ajax: '/cuentas/pagar/data',
    columns: [
      {data: 'factura_numero', name: 'factura_numero'},
      {data: 'total_transferencia', name: 'total_transferencia'},
      {data: 'fecha_pago', name: 'fecha_pago'},
      {data: 'empresas', name: 'empresas'},
      {data: 'estado', name: 'estado'},
    ],
    'language': {
      'url': '../plugins/dataTables.spanish.lang'
    }
  })
})

and this is the result

enter image description here

Thx for the help!

The method that's working enter image description here

enter image description here

miken32
  • 42,008
  • 16
  • 111
  • 154
Christian
  • 481
  • 1
  • 7
  • 24
  • Well, it's doing exactly what you tell it to do. Your php code is returning a string, and that string will get plugged into your data column. What exactly are you trying to accomplish? – BobRodes Sep 15 '18 at 02:59
  • change return to echo – Just L Sep 15 '18 at 03:02
  • @BobRodes, Hi, when I return this string, it its get in this line `{data: 'empresas', name: 'empresas'},` and interpreted as html code, I've done it before, that's why i cant undertand why it's not working now, I'll show u more pics with the working method and view – Christian Sep 15 '18 at 03:30
  • So, you are trying to add buttons in each field? – BobRodes Sep 15 '18 at 03:43
  • @BobRodes [This](https://yajrabox.com/docs/laravel-datatables/master/raw-columns) This in fact, i'd like to have comments with some ideas .... – Christian Sep 15 '18 at 04:01

2 Answers2

0

The solutions is to add ->rawColumns() to the end of our method

public function getCompras()
{
    $compra = Compra::with('empresas');
    return DataTables::of($compra)
        ->addColumn('empresas', function ($compra) {
            return $compra->empresas->first()->nombre;
        })
        ->addColumn('estado', function ($compra) {

                return "<span class='label label-warning' >Pendiente</span>";

        })

        ->rawColumns(['estado'])
        ->make(true);
}
Christian
  • 481
  • 1
  • 7
  • 24
0

The solutiuon is add to $dt->escapeColumns([])->make( true ) to the end of line. Here is my project code:

$data = DB::table( 'products' )->get();
$dt   = DataTables::of( $data );
$dt->addColumn( 'action', function ( $data ) {
    $button = '<a class="pointer edit" data-id="' . $data->id . '" title="Edit(' . $data->id . ')"><i class="fa fa-edit"></i></a>';
    $button .= '&nbsp;';
    $button .= '<a class="pointer delete" data-id="' . $data->id . '" title="Delete(' . $data->id . ')"><i class="fa fa-trash"></i></a>';

    return $button;
} );

return $dt->escapeColumns([])->make( true );
NASIR
  • 1
  • 1
  • 2