简体   繁体   中英

Use row.add for datatable with checkboxes

I have the following code that I am using to populate and generate rows for a checkbox datatable:

    var array = [...];
    $datatable = $('#datatable-checkbox');

    // checkboxes
    $datatable.dataTable({
      'order': [[ 1, 'asc' ]],
      'columnDefs': [
        { orderable: false, targets: [0] }
      ]
    });

    for(int i in array) {
        var name = array[i][0];
        var message = array[i][1];
        var num = array[i][2]; 

        $datatable.DataTable().row.add([
            name,
            message,
            num
        ]).draw( false );
    }

However, the first data field in the row (name) seems to replace the checkboxes as if the rows were empty.

My question is, is there a way to add data to a row while ignoring the first column? Or to add checkboxes back to the first column?

I was able to fix it simply with the following:

$datatable.DataTable().row.add([
    '<input type="checkbox"></input>',
    name,
    message,
    num
]).draw();

Just insert a blank value, when you are using an array to populate the row value, and later fill it using the edit method

$datatable.DataTable().row.add([
            name,
            '',
            ''
        ])

To answer your question Or to add checkboxes back to the first column? . Yes, you can edit a particular row's data after insert using the following:

var myTable = $('#myTable').DataTable();

myTable.row( ':eq(0)' ).edit( {
    name: 'Edit first row name object'
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM