Empty JSON array passed to aoColumns causing issues after 1.9 to 1.10 migration
Empty JSON array passed to aoColumns causing issues after 1.9 to 1.10 migration
Im having issues with passing an empty json array to aoColumns... DataTables is trying to display this data. In most cases, it will just render a series of "null" for the first (and only) table row. In other cases, like when im trying to use - "render": function ( data, type, full ) {...} - I may get errors because data is empty.
This is happening on a fresh 1.9.4 to 1.10 migration. Obviously, everything was working fine in 1.9.4... Am I missing something? I know I could add come kind of check to see if the json array is empty, but this was previously unnecessary.
Thanks,
Joe
This question has an accepted answers - jump to answer
Answers
Can you give an example of the code you're trying to use so that I can mess around with it? Hard to say what the issue may be otherwise.
had to post everything in this comment, but I define my aoColumns in a variable like so:
THEN, I call a function to create the datatable with that aoColumns data. something like...
formTableDataTable(context,columns);
then I query for data and I expect a JSON response (using the sharepoint 2013 REST API):
My Problem: when contextReturned is an empty array [], datatables still trys to push a row to the table...
oh, and refreshDataTable looks like:
var refreshDataTable = function (oTable,context){
oTable.fnClearTable();
oTable.fnAddData(context.item);
oTable.fnDraw();
};
My best guess is that when you're passing an empty json array aoColumns is freaking out because it requires that you give it the exact number of columns that you plan on using. IE if you pass it an empty array it thinks you want a table with 0 columns. Instead of an empty array the columns should be null for each column you want.
From the legacy documentation
well... im not sure I understand exactly what you mean, but this works fine in 1.9.4, so something in 1.10 changed which is causing the issue... ill see if I can narrow it down a bit more
I think you are going to have to link us to a working test case so it can be properly debugged and the error tracked down. I don't immediately see the issue int he above code.
Allan
Here is a test case: http://jsfiddle.net/q72PG/17/
I have commented out some JSON test data, and in its place I put an empty array. DataTables attempts to process the empty array. In this case, the first error I get is with the "render" function since it is trying to perform a render operation on an empty data set.
Also... I did some more testing and if I remove the render functions from the column processing, my table does get a single row of "null" columns. I also noticed that I didn't necessarily get my first error unless I already had the debugger open. Finally, the last issue I noticed, which may or may not be related to datatables, is that when I call my "refreshDataTable" function, I get an object expected error if I define the function:
var refreshDataTable = function (){...};
as opposed to:
function refreshDataTable(){...}
but this last one only happens in jsFiddle, so it may just be related to that.
I'm a bit confused - you are getting an error when you add an empty array to the table. I don't see why that would be wrong? The specific error is that your
render
function is trying to get a part of an undefined string. There is noFileLeafRef
property of the empty array, so it throws an error.I'd be surprised if that worked in 1.9, but if it did, it was an error - that should not work. If you want to add data, you need to add the properties you've told DataTables to expect.
Allan
I understand that, but given how this works in 1.9, I assumed datatables had a built in determination of whether or not the dataset was empty. I guess you're saying it doesn't inherently check if the dataset is empty? In that case, 'll add empty dataset checks in my code then.
The
columns.defaultContent
option can be used if data isundefined
ornull
.Allan
gotcha - thanks!