Added data does not match known number of columns

Added data does not match known number of columns

DFDF Posts: 13Questions: 0Answers: 0
edited August 2010 in General
Great job on dataTables!

I am having trouble loading data into a single column table.

The table structure is:
[code]

Replies

  • ruzzruzz Posts: 49Questions: 0Answers: 0
    Not sure if this will help but...

    Firstly, your HTML has a stray double-quote in it - I assume it's a typo and your actual HTML code is fine.

    Next, if you're supposed to be receiving json and via ajax, (I suspect that's the case?) then the contenttype should be correct - plain text is not correct (though some systems may not complain).

    Finally, the result you're getting back does not look like valid json - confirmed here: jsonlint.com

    HTH, ...

    ruzz
  • DFDF Posts: 13Questions: 0Answers: 0
    Thanks, ruzz.

    The extra " was just a typo.

    I'm not using json. The Ajax call datatype is text and text it gets.

    If I can't figure this out tomorrow am (I'm GMT - 5 hours and bushed), I'll try replacing the entire table when the Ajax completes as a workaround.

    -DF
  • DFDF Posts: 13Questions: 0Answers: 0
    I have boiled this down to the simplest test code: one column table, no ajax, just a function to load a static 2-dimensional array and still no joy. (Note the paths for the js and theme files may be non-standard.) I am using version 1.7.0.

    Here's my code:

    [code]
    <!DOCTYPE html>



    dataTables Data Load Test








    var iTable = '';

    $(document).ready(function() {

    iTable = $('#test').dataTable({
    "bJQueryUI": true,
    "bAutoWidth": false,
    "bLengthChange": false,
    "bPaginate": false,
    "sScrollY": "400px",
    "bFilter": true,
    "oLanguage": {
    "sSearch": "Filter records:"
    },
    "sDom": '<"H" <"i_toolbar">f>t<"F" i>',
    "aoColumns":[
    {"sWidth": "50%", "bSortable": false}
    ]
    });

    $("div.i_toolbar").html('Results');

    });

    function display_results() {
    var txt = '[["a"], ["b"]]';
    iTable.fnClearTable();
    iTable.fnAddData(txt);
    }






    Preview results













    [/code]

    Clumps of my hair, torn out in frustration, are piling up around me.

    Any ideas?

    -DF
  • cdaiglecdaigle Posts: 57Questions: 0Answers: 0
    Try changing the var txt from a string to the actual 2D array that it represents.
  • DFDF Posts: 13Questions: 0Answers: 0
    Thanks, cdaigle. jQuery.makeArray() and a little futzing with the string did the trick!

    -DF
  • DFDF Posts: 13Questions: 0Answers: 0
    edited August 2010
    Spoke too soon. jQuery.makeArray () was fine on my little test page, looking like this:

    [code]
    function display_results() {
    var txt = [["a"], ["b"]];
    var arr = jQuery.makeArray(txt);
    iTable.fnClearTable();
    iTable.fnAddData(arr);
    }
    [/code]
    Result = two rows: a and b.

    But when I applied this to my actual code and passed the same string back from my server via ajax, I got one row with both values still in their brackets instead of two bracket-free rows and I couldn't make it work despite much fiddling with the format of the returned string and how I processed it in the callback function.

    So here's my working workaround: Pass back the incoming rows as a string using '|' as a delimiter and cycle through constructing arrays as follows:

    [code]
    function display_results(data) {
    var i;
    var dat = new Array();
    var arr = data.split('|');
    for (i in arr) {
    dat.push(new Array(arr[i]));
    }
    iTable.fnClearTable();
    iTable.fnAddData(dat);
    }
    [/code]

    Would still be interested in any more elegant solutions.

    -DF
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi DF,

    I believe that the following will work as you would expect:

    [code]
    function display_results() {
    iTable.fnClearTable();
    var arr = [["a"], ["b"]];
    iTable.fnAddData(arr);
    }
    [/code]
    There should be no need to use makeArray for this little test case. Having said that, how are you calling display_results? Are you passing it a json data object (i.e. is your $.ajax call dataType json?) or are you passing it a string. If you are giving it a string, then you'll need to call jQuery.parseJSON() and give it your string to convert it into a 2D array. Perhaps you could show us how that code works?

    Regards,
    Allan
This discussion has been closed.