Do I have to call dataTable() on an enhanced table in order to run fnGetSelected()?

Do I have to call dataTable() on an enhanced table in order to run fnGetSelected()?

frequentfrequent Posts: 23Questions: 0Answers: 0
edited July 2012 in General
I'm trying to set up a generic call for fnGetSelected() to select highlighted rows, so I can run it on multiple dataTables instances

The triggering function is fired in my main.js controller with the dataTables config (and fnGetSelected) in a seperate app.js file. I have a global object for "external functions", which I'm adding fnGetSelected to make it callable "from outside"

I have finally got it to work by doing this:

[code]var selected = $.pluginFunctionCaller.getKeys( $('.tbl_orders') );[/code]

and in my dataTables.js utils

[code]fnGetSelected = function( table ) {
var aTrs = table.dataTable().fnGetNodes(),
...
[/code]

I saw in the example that fnGetSelected/fnGetNodes require calling dataTable() on the table, but I did not know I would also have to do this on the already enhanced table.

If I console the table only and the table.dataTable(), they both seem to be identical. However trying to call fnGetNodes() without a prior call to dataTable() just returns ".... is not a function".

Although I don't know of the "payload" of running dataTable() on an already enhanced table, I was wondering if there is a way to run the functions without the additional call?

Let me know if you need the full table initialization.

Thanks your support!

Replies

  • frequentfrequent Posts: 23Questions: 0Answers: 0
    Ups. Forgot a line of code:

    [code]
    // add to global functions
    $.pluginFunctionCaller.getKeys = fnGetSelected;
    [/code]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    What fnGetSelected are we talking about here? The one in TableTools or from my basic row selection example: http://datatables.net/release-datatables/examples/api/select_row.html ? Or do you have it as an API method somewhere?

    Allan
  • frequentfrequent Posts: 23Questions: 0Answers: 0
    I was wondering where I had the function from (it was from an older dataTables script I did. I guess it was the basic row selection example. Here is my full function:

    [code]
    fnGetSelected = function( table ) {
    var aTrs = table.dataTable().fnGetNodes(),
    bestellkeys = "", ilns = "", newKey, newiln;

    for ( var i = 0; i<=aTrs.length-1 ; i++ ) {
    thisRow = $(aTrs[i]);
    if ( thisRow.find(".selector").is(':checked') == true ) {

    newKey = thisRow.find('.loadDetails').jqmData('key');
    newIln = thisRow.find('.loadDetails').jqmData('iln');
    bestellkeys = bestellkeys == "" ? newKey : bestellkeys+","+newKey;
    ilns = ilns == "" ? newIln : ilns+","+newIln;
    }
    }
    return [bestellkeys,ilns]
    }
    [/code]

    Actually the culprit is fnGetNodes(), which I can't get to fire without supplying the already-enhanced table.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    With that function it looks like you need to pass in a jQuery object with the table node selected. It might be easier if you modified the first line to be:

    [code]
    var aTrs = $(table).dataTable().fnGetNodes()
    [/code]

    if it is already a jQuery object, the jQuery will carry on anyway. If its a selector ('#myTable' for example) then jQuery will do its normal selector thing on it. And if you give it a node, that will also be picked up by jQuery.

    Allan
  • frequentfrequent Posts: 23Questions: 0Answers: 0
    Agree, but I'm passing in $('.tbl_orders') as parameter, so it's already an object. I was more wondering about the .dataTabels() call as I the table already is enhanced by dataTables.

    That's why I consoled both:

    $('.tbl_orders')
    $('.tbl_orders').dataTable()

    Which on Firebug are the same, but omitting the call to dataTable() causes the function to fail. That was my question - why the additional call to dataTabel() is required?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Oh I see - sorry for the misunderstanding. Because you need to work with a DataTables 'object' and the dataTable() call does that. $(...) just gives you a jQuery object, so you can't go: $('#myReadyTable').fnGetData(), since fnGetData is not a jQuery method - it is a DataTables method.

    Allan
  • frequentfrequent Posts: 23Questions: 0Answers: 0
    Ok. Makes sense :-)
    Thanks for clarification.
This discussion has been closed.