Returning a jQuery element from mRender 'display' mode?

Returning a jQuery element from mRender 'display' mode?

benallfreebenallfree Posts: 7Questions: 0Answers: 0
edited August 2012 in DataTables 1.9
Hi, I love the mRender feature.

It seems that it would be convenient if a jQuery element could be returned. That way you can dynamically create an element and load it with click() handlers and data().

I don't know if this is the right place to discuss mods, but perhaps I can get some feedback on this change. I'd fork the project and submit a pull request, but I don't fully understand the packaging system. The main zip download is different from cloning the repo.

Was:
[code]
nTd.innerHTML = (typeof oCol.fnRender === 'function' && (!oCol.bUseRendered || oCol.mData === null)) ?
_fnRender( oSettings, iRow, i ) :
_fnGetCellData( oSettings, iRow, i, 'display' );
[/code]

Changed to:

[code]
var display_obj = (typeof oCol.fnRender === 'function' && (!oCol.bUseRendered || oCol.mData === null)) ?
_fnRender( oSettings, iRow, i ) :
_fnGetCellData( oSettings, iRow, i, 'display' );
if(typeof(display_obj)=="object")
{
$(nTd).append(display_obj);
} else {
nTd.innerHTML = display_obj;
}
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    That's a really interesting idea - I do like it, but I'm not sure that it will work quite as expected in some cases. mRender for display can be called multiple times (for example to get the data to actually display, then on fnCreatedCell etc) - so if you want to work with the same node, you would always need to return the same object. So that is possible, but I would say non-obvious, and therefore error prone.

    The fnCreatedCell and fnCreatedRow callbacks are designed for this kind of thing, and also take into account deferred rendering etc. When those methods are called, you know that the cell is ready for events.

    However, this is something that could use further refinement, so further thoughts are very welcome on this.

    Regards,
    Allan
  • benallfreebenallfree Posts: 7Questions: 0Answers: 0
    Can fnCreatedCell be supplied in aoColumns next to mRender and mData in the setup array? If so, I'd say that's an equivalent solution. mRender can return HTML and then you can grab the jQuery object in fnCreatedCell and configure it.

    -Ben
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > Can fnCreatedCell be supplied in aoColumns next to mRender and mData in the setup array?

    Yes - exactly that. fnCreatedCell is column based, while fnCreatedRow is a 'top level' callback.

    Allan
  • radisbradisb Posts: 6Questions: 2Answers: 0
    fnCreatedCell/Row are not called on fnUpdate so any jQuery configuration inside them that depends on the underlying data will not work when you fnUpdate the row/cell....
This discussion has been closed.