How do I access columns.data inside of defaultContent

How do I access columns.data inside of defaultContent

codeMonkeycodeMonkey Posts: 2Questions: 1Answers: 0
edited September 2014 in Free community support

I am trying to set some buttons id's to the last element in each row in data(id). My JS code is

```js
$('#student_dash_tbl_1').dataTable( {
"processing": true,
"ajax": "/datatable_3",
"columns" : [
{'data':'0'},
{'data':'1'},
{'data':'2'},
{
data: null,
className: "center",
sortable: false,
defaultContent: '<a id="rollover_"''+data.id+'this.columns.data.id+' class="btn btn-success" role="button">Rollover</a>'
},
{
data: null,
className: "center",
sortable: false,
defaultContent: '<a class="btn btn-warning" role="button">Withdraw</a>'
},
{
data: null,
className: "center",
sortable: false,
defaultContent: '<a class="btn btn-danger" role="button">Delete</a>'
},
{
data: null,
className: "center",
sortable: false,
defaultContent: '<a class="btn btn-info" role="button">Reset</a>'
},

     ]  
});

and an example of the data being returned to the AJAX is

[23-Sep-2014 15:38:47 UTC] Array
(
[0] => Array
(
[0] => Snow, Jon
[1] => 3
[2] => Archway Cicero
[id] => 3
)

[1] => Array
    (
        [0] => Targaryen, Daenerys
        [1] => 3
        [2] => Archway Scottsdale
        [id] => 2
    )

)

Any help would be appreciated.

This question has an accepted answers - jump to answer

Answers

  • codeMonkeycodeMonkey Posts: 2Questions: 1Answers: 0

    UPDATE:

    Rather then using defaultContent I switched it to render.

    $('#student_dash_tbl_1').dataTable( {
            "processing": true,
            "ajax": "/datatable_3",
            "columns" : [
                 {'data':'0'}, 
                 {'data':'1'},
                 {'data':'2'},
                 {
                     sortable: false,
                     "render": function ( data, type, full, meta ) {
                         var buttonID = "rollover_"+full.id;
                         return '<a id='+buttonID+' class="btn btn-success rolloverBtn" role="button">Rollover</a>';
                     }
                 },
                 {
                     sortable: false,
                     "render": function ( data, type, full, meta ) {
                         var buttonID = "withdraw_"+full.id;
                         return '<a id='+buttonID+' class="btn btn-warning rescindBtn" role="button">Withdraw</a>';
                     }
                 },
                 {
                     sortable: false,
                     "render": function ( data, type, full, meta ) {
                         var buttonID = "delete_"+full.id;
                         return '<a id='+buttonID+' class="btn btn-danger deleteBtn" role="button">Delete</a>';
                     }
                 },
                 {
                     sortable: false,
                     "render": function ( data, type, full, meta ) {
                         var buttonID = "reset_"+full.id;
                         return '<a id='+buttonID+' class="btn btn-info resetBtn" role="button">Reset</a>';
                     }
                 }
    
         ]  
    });
    

    ```

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Answer ✓

    That is the correct thing to do. columns.defaultContent is static and therefore cannot possibly access the data.

    Allan

This discussion has been closed.