lundi 23 décembre 2019

Adding check boxes to datatable where data is pulled from an api

I have my data as json key value pairs in an array.

$(document).ready(function () {
        var table = $("#main").DataTable({
            columns: [{
                    "data": "ProductNumber",
                    "title": "ProductNumber"
                },
                {
                    "data": "ReceivingDate",
                    "title": "ReceivingDate"
                }]

I want to add checkboxes so that users can select some rows.

columnDefs: [{
                targets: 0,
                searchable: false,
                orderable: false,
                className: 'dt-body-center',
                render: function (data, type, full, meta) {
                    return '<input type="checkbox" name="id[]" value="' +
                        $('<div/>').text(data).html() + '">';
                }
            }]

I found the columnDef example from the DataTable website. The issue is, it is told to render this checkbox on column 0 which replaces my productnumber column. And I can't add an empty column as my json data has the exact number of data as the columns have. Are there any other ways I can achieve this?




Aucun commentaire:

Enregistrer un commentaire