Reputation: 891
I am trying to add a checkbox to my DataTable but when I run my project it returns the checkbox in text as it is shown in my controller. How can I get this done from my controller?
When i run the project, i get the checkbox does not render in the template but shows raw text. What is happening ?
public function getItem()
{
$items = Item::all();
return Datatables::of($items)->addColumn('checkbox', function ($item) {
return '<input type="checkbox" id="'.item->id.'" name="someCheckbox" />';
})->make(true);
}
View
oTable = $('#users-table').DataTable({
"processing": true,
"serverSide": true,
"ajax": "{{ route('datatable.getitems') }}",
"columns": [
{data: 'checkbox', name: 'checkbox', orderable: false, searchable: false},
{data: 'name', name: 'name'},
{data: 'action', name: 'action', orderable: false, searchable: false}
],
});
Upvotes: 3
Views: 13240
Reputation: 305
it is not necessary to modify the config / datatables.php file, you can modify it directly in the return datatable instruction, adding:
->rawColumns(['checkbox','name_column'])
You can put it like this:
public function getItem()
{
$items = Item::all();
return Datatables::of($items)->addColumn('checkbox', function ($item) {
return '<input type="checkbox" id="'.item->id.'" name="someCheckbox" />';
})
->rawColumns(['checkbox'])
->make(true);
}
Upvotes: 1
Reputation:
->editColumn('select_orders', static function ($row) {
return '<input type="checkbox" name="registrations[]" value="'.$row->id.'"/>';
})->rawColumns(['select_orders'])
In config/datatables.php
search for action
and add checkbox to that array. Note: this will exclude this column globally from escaping
'raw' => ['action', 'checkbox'],
Inside your datatable.js
file
{data: 'select_orders', name: 'select_orders', searchable: false, orderable: false},
Upvotes: 0
Reputation: 3912
You have an error in your code:
return '<input type="checkbox" id="'.item->id.'" // should be $item
The Laravel Datatables package escapes the content of all columns by default – except for action
column. To exclude your checkbox
column from escaping you have two options:
In config/datatables.php
search for action
and add checkbox
to that array. Note: this will exclude this column globally from escaping
add ->rawColumns(['action', 'checkbox'])
to your definition
Upvotes: 1