Referencing column elements

Referencing column elements

kgprasadkgprasad Posts: 6Questions: 4Answers: 0
edited March 2017 in Free community support

Hello, I would like to set the button id to the pkey of the row, in a dynamically
created button (based on the 'status' column, see below). I am not able to get
the specific "id" I need :

$('#lidarSaas').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'columnsToggle'
    ],
    //"processing": true,
    "ajax": {
        "url": "http://127.0.0.1:8000/lidarSaas/uploads",
        "dataType" :"json",
        "dataSrc": "data"
    },
    columns: [
    {   "data": "id"},  // The id I want in the code below ..
    {
        "data": "status",
        render: function(data, type, row) {
        // Dynamically add buttons indicating the rowId in the DB for easy
        // reference when the function needs to be executed with that rowId
        // on the server side
        if (data == 'Processing') {
           return '<button type="button" id=table.row(this).id onclick="handleStop(table.row(this).id)">Stop</button>';
        }

        return data;
    }
 ..
}

function handleStop(id) {
  alert(id);
  // do stuf ..
  ..
}

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Try replacing table.row(this).id with row.id.

    Kevin

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin

    Yup. Just to refine what Kevin says a bit, replace table.row(this).id with

    handleStop('+row.id+')
    

    Also:

    id=table.row(this).id

    While it won't work, the intention appears to be to assign the row id to the button, which doesn't seem like a good idea to me, since you might have two elements with the same id (the row and the button).

    Two other points:

    1. table is not defined anywhere
    2. Don't use DOM0 event handlers. Use jQuery delegated events.

    Allan

This discussion has been closed.