Adding Checkbox to Datatables - Jquery & Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering DataTables: Adding Interactive Checkboxes with Laravel and jQuery

Dealing with dynamic data tables in web applications often involves adding interactive elements like checkboxes, sorting controls, or custom rendering. When integrating Laravel's powerful backend capabilities with front-end libraries like DataTables and jQuery, developers frequently run into issues where the intended HTML structure doesn't render correctly—it displays as raw text instead of interactive elements.

This post dives deep into the specific problem you are facing: why your generated checkbox code appears as plain text, and how to correctly inject dynamic HTML from your Laravel controller into a DataTables setup using jQuery.

The Mystery of Raw Text Rendering

You have correctly identified the core issue: when your controller returns a string containing HTML (like <input type="checkbox"...), DataTables, by default, treats that content as plain text rather than rendering it as actual interactive HTML elements. This happens because the standard JSON response from your Laravel route is being parsed and displayed directly into a <td> element without explicit instruction on how to interpret the data type.

Your current approach in the controller:

public function getItem()
{
  $items = Item::all();
  return Datatables::of($items)->addColumn('checkbox', function ($item) {
    // This returns a string, which is rendered as text by default
    return '<input type="checkbox" id="' . $item->id . '" name="someCheckbox">';
  })-->make(true);     
}

While this generates the correct HTML string, DataTables needs context to know that this specific column should be treated as HTML content. When using server-side processing with DataTables, you must ensure your response structure aligns with what the client expects for complex data types.

The Solution: Ensuring Proper HTML Serialization

To successfully display interactive elements like checkboxes in a DataTables setup, you need to ensure that the data returned from your Laravel endpoint is properly serialized and interpreted by the front end as HTML.

1. Correcting the Server-Side Output

Instead of trying to inject complex HTML strings directly into a standard column definition for simple data fields, the most reliable method in a server-side context (like using serverSide: true with AJAX) is to structure your response so that the column data itself contains the necessary HTML markup.

If you are using DataTables' built-in server-side processing capabilities, the response format usually expects an array of data for each row. You need to return the HTML directly within the appropriate cell data field.

Here is how you should adjust your controller method to ensure the output is interpreted correctly:

use Illuminate\Support\Facades\DB; // Assuming you are using Eloquent/DB operations

public function getItem()
{
    $items = DB::table('items')->get();
    $data = [];

    foreach ($items as $item) {
        // Construct the checkbox HTML for this specific item
        $checkboxHtml = '<input type="checkbox" class="item-checkbox" data-id="' . $item->id . '">';

        $data[] = [
            $checkboxHtml, // The content for the checkbox column
            $item->name,    // Data for the name column
            'action'         // Placeholder data
        ];
    }

    // Return the structured array that DataTables expects
    return $data; 
}

2. Configuring DataTables to Render HTML

Once your Laravel endpoint returns this structured array, you need to adjust your front-end configuration to tell DataTables how to render the content of the first column. You achieve this by defining the render function within the columns definition.

In your JavaScript initialization, instead of relying on a simple data: 'checkbox' mapping, you define a custom rendering function for that specific column:

oTable = $('#users-table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{ route('datatable.getitems') }}",
    "columns": [
        {
            // Custom rendering function for the checkbox column
            "data": null, // We don't use data from the array directly here
            "name": "checkbox_column", 
            "orderable": false,
            "searchable": false,
            "render": function (data, type, row) {
                // 'data' will contain the HTML string we returned from the controller
                return data;
            }
        },
        { "data": 'name', "name": 'name' },
        { "data": 'action', "name": 'action', "orderable": false, "searchable": false }
    ],
});

By using the render function, you explicitly instruct DataTables to take whatever string is provided in that cell and render it as raw HTML, resolving the issue where it was treated as plain text. This separation between data fetching (Laravel) and presentation logic (JavaScript/jQuery) is a core principle of building scalable applications, much like the robust architecture promoted by platforms like those found at laravelcompany.com.

Conclusion

The challenge of mixing server-side generated HTML with client-side DataTables rendering often boils down to proper serialization and explicit instruction to the frontend framework. By structuring your Laravel controller to return pre-formatted HTML strings as data, and then using DataTables' render feature in your JavaScript initialization, you gain complete control over how your dynamic table is displayed. This approach ensures that your application remains clean, secure, and highly flexible.