Any way to get table nodes or table instance within mData / mRender?

Any way to get table nodes or table instance within mData / mRender?

abilekabilek Posts: 2Questions: 0Answers: 0
edited October 2012 in DataTables 1.9
Hi there

I am trying to update/convert my old fnRender functions to mData or mRender and I'm running into a problem. I have attached data to the table itself that I need to use in one of my rendering functions. My current function accesses the table node thusly

[code]
"fnRender": function( oCell ) {
var actionData = oCell.aData[ oCell.iDataColumn ],
oActions = $(oCell.oSettings.nTable).data("actions");

etc...
}
[/code]

There seems to be no way to access the current table node or current instance using mData or mRender. Because I am trying to make this a generic function that I can use on any datatable, and because I might have multiple datatables on a single page, I cannot do what many of your examples suggest, which is to assign the datatables instance to a global variable and use that to access the table node (and thus the data I need).

Similarly, because there may be more than one datatable on a page, I cannot use $.fn.dataTable.fnTables to access the current table node.

Do you have any suggestions as to how I can overcome this problem? It seems a real oversight to me that there is no way to access information about the current settings or instance within mData or mRender.

Thanks very much for any insight you can provide.

Kind regards
anne

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited October 2012
    The fnCreatedCell callback is the way to do this now - it will be called when the cell is ready / created.

    Allan
  • timtuckertimtucker Posts: 48Questions: 0Answers: 0
    edited October 2012
    We're using a variation on the global variable model -- we're using independent modules for each of the tables we use. We use requirejs, but it could also be done more simply following the patterns described here:
    http://briancray.com/posts/javascript-module-pattern

    That lets us keep an instance of the table within the scope of the module, so we can refer to it anywhere inside that we want.

    Another alternative that we use variations of is to have a global function that return a function, i.e. your shared function could be:
    [code]
    var dependsOnTable = function(myTable)
    {
    return function(data, type, value)
    {
    var oActions = myTable.data("actions");

    // Do something to return a value
    };
    };
    [/code]

    Which would let you do something like the following:
    [code]
    var myTable1 = $("#firstTable");
    myTable1.dataTable(
    {
    aoColumnDefs : [
    mData : dependsOnTable(myTable1)
    ]
    }
    );

    var myTable2 = $("#secondTable");
    myTable2.dataTable(
    {
    aoColumnDefs : [
    mData : dependsOnTable(myTable2)
    ]
    }
    );
    [/code]
This discussion has been closed.