jQuery object, returned by to$() API, is not fully functional
jQuery object, returned by to$() API, is not fully functional
I assume that myTable.row(index).to$()
returns <tr>
node wrapped into jQuery object. This is partially true, as one can call myTable.row(index).to$().addClass('selected')
, but that fails in following scenario my top = myTable.row(index).to$().position().top
.
In jQuery code of position()
function
position: function() {
elem = this[ 0 ];
if ( jQuery.css( elem, "position" ) === "fixed" ) { ... }
}
the variable elem
is initialized with array [ 0 ]
which is not DOM element at all. The same affects toJQuery()
which is alias for to$()
.
Workaround: use row().node()
$(myTable.row(index).node()).position()
Replies
The workaround is actually the intended usage. The
row()
method basically populates the data set in the index with row indexes - you need to then usenode()
ordata()
or whatever to actually do something useful with it.Allan
Yes, but then
to$()
andtoJQuery()
are misleading, because one would expect the fully-functional object, and it's only provide a subset of functions. At least that should be mentioned in documentation (and documentation says "will create a jQuery object"). Well, anyway I personally was trapped. Ifto$()
would basically doreturn $(this.node())
then it will work more "reliably".In what way does it give an incomplete Jquery object? The node() method returns a node, not a DataTables API object so it shouldn't have a to$ method. The nodes() methods however dp and it should work there ( lon phone atm, sorry, can't double check!)
Allan
I was referring this construction:
It does not work, however according to documentation, it should.
node()
indeed works.I see thanks for the clarification. Using
to$()
on therows()
method will give you a jQuery object, but one populated by integers (the row indexes) rather than nodes (which is probably fairly pointless!) - if you want the nodes you need to use thenodes()
method.I'll add a note to the documentation for
to$()
to say that it is only really useful withrows().nodes()
and its cells and columns counterparts.Allan
It did actually have a note to that effect already, although was missing the cells method and a typo at the start of the sentence. It will now say:
once I redeploy the updated site. Thanks for flagging that up.
Allan
In case
to$()
does not make sense with integers, perhaps it should be not present inApi.prototype()
? But it should be populated toApi.prototype()
afternode()
ornodes()
call:myTable.row(index).to$()
– should cause "function not found"myTable.row(index).node().to$()
– should work OKAlternatively
to$()
could check if current context is node (or nodes) and convert the context if necessary (I provide just an idea, perhaps it won't work):Would it be more universal solution?
Yes, that's a good idea. There are a few other types as well that I thought of -
tables().nodes()
,columns().header()
which can also be used withto$()
so perhaps it should throw an error if it finds something other than an empty array or an array of nodes in the current context...I'll have a think about it!
Allan