Vaild JSON, still: DataTables warning: Added data does not match known number of columns
Vaild JSON, still: DataTables warning: Added data does not match known number of columns
michael.griffith
Posts: 7Questions: 0Answers: 0
Hi,
I am using Struts2 as an action to load a DataTable, and getting the DataTables warning: Added data does not match known number of columns. I verified the JSON is valid with a JSON validator. The table looks like this:
[code]
Age
Breed
Gender
Id
Name
[/code]
The jQuery init looks like this:
[code]
$(document).ready(function() {
$('#list_table').dataTable( {
"aoColumns": [null, null, null, null, null],
"bFilter": true,
"bProcessing": true,
"sAjaxSource": './list-data.do'
} );
});
[/code]
And my JSON result looks like this:
[code]
{"aaData":[{"age":0,"breed":"Unkown","gender":"Female","id":0,"name":"Snowflake 0"},{"age":1,"breed":"Persian","gender":"Male","id":1,"name":"Snowflake 1"},{"age":2,"breed":"Unkown","gender":"Female","id":2,"name":"Snowflake 2"},{"age":3,"breed":"Persian","gender":"Male","id":3,"name":"Snowflake 3"},{"age":4,"breed":"Unkown","gender":"Female","id":4,"name":"Snowflake 4"},{"age":5,"breed":"Persian","gender":"Male","id":5,"name":"Snowflake 5"},{"age":6,"breed":"Unkown","gender":"Female","id":6,"name":"Snowflake 6"},{"age":7,"breed":"Persian","gender":"Male","id":7,"name":"Snowflake 7"},{"age":8,"breed":"Unkown","gender":"Female","id":8,"name":"Snowflake 8"},{"age":9,"breed":"Persian","gender":"Male","id":9,"name":"Snowflake 9"},{"age":10,"breed":"Unkown","gender":"Female","id":10,"name":"Snowflake 10"},{"age":11,"breed":"Persian","gender":"Male","id":11,"name":"Snowflake 95"},{"age":96,"breed":"Unkown","gender":"Female","id":96,"name":"Snowflake 96"},{"age":97,"breed":"Persian","gender":"Male","id":97,"name":"Snowflake 97"},{"age":98,"breed":"Unkown","gender":"Female","id":98,"name":"Snowflake 98"},{"age":99,"breed":"Persian","gender":"Male","id":99,"name":"Snowflake 99"}]}
[/code]
What am I doing wrong? Thanks in advance for any reply.
I am using Struts2 as an action to load a DataTable, and getting the DataTables warning: Added data does not match known number of columns. I verified the JSON is valid with a JSON validator. The table looks like this:
[code]
Age
Breed
Gender
Id
Name
[/code]
The jQuery init looks like this:
[code]
$(document).ready(function() {
$('#list_table').dataTable( {
"aoColumns": [null, null, null, null, null],
"bFilter": true,
"bProcessing": true,
"sAjaxSource": './list-data.do'
} );
});
[/code]
And my JSON result looks like this:
[code]
{"aaData":[{"age":0,"breed":"Unkown","gender":"Female","id":0,"name":"Snowflake 0"},{"age":1,"breed":"Persian","gender":"Male","id":1,"name":"Snowflake 1"},{"age":2,"breed":"Unkown","gender":"Female","id":2,"name":"Snowflake 2"},{"age":3,"breed":"Persian","gender":"Male","id":3,"name":"Snowflake 3"},{"age":4,"breed":"Unkown","gender":"Female","id":4,"name":"Snowflake 4"},{"age":5,"breed":"Persian","gender":"Male","id":5,"name":"Snowflake 5"},{"age":6,"breed":"Unkown","gender":"Female","id":6,"name":"Snowflake 6"},{"age":7,"breed":"Persian","gender":"Male","id":7,"name":"Snowflake 7"},{"age":8,"breed":"Unkown","gender":"Female","id":8,"name":"Snowflake 8"},{"age":9,"breed":"Persian","gender":"Male","id":9,"name":"Snowflake 9"},{"age":10,"breed":"Unkown","gender":"Female","id":10,"name":"Snowflake 10"},{"age":11,"breed":"Persian","gender":"Male","id":11,"name":"Snowflake 95"},{"age":96,"breed":"Unkown","gender":"Female","id":96,"name":"Snowflake 96"},{"age":97,"breed":"Persian","gender":"Male","id":97,"name":"Snowflake 97"},{"age":98,"breed":"Unkown","gender":"Female","id":98,"name":"Snowflake 98"},{"age":99,"breed":"Persian","gender":"Male","id":99,"name":"Snowflake 99"}]}
[/code]
What am I doing wrong? Thanks in advance for any reply.
This discussion has been closed.
Replies
It seems that you have one extra column in your table header. If you delete the line:
[code][/code]
your issue should go away.
(Also, if you have all the options in aoColumns as null, you don't even need to have it there, although it might make sense to move the width options there from your html).
- ab
Does any other problem jump out with the JSON response? Is the problem possibly with the leading and trailing
[code]{"aaData"[...]} [/code]
Object definition?
[code]
aaData: [
[ 1, 2, 3 ],
[ 4, 5, 6 ]
]
[/code]
The reason for this is that you would need to tell DataTables what column 'id' (etc) belong to. It's possible and probably fairly easy in the code to hack that in - but adds a little more complexity. So I'd suggest if you can change the server-side code to just give you a 2D array that will do nicely - if that can't be done, you could use fnServerData to convert the array of objects in a 2D array.
Allan