How to persist the state of checkboxes in Yajra Datatables

How to persist the state of checkboxes in Yajra Datatables

JordanPattinsonJordanPattinson Posts: 1Questions: 1Answers: 0

I am using Yajra Datatables, and am generating a table containing a checkbox column, but the state of the "checked" attribute of the checkbox doesn't seem to persist when changing pages. I have tried using the "jquery-datatables-checkboxes" plugin https://www.gyrocode.com/projects/jquery-datatables-checkboxes/examples/data-sources/server-side/

but while that worked, the "id" and "value" properties of my checkbox were not able to be set from my controllers addColumn().

The Controller Code is:

return DataTables::of($data)
->addIndexColumn()
->addColumn('checkbox', function ($item) {
return '<input type="checkbox" id="manual_entry_'.$item->id.'" class="manual_entry_cb" value="'.$item->id.'" />';
})->rawColumns(['action', 'checkbox'])
->make(true);

And my JS:

```$('#manual_select_modal').on('click', function(){
$('.yajra-datatable').DataTable().clear().destroy();

    var table = $('.yajra-datatable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            'url': "{{ route('posts.list') }}",
            'data': {
                post_id: post_id,
            },
        },
        'columnDefs': [
            {
                'targets': 0,
                'checkboxes': {
                    'selectRow': true
                }
            }
        ],
        'select': {
            'style': 'multi'
        },
        columns: [
            {data: 'checkbox', name: 'checkbox', orderable: false, searchable: false},
            {data: 'DT_RowIndex', orderable: false, searchable: false},
            {data: 'post_name', name: 'post_name'},
        ]
    });
});```

Is there a way I can use the addColumn() definition that I have set, with the jquery-datatables-checkboxes plugin?

This discussion has been closed.