3

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}        
        ],

    });

3 Answers 3

1

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:

  1. In config/datatables.php search for action and add checkbox to that array. Note: this will exclude this column globally from escaping

  2. add ->rawColumns(['action', 'checkbox']) to your definition

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

1 Comment

how do it get id of the selected row?
1

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);     
}

Comments

0
->editColumn('select_orders', static function ($row) {
            return '<input type="checkbox" name="registrations[]" value="'.$row->id.'"/>';
        })->rawColumns(['select_orders'])

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

'raw' => ['action', 'checkbox'],

Inside your datatable.js file

{data: 'select_orders', name: 'select_orders', searchable: false, orderable: false},

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.