Trying to extract a handler (CSS Selector) for the table from the API

Trying to extract a handler (CSS Selector) for the table from the API

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited October 2015 in Free community support

I have a function that is used to manage multiple DataTables instances, its given just the API as the only parameter, and each of these tables use different CSS selectors to initiate the DataTables instance, so I can't/shouldn't hardcode the selector in the function.

I was wondering if it was possible to get the CSS selector from the API, I tried all of the following...

var $table = $dt.toJQuery();
var $table = $dt.table();
var $table = $dt.table().toJQuery();
var $table = $dt.table().node();
var $table = $dt.table().node().toJQuery();

// Trying to do something like this..
var whatever = $table.find('tbody>tr:eq(0)>td:eq(0)').text();

The only one that doesnt throw an error is

$dt.table().toJQuery();

But it doesnt return the value im looking for, just empty

Heres the example: http://live.datatables.net/wifeqobi/1/

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I was able to get the content I wanted by:

    var $table = $( $dt.table().body() );
    

    example

    But is there a way to get the entire table? I tried

    var $table = $( $dt.table() );
    

    Which didnt seem to work

  • allanallan Posts: 62,316Questions: 1Answers: 10,226 Site admin
    Answer ✓
    var $table = $( $dt.table().node() );
    

    See - table().node().

    There isn't a way to get the original selector - but hopefully if you've got the node, there shouldn't be any need for it.

    One note about the toJQuery() method - it will only work on a collection (i.e. an API instance). So it will work on rows().nodes() for example. row().node() on the other hand returns a node so there is no toJQuery() method.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    Ah, ok, thanks allan!

    There isn't a way to get the original selector - but hopefully if you've got the node, there shouldn't be any need for it.

    Yeah, this is definitely good enough

This discussion has been closed.