Error after upgrading from 1.3.1 to 1.5.1: obj.aData[obj.iDataRow] is undefined

Error after upgrading from 1.3.1 to 1.5.1: obj.aData[obj.iDataRow] is undefined

pmalvespmalves Posts: 18Questions: 0Answers: 0
edited August 2009 in General
First of all congrats for the new features! Seems great and I'm eager to try it, but when I upgraded my program to the new version I found a problem.

The error is:

obj.aData[obj.iDataRow] is undefined


This is the code that's causing the error:

[code]
if(options.colFormats!=undefined){

$.each(options.colFormats,function(i,val){
if (val != null){
dtData.aoColumns[i].fnRender=
function ( obj ) {
return sprintf(val,obj.aData[obj.iDataRow][obj.iDataColumn]);
}
}
})
};
[/code]


Basically, my program allows a sprintf definition of the col format, and I'm accessing the data in that way. This seems to have changed, and before I start debugging I thought of asking if the subject if familiar to anyone.


Thanks

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi pmalves,

    It's quite a big jump from 1.3.1 to 1.5.1 - a lot has changed :-)

    fnRender has changed significantly over the versions - specifically it is not called on every single draw of the table now. Rather it is called only once, when the table is initialised. As such, it is required that fnRender be provided as part of the aoColumns initialisation object: http://datatables.net/usage/columns#fnRender . The information passed to fnRender is the same, but the way it operates is different due to the many internal changes (hopefully it is much better as well given that you don't need to render the data on every draw!).

    Regards,
    Allan
  • pmalvespmalves Posts: 18Questions: 0Answers: 0
    Hum, what impact does this have on DrawCallback?

    One of the things I'm doing is for every page draw (next page, prev page, sort) do some events on it, eg, build sparklines for table row.

    How will this work now?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi pmalves,

    Yup - I think it will impact this as well :-).

    Basically what you want to do with 1.4 and greater is to modify the table as you require using Javascript (for example add your spark lines, add events etc) _before_ you initialise the DataTable. The reason for this is that DataTables 1.4+ is non-destructive with the DOM, where as 1.3 was very destructive.

    These two examples for events might help:
    http://datatables.net/examples/advanced_init/events_pre_init.html
    http://datatables.net/examples/advanced_init/events_post_init.html

    Regards,
    Allan
  • pmalvespmalves Posts: 18Questions: 0Answers: 0
    Hello Allan.

    I'm in the process of upgrading, and it's not much of a pain - the advantages are far greater than the effort.

    I have a question regarding the post events, though.

    If I do:

    [code]

    var dataTable = $("#"+this.htmlObject+'Table').dataTable( dtData );

    $( dataTable.fnGetNodes() ).find("td:eq(1)").each(function(){
    $(this).css("color","red");
    })
    [/code]

    it works as expected; However,

    [code]

    $("#"+this.htmlObject+'Table').dataTable( dtData );

    $( $("#"+this.htmlObject+'Table').fnGetNodes() ).find("td:eq(1)").each(function(){
    $(this).css("color","red");
    })
    [/code]

    says that fnGetNodes is undefined; can I get back my dataTables object if I don't store it in a var?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    HI pmalves,

    The simple answer is no - you need to store the created DataTables object in order to be able to access it's properties.

    The complex answer is - it is possible, but it's a bit hairy. All of the settings objects are stored in $.fn.dataTableSettings, and you can use that in-combination with the raw DataTables object to do what you need, but I'd very much recommend you just store the created object. Just think of it as an object constructor :-)

    Regards,
    Allan
  • vexvex Posts: 30Questions: 0Answers: 0
    If you don't want a global variable you can use the jQuery data store (http://docs.jquery.com/Core/data#name). Something like this should probably work:
    [code]var dataTable = $("#"+this.htmlObject+'Table').dataTable( dtData );
    $("#"+this.htmlObject+'Table').data('ref', dataTable); // Save the reference for later

    ...

    $( $("#"+this.htmlObject+'Table').data('ref').fnGetNodes() ).find("td:eq(1)").each(function(){
    $(this).css("color","red");
    })
    [/code]
  • pmalvespmalves Posts: 18Questions: 0Answers: 0
    Wow, I didn't know that, incredibly useful!

    Thanks
This discussion has been closed.