get data for selected row for TableTool button
get data for selected row for TableTool button
I have some data coming from the Editor that are not displayed in the dataTable. I have a tableTool button that needs to get two of those data elements for the selected row (I only have single-select enabled).
Currently I have two hidden input boxes and am storing the values there. It works for the most part but if the user unselects the row without selecting a new one, the previously selected values still stay in the input boxes. I'm thinking there has to be a better way that will get the selected row data in the button click function.
$('#referralsTable tbody').on( 'click', 'td', function () {
var tr = $(this).closest('tr');
var row = referralsTable.row( tr );
//alert(row.data().Referrals.ReferralID);
$("#SelectedReferralID").val(row.data().Referrals.ReferralID);
$("#SelectedCustomerID").val(row.data().Referrals.CustomerID);
} );
var referralsTableTools = new $.fn.dataTable.TableTools( referralsTable, {
sRowSelect: "os",
aButtons: [
{ sExtends: "editor_create", sButtonText: "New Referral", editor: referralsEditor },
{ sExtends: "editor_remove", editor: referralsEditor },
{
sExtends: 'select_single',
sButtonClass: 'marginLeft',
sButtonText: 'Make Appointment',
fnClick: function () {
if ( $("#SelectedReferralID").val() !='') {
//if selected referral is already a customer, then just open customerDetail
//otherwise, insert Customer record, then open customerDetail
if ( $("#SelectedCustomerID").val() != 0) {
window.location.href = ('customerDetail.php?id='+ $("#SelectedCustomerID").val());
} else {
$.ajax({
type: "POST",
url: "lib/makeCustomer.php",
data: "id="+$("#SelectedReferralID").val(),
cache: false,
success: function(result){
window.location.href = ('customerDetail.php?id='+ result);
}
});
}
} else {
alert('You must select someone first');
}
}
}
]
} );
$( referralsTableTools.fnContainer() ).insertBefore( '#referralsTable_filter' );
This question has an accepted answers - jump to answer
Answers
Anyone?? :)
In the TableTools
fnClick
you should be able to use thefnGetSelectedData
API method. Simplythis.fnGetSelectedData()
will give you the data for the selected rows in an array.Allan
I try this and get an error: Uncaught TypeError: Cannot read property 'CustomerID' of undefined
this code works:
but not this:
This returns an array. So you would need to use
aData[0]...
to get the data for the first row.Allan
worked perfectly..
Thank you for your assistance.