1

I have a problem with adding column into the jquery.datatables.

My datatable function looks like this

 public function getSliderImages()
{

    $query = Slider::with('User')->select('sliders.*');

    return Datatables::of($query)
        ->addColumn('fullName',function ($data){
            return $data->user->firstName .' '. $data->user->lastName;
        })
        ->addColumn('types',function ($data){
            return '<ul class="list-condensed list-unstyled no-margin">' .
                '<li><span class="text-semibold">Boyut:</span> '.$data->size.'</li>' .
                '<li><span class="text-semibold">Format:</span> ' .$data->ext.'</li></ul>';
        })

        ->make(true);
}

The returning json file is pretty good however here is the problem My datatable looks like thisit acts as a raw text not as a html

And last thing is my javascript

columns: [
                {data: 'slider_slug', name: 'slider_slug',"render": function(data, type, row) {
                    return '<a href="'+loc+'/'+data+'" data-popup="lightbox"> <img src="'+loc+'/'+data+'" class="img-rounded img-preview" </a>';
                },

                    orderable: false,
                    searchable: false
                },
                {data: 'slider_slug', name: 'slider_slug'},
                {data: 'fullName', name: 'fullName'},
                {data: 'created_at', name: 'created_at'},
                {data: 'action',name:'action'},
                {data: 'types',name:'types'},

The returning value is acts like a plain text but its html.

In the javascript I couldnt get 2 data from server.I mean I cannot get 2 variables in 1 row.

How can I solve this problem

3 Answers 3

6

Simple... just add ->rawColumns(['fullName', 'types'])

So... your code will be:

public function getSliderImages()
{

    $query = Slider::with('User')->select('sliders.*');

    return Datatables::of($query)
        ->addColumn('fullName',function ($data){
            return $data->user->firstName .' '. $data->user->lastName;
        })
        ->addColumn('types',function ($data){
            return '<ul class="list-condensed list-unstyled no-margin">' .
                '<li><span class="text-semibold">Boyut:</span> '.$data->size.'</li>' .
                '<li><span class="text-semibold">Format:</span> ' .$data->ext.'</li></ul>';
        })
        ->rawColumns(['fullName', 'types'])
        ->make(true);
}

See https://yajrabox.com/docs/laravel-datatables/master/raw-columns

Sign up to request clarification or add additional context in comments.

Comments

4

I think I have the solution to your problem, try this

 columns: [
              {data: 'slider_slug', name: 'slider_slug',
                            render:function (data, type, full, meta) {
                                console.log(full);
                                return '<a href="'+loc+'/'+data+'" data-popup="lightbox"> <img src="'+loc+'/'+data+'" class="img-rounded img-preview" </a>';
                            }
                    }

5 Comments

I cannot use this because I need two data here. Both $data->ext and $data->size
Why downvote? With full you can retrieve the values ​​of all the other columns
can you explain more ? btw I didnt downvoted I was searching
Thank you very much
Oh, np :) bye !
2

please use new rawColumns() method. you have to pass an array or columns that you need to be displayed as raw columns ( with HTML rendered)

example: (driver and action column will be rendered as raw - with HTML)

rawColumns(['driver','action'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.