How to set up the row ID properly

How to set up the row ID properly

andras2andras2 Posts: 31Questions: 12Answers: 0

Hi there, I would need more help how to use and setup the rowID as I am new on the web. I am not sure what staffID means in the description (whether it is a custom or built-in property?). I am also not sure if I need to set up a column structure and how to be in line with the IDs. Could you help me?

<script>    
$(document).ready(function() {  
    var table = $('#example').DataTable( {
        select: true,
        stateSave: true,
        ajax: '/api/staff',
        rowId: 'staffId',
        columnDefs: [ {
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        } ],
        select: {
            style:    'os',
            selector: 'td:first-child'
        },
        order: [[ 1, 'asc' ]],
        "ajax": "data.txt"
    } );
 
    $("#delBtn").click(function(){
            var id = table.rows( { selected: true } ).ids().toArray();
            alert( 'Clicked row id '+id);
        }); 
 
    $('#example').on( 'click', 'tr', function () {
    var id = table.row( this ).id();
     alert( 'Clicked row id '+id );
    } );

     setInterval( function () {
        var rows = table.rows({selected: true});
        table.ajax.reload( 
            function () {rows.select()}, false );}, 
            1000 );
});
</script>

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735
    edited March 2022

    Since you don't have columns.data your row data is arrays instead of objects. See the data docs for details. I'm not sure if rowId will work with array based data which uses indexes (numbers) to access the row data instead of a string which is used for objects. The rowID docs state it takes a string parameter.

    The rowId docs have rowId: 'staffId' in the example. The staffId is one of the object keys. You would use a key that has unique data for the column. It is recommended to use object instead of arrays for the row data.

    Kevin

Sign In or Register to comment.