jQuery object, returned by to$() API, is not fully functional

jQuery object, returned by to$() API, is not fully functional

dma_kdma_k Posts: 18Questions: 3Answers: 0
edited July 2014 in Free community support

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

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    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 use node() or data() or whatever to actually do something useful with it.

    Allan

  • dma_kdma_k Posts: 18Questions: 3Answers: 0

    Yes, but then to$() and toJQuery() 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. If to$() would basically do return $(this.node()) then it will work more "reliably".

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    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

  • dma_kdma_k Posts: 18Questions: 3Answers: 0

    In what way does it give an incomplete Jquery object?

    I was referring this construction:

    myTable.row(index).to$().position().top
    

    It does not work, however according to documentation, it should. node() indeed works.

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    I see thanks for the clarification. Using to$() on the rows() 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 the nodes() method.

    I'll add a note to the documentation for to$() to say that it is only really useful with rows().nodes() and its cells and columns counterparts.

    Allan

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    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:

    This is primarily of use when working with the nodes() functions of the API such as rows().nodes(), column().nodes() and `dt-api cells().

    once I redeploy the updated site. Thanks for flagging that up.

    Allan

  • dma_kdma_k Posts: 18Questions: 3Answers: 0
    edited July 2014

    In case to$() does not make sense with integers, perhaps it should be not present in Api.prototype()? But it should be populated to Api.prototype() after node() or nodes() call:

    myTable.row(index).to$() – should cause "function not found"

    myTable.row(index).node().to$() – should work OK

    Alternatively 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):

    function to$() {
      if (typeof this[0] === 'number') {
        return this.length === 1 ? $(this.node()) : $(this.nodes());
      }
    
      return $(this);
    }
    

    Would it be more universal solution?

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    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 with to$() 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

This discussion has been closed.