fnGetSelectedData Example
fnGetSelectedData Example
jdadwilson
Posts: 127Questions: 30Answers: 1
I am attempting to implement a dataTable with tableTools into one of my app modules. The dataTables works great as does the initial function of the tableTools (selecting rows etc.).
What I need to do is use my 'Print' button click event and then determine which rows have been selected so that I can use the information to process further information.
I used the sample code from the fnGetSelectedData API but I get a TableTools undefined error. Is the following maybe part of the issue...
My table is loaded as part of the which is thus outside the jquery $(function() { }. Further, the code for the instance is within the print click handler.
Here is my code...
[code]
function loadTable() {
oTable = $('#invoices').dataTable( {
'aoColumns': [
{ 'mDataProp': 'inv_Num', 'sClass': 'udatal' },
{ 'mDataProp': 'inv_Project', 'sClass': 'udatal' },
{ 'mDataProp': 'inv_Date', 'sClass': 'udatac' },
{ 'mDataProp': 'inv_Status', 'sClass': 'udatac' },
{ 'mDataProp': 'inv_Posted', 'sClass': 'udatac' }
], // End of aoColumns
'bAutoWidth': false,
'bDestroy': true,
'bFilter': false,
'bInfo': false,
'bLengthChange': false,
'bPaginate': true,
'bProcessing': false,
'bServerSide': false,
'bSort': false,
'bVisible': true,
'iDisplayLength': 12,
'oLanguage': { 'oPaginate': { 'sPrevious': 'Prev' } },
'oTableTools': {
'sRowSelect': 'multi',
'aButtons': [ 'select_all', 'select_none' ]
},
'sAjaxSource': 'files_json/ajax_Invoices.php' + sParams,
'sDom': 'T<"clear">lfrtip',
'sPaginationType': 'full_numbers',
'fnInitComplete': function(oSettings, json) {
if ( this.fnGetData().length === 0 ) {
$('#noRecords').show();
} else {
$('#invTable').show();
}
} // End of fnInitComplete
}); // End of dataTable
} // End of loadTable
// Initiate table on load
function initForm() {
// code to initialize form elements goes here
loadTable();
} // End of init function
$(function() {
// ************************************************************************************************* Event handlers
// Print event handler
$('#buttonPrt').click(function() {
var oTT = TableTools.fnGetInstance( 'invoices' ); // throws undefined error in console.log
var aData = oTT.fnGetSelectedData();
// Build parameter string
//popupWindow('files_print/pdf_Invoices.php' + Params,'1100','600')
}); // End of buttonPrt function
});
[/code]
What I need to do is use my 'Print' button click event and then determine which rows have been selected so that I can use the information to process further information.
I used the sample code from the fnGetSelectedData API but I get a TableTools undefined error. Is the following maybe part of the issue...
My table is loaded as part of the which is thus outside the jquery $(function() { }. Further, the code for the instance is within the print click handler.
Here is my code...
[code]
function loadTable() {
oTable = $('#invoices').dataTable( {
'aoColumns': [
{ 'mDataProp': 'inv_Num', 'sClass': 'udatal' },
{ 'mDataProp': 'inv_Project', 'sClass': 'udatal' },
{ 'mDataProp': 'inv_Date', 'sClass': 'udatac' },
{ 'mDataProp': 'inv_Status', 'sClass': 'udatac' },
{ 'mDataProp': 'inv_Posted', 'sClass': 'udatac' }
], // End of aoColumns
'bAutoWidth': false,
'bDestroy': true,
'bFilter': false,
'bInfo': false,
'bLengthChange': false,
'bPaginate': true,
'bProcessing': false,
'bServerSide': false,
'bSort': false,
'bVisible': true,
'iDisplayLength': 12,
'oLanguage': { 'oPaginate': { 'sPrevious': 'Prev' } },
'oTableTools': {
'sRowSelect': 'multi',
'aButtons': [ 'select_all', 'select_none' ]
},
'sAjaxSource': 'files_json/ajax_Invoices.php' + sParams,
'sDom': 'T<"clear">lfrtip',
'sPaginationType': 'full_numbers',
'fnInitComplete': function(oSettings, json) {
if ( this.fnGetData().length === 0 ) {
$('#noRecords').show();
} else {
$('#invTable').show();
}
} // End of fnInitComplete
}); // End of dataTable
} // End of loadTable
// Initiate table on load
function initForm() {
// code to initialize form elements goes here
loadTable();
} // End of init function
$(function() {
// ************************************************************************************************* Event handlers
// Print event handler
$('#buttonPrt').click(function() {
var oTT = TableTools.fnGetInstance( 'invoices' ); // throws undefined error in console.log
var aData = oTT.fnGetSelectedData();
// Build parameter string
//popupWindow('files_print/pdf_Invoices.php' + Params,'1100','600')
}); // End of buttonPrt function
});
[/code]
This discussion has been closed.
Replies
Allan
Thanks...
jdadwilson
My goal is to build a string of the indexes contained in column 1 (inv_Num) to pass via a $_GET to a pdf print routine.
TIA in advance for any assistance
jdadwilson
Use do a `join` ? `aData.join(',')` would give you a comma separated list of the values.
Allan
I must be doing something wrong (not unusual).
[code]
$('#buttonPrt').click(function() {
var oTT = $.fn.dataTable.TableTools.fnGetInstance( 'invoices' );
var aData = oTT.fnGetSelectedData();
var iNum = aData.length;
console.log('iNum: ' + iNum);
var sInvoices = aData.join[','];
console.log('sInvoices: ' + sInvoices);
}); // End of buttonPrt function
[/code]
Running the above code gives a value of iNum of 0 and sInvoices as undefined.
What am I missing?
jdadwilson
`join` isn't an object - its a function. You want to call it...
[code]
join(',')
[/code]
jdadwilson
Allan
Thanks so much for your assistance and a great product.
Thanks a bunch for the help. I got it figured out and it now works as it should. Now on to figuring out the fnClearTable and fnAddData stuff.