fnGetSelectedData Example

fnGetSelectedData Example

jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
edited March 2014 in TableTools
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]

Replies

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Which version of TableTools are you using? Try using `$.fn.dataTable.TableTools` rather than just `TableTools` .

    Allan
  • jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
    That did the trick...

    Thanks...

    jdadwilson
  • jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
    Yes, indeed that did the trick... but as a newbie to javascript and very newbie to dataTables.tableTools I would most appreciate an example of how to access the aData array. I would assume for a multi selection I would have to loop through each of the array elements to get all of the information I need.

    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
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    > 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.

    Use do a `join` ? `aData.join(',')` would give you a comma separated list of the values.

    Allan
  • jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
    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
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    > join[',']

    `join` isn't an object - its a function. You want to call it...

    [code]
    join(',')
    [/code]
  • jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
    Well color me a newbie... that solved the problem with sInvoices being undefined BUT why is iNum 0?

    jdadwilson
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    No idea - the array must be empty I guess. I'd need a link to a test case to understand what is actually happening.

    Allan
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Oh - actually - it will be because you don't have any rows selected...
  • jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
    I may be a newbie, but I'm not that new ;>)... I'll set you up a test case on the site like I did before. Will send you the details when ready.

    Thanks so much for your assistance and a great product.
  • jdadwilsonjdadwilson Posts: 80Questions: 14Answers: 1
    Allan,

    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.
This discussion has been closed.