Call fnGetData from outside (and with no var for the table)

Call fnGetData from outside (and with no var for the table)

RockbRockb Posts: 97Questions: 0Answers: 0
edited April 2014 in TableTools
In my local test-area I use this

[code]$(document).ready( function () {
oTable = $('#example').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sRowSelect": "single"
}
} );[/code]

and something like this

[code]oTable.$('tr').click(function ()[/code]

to capture the ID of the first column. This works fine!

BUT with the tablepress-plugin for WordPress, which is basically using your DataTables, I cannot add (in this case) "oTable = " to the relating table I whish. This is really odd. The only way is to change the "core" of the plugin, but that's nasty. So I have to place the 2nd part somewhere else on the page.

Is there any other way to get the click on the table? Something like:

[code]$('#example tr').click(function ()[/code]

I hope there is an easy and simple way ;-)

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You can simply do: `$('#example').dataTable()` .

    If the table already exits, DataTables will just use that original instance.

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    edited April 2014
    Hello Allan, I'm not sure about your answer... maybe I didn't make myself clear.

    The modified start position is:

    [code]$(document).ready( function () {
    $('#example').dataTable( {
    "sDom": 'T<"clear">lfrtip',
    "oTableTools": {
    "sRowSelect": "single"
    }
    } );[/code]

    ...so without the "oTable = ". If I simple add the

    [code]$('#example').dataTable()[/code]

    infront of the click-function, it doesn't work. Or what do you exactly mean by that suggested code-line? As a single relating it will miss the "tr".
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    [code]
    $('#example tr').click(function () {
    var data = $('#example').dataTable().fnGetData( this );
    ... // do something with the data
    } );
    [/code]

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    Awesome! Thank you very much. :-)
  • RockbRockb Posts: 97Questions: 0Answers: 0
    Allan, is there any big change that came with 1.10 that makes the script above ineffective?

    After changing to the new versions and newest nightly-downloads this...

    [code]$('#example tr').click(function () {
    var data = $('#example').dataTable().fnGetData( this,0 );
    });[/code]

    allows only to click the thead (!) instead of the normal rows - for sure with no result. If change to...

    [code]$('#example tbody').click(function () {
    var data = $('#example').dataTable().fnGetData( this,0 );
    });[/code]

    ...so with tbody, it handles also the click, but the values in 'data' are again empty. What has happened?
  • RockbRockb Posts: 97Questions: 0Answers: 0
    edited May 2014

    changing to

    `$('#example tbody').on('click','tr',function ()

    fixed it. I hope^^

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Sounds like its a jQuery event listener issue more than a DataTables one. You just need the right combination of selectors and handlers - and that one, in your last post, looks perfect to me.

    Allan

This discussion has been closed.