mRender giving an initial error

mRender giving an initial error

leeguthleeguth Posts: 19Questions: 1Answers: 0
edited March 2014 in DataTables 1.9
I am using the mRender to display a count (not coming back in my ajax call) and a button (using a value in my data that I am not displaying). When i initially load the page, I get the following error: "DataTables warning (table id = 'example'): Requested unknown parameter '1' from the data source for row0."

After I get the error though, the page does display correctly. How do I get rid of the error?

Here is my ajax call:
success:function(data){
oTable = $('#example').dataTable( {
"bProcessing": true,
"aaData": data,
"aoColumns": [
{
"mData": null,
"sClass": "control center",
"sDefaultContent": ''
},
{ "mRender": function(data,type, row) {
return getProjectCount(row.prg_guid);
}
},
{ "mData": "prg_name" },
{ "mData": "market" },
{ "mData": "beta_date" },
{ "mData": "rtc_date" },
{ "mRender": function(data, type, row) {
if (row.prj_guid != ""){
return "Open in cPro";
} else
return "No cPro";
}
}

]
} );
}

I am hopefully using the mRender properly.....

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Set the mData option for your second column to `null` , just like you have for the first column. That will deal with it.

    Allan
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    Thanks that worked!!! I didn't realize I needed both :)
  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    mData will default to the column index if it isn't given - which is why you were getting an error about the data at index `1` missing.

    Allan
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    Further question on mRender.....do you know if within the function for setting the value, I can make another ajax call? I am trying it but keep getting an error in dataTable - I get the same error I had above, plus I get a similar one for 'row 2' and I get null for the datatable column where I do seem to be getting something back from the ajax call.

    for example: (I blanked on most of the url below because it was company specific and this is only a small portion of the code) The value of 'burl' is correct because i can take out the ajax call and output the value, just seems when i do the ajax call that it is causing the error.

    { "mData": null,
    "mRender": function(data,display, row) {
    var burl = "https://...../beta/"+row.prg_guid;
    $.ajax({
    url:burl,
    success:function(data2){
    if (data2 == "green"){
    return "";
    }
    }
    }
    ]
    } );
    }
    } );
    }

    Would you have any suggestions of a different way to do this, if I can't have the ajax call in the mRender?
  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    > I can make another ajax call?

    Yes, but remember what the first letter of Ajax stands for: _asynchronous_. mRender is executed synchronously, and expects the data back as such. But your Ajax is completing after that. You could make it Sjax, but that will _kill_ performance.

    You'd be much better trying to get all the data you need into the data structure.

    Allan
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    hmmmm....ok. thanks!
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    Allan,
    I've been wondering on my issue of needing to call a function (that requires an ajx call) to fill in a value....Could I give the cell a sName and a value of null. Then use a .each call on the success of the ajax call to loop through each table row and update the particular cell (column) with the value?

    I saw the documentation for fnUpdate and wasn't sure if that could be what I could use. I couldn't tell by the example though if I could use it that way or exactly how I would go about it.

    You documentation example shows the code below....and seems to allow updating a cell at a time.
    $(document).ready(function() {
    var oTable = $('#example').dataTable();
    oTable.fnUpdate( 'Example update', 0, 0 ); // Single cell
    oTable.fnUpdate( ['a', 'b', 'c', 'd', 'e'], 1 ); // Row
    } );

    Can I call the single cell example in conjunction with the each? If so, the two zeroes shown....would I need to change that to 'row' reference somehow? And can I reference the cell by the name I give it or do I need to provide a number there?
  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Yes, that would be one way of doing it (use an empty string, not null though).

    You might find it easier to understand using the new API in DataTables 1.10 - http://next.datatables.net/reference/api/cell().data() (I need to update the forum linking - you need to add the `()` at the end manually - sorry.

    Allan
This discussion has been closed.