.toJQuery() and .to$() is "not a function" for .node() in DT 2.0

.toJQuery() and .to$() is "not a function" for .node() in DT 2.0

Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

I think this is a bug and not intentional, but .to$() and .toJQuery() return "not a function" when used with .node().

Might be an artifact of this from the Release Notes:

Plural methods were available on singular API invocations. For example row().nodes() was available, even although there was no definition for it - it was rows().nodes() leaking through.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    I checked my own code. I use "nodes().to$()" 55 times. But I never use "node().to$". Do you think the singular "node" instead of "nodes" causes the issue?
    "header().to$" also works by the way.

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
    edited February 18

    I'm pretty sure it's the singular case.

    Here's my code (run code associated with closing child rows on all the open ones) that causes the error:

    datatable.rows('.dt-hasChild').every(function() {
        this.node().to$().find('a.details-record').trigger('click')
    })
    

    It causes this error:

    Uncaught TypeError: this.node(...).to$ is not a function

    I've changed it to this, which works:

    datatable.rows('.dt-hasChild').every(function() {
        $(this.node()).find('a.details-record').trigger('click')
    })
    
  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Using to$() never worked with row().node() or any of the other singular API's using .node(). The to$() and toJquery() APIs are Datatable APIs and will only work with objects that contain an instance of the Datatable API. row().node() returns just the tr element where rows().nodes() returns the tr elements along with the Datatables API.

    Use $() to return a jQuery object from row().node(), for example:

    $( table.row( 0 ).node() ).addClass( 'myClass' );
    

    The note you referenced is saying that using something like row().nodes() was never supported nor in the docs. It mistakenly worked and now 2.0 is tightened up so those APIs calls are now invalid.

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    Thanks, Kevin -- you might be right, it's intentional and not a bug and I just happen to actually be taking advantage of a previous "bug" that's now been corrected with 2.0.

    Our posts crossed so you might not have seen mine just a few minutes prior to yours, but I've gone to the $() you mentioned and it works as expected.

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794
    datatable.rows('.dt-hasChild').every(function() {
        this.node().to$().find('a.details-record').trigger('click')
    })
    

    Hmm, not sure how that worked. May have been a bug in the particular version you are using. I tried this code with 1.13.7 and it throws the same error:

    table.rows({search: 'applied'}).every(function() {
        console.log(this.node().to$())
    })
    

    https://live.datatables.net/migoniga/1/edit

    Kevin

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    Hey Kevin!

    I thought that $( ) and to$ were freely interchangeable. I was wrong. Learned something. Thanks.

    Roland

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    @kthorngren -- I had been using 1.13.7.

    Technically this was the code previously, although it shouldn't have made any difference:

    datatable.rows().every(function() {
        if (this.child.isShown()) {
            this.node().to$().find('a.details-record').trigger('click')
        }
    })
    

    When I upgraded to 2.0 I got the "not a function" error, and so I changed it to use $() and then added the `.dt-hasChild' row selector:

    datatable.rows('.dt-hasChild').every(function() {
        $(this.node()).find('a.details-record').trigger('click')
    })
    
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    How interesting - I hadn't realised there was that loop hole! The row().node() method should return an HTML element, which don't have a .to$() method, hence the error. The fact that it worked in 1.x was a bug!

    Allan

Sign In or Register to comment.