Obtain specific column value of current row
Obtain specific column value of current row
Hi,
I am pulling data from my MySQL database via AJAX and then use DataTables to generate dynamic buttons in an additional column for each row. This column provides Edit / Delete buttons for each row, which in turn bring up a modal where the action is being taken care of.
The problem now is that I need the buttons to have a data-id attribute where id refers to the ID
column of the row. I have tried many ways to get this done, but so far I am only able to extract the full row data as a comma separated string, which is not really how I want to do this.
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
$('#auto_cc_table').DataTable( {
// load table data via AJAX
"processing": true,
"serverSide": true,
"ajax":{
url: "../../plugins/MySQL/ajax_action.php", // json datasource
data: { action:"view_auto_cc_email_AJAX" },
type: "post", // connection method (default: GET)
},
"columns": [
{ "aaData": "ID" },
{ "aaData": "Holidex" },
{ "aaData": "First" },
{ "aaData": "Last" },
{ "aaData": "Email" },
{ "aaData": null },
],
columnDefs: [{
className: "text-right", "targets": [1],
className: "text-nowrap", "targets": [3],
// puts a button in the last column
targets: [-1], render: function (data, type, row, meta) {
// look at your console and make sure there is a value here.
//console.log(data); // error - undefinded in console
console.log(row); // ok
console.log(row.ID); // error - undefined in console
//console.log(row.Holidex); // error - undefined in console
//console.log(row.First); // error - undefined in console
//console.log(row.Last); // error - undefined in console
//console.log(row.Email); // error - undefined in console
return '<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#EditEmailModal" data-keyboard="true" data-id="' + row.ID +'"><i class="fa fa-edit"></i></button>'
+'<button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#EditEmailModal" data-keyboard="true" data-id="' + row.ID +'"><i class="fa fa-times"></i></button>'
}
}],
dom: 'Bfrtip',
stateSave: true,
buttons: [
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5',
{
extend: 'print',
message: '(c) http://www.website.com/'
},
{
extend: 'collection',
text: 'Others',
buttons: [
{
text: 'Toggle button',
action: function ( e, dt, node, config ) {
dt.column( -6 ).visible( ! dt.column( -6 ).visible() );
}
},
'colvis',
'columnsToggle',
]
},
],
"pagingType": "full_numbers",
"pageLength": 25,
"lengthChange": true,
"searching": true,
"ordering": false,
"order": [[ 1, "asc" ], [ 2, "asc" ]],
"info": true,
"autoWidth": true
})
});
</script>
I am logging the output for data, row, row.ID, etc into the console, where all I get is an error message saying 'undefined'.
I think my question is this: How can I extract the ID
value of the specific row and attach it to the button as a data-id="xx" element?
And on a sidenote, I am unable to get the column text aligned to the right / left / nowrap, even though the columnDefs seem ok.
Any help would be appreciated.
Thank you.
This question has an accepted answers - jump to answer
Answers
Looks like your code should work. Does your table populate properly if you remove the last column?
What is the output from this?
You are using
aaData
, which is a legacy command, instead ofdata
. Is there a reason? Not sure why this command outputsundefined
withaaData
but should work if you usedata
.This is typically due to not having
width="100%"
configured in the html of yourtable
definition.Kevin
Thank you for your response, appreciate you taking the time to look at this.
console.log(row); outputs the following, which is the full row content of the current row:
["7", "ADMIN", "Jane", "Doe", "jane.doe@gmail.com"]
as for
aaData
, no I have no particular reason, I just reused portion of some code that I had put together previously and never had an issue with. I am changing to `data to see how it goes.I will have a look at the
width="100%"
as you mentioned. Is this applied to thetable
element or the thetd
tr
?Thank you
Just changed from
aaData
todata
and the whole script stopped working. Not sure why, but it seems like DataTables requires me to define the query data asaaData
during the SQL interaction before sending it back to my page for display. Below my MySQL interaction:Alright, found the problem.
Turns out that the
row
variable was an array but needed to be defined as an object in order to address the ID column directly. Code below is updated and working.