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

JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

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

  • RpiechuraRpiechura Posts: 98Questions: 3Answers: 14

    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.

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0
    edited May 2014

    had to post everything in this comment, but I define my aoColumns in a variable like so:

    var columns = [
    /* Icon */ { "mData": "FileLeafRef", "render": function ( data, type, full ) { return '<img width="16" height="16" title="'+data+'" alt="'+data+'" src="/_layouts/15/images/ic'+data.substring(data.lastIndexOf('.')+1,data.length)+'.png" border="0"/>'; } },
    
    ... other columns...
    
    ];
    

    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):

    queryData(context).done(function(contextReturned){
            var oTable = $('#'+contextReturned.tableId).dataTable();
            refreshDataTable(oTable,contextReturned);
        });
    

    My Problem: when contextReturned is an empty array [], datatables still trys to push a row to the table...

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

    oh, and refreshDataTable looks like:

    var refreshDataTable = function (oTable,context){
    oTable.fnClearTable();
    oTable.fnAddData(context.item);
    oTable.fnDraw();
    };

  • RpiechuraRpiechura Posts: 98Questions: 3Answers: 14
    edited May 2014

    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

    aoColumns: If specified, then the length of this array must be equal to the number of columns in the original HTML table. Use 'null' where you wish to use only the default values and automatically detected options.

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

    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

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin

    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

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

    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.

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

    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.

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin

    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 no FileLeafRef 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

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

    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.

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin
    Answer ✓

    The columns.defaultContent option can be used if data is undefined or null.

    Allan

  • JoeIsMyNameJoeIsMyName Posts: 8Questions: 1Answers: 0

    gotcha - thanks!

This discussion has been closed.