Json not well formed
Json not well formed
Hello,
I want to display a list of users with a json file. Here is the json file :
[code]
{
"aaData" : [
{
"user_id" : "1",
"name" : "Grace Wilson"
},
{
"user_id" : "2",
"name" : "John Hamilton"
},
{
"user_id" : "3",
"name" : "Amanda Hamilton"
}
]
}
[/code]
I am sure the json is valid, as i've tested it on several sites (e.g http://jsonlint.com/)
Here is my code for displaying the table :
[code]
$(document).ready(function() {
var oTable = $("#table_id").dataTable({
"bInfo": false,
"bPaginate": false,
"sScrollY": "200",
"bScrollCollapse": false,
"sAjaxSource": "arrays.txt",
"aoColumns": [
{ "sTitle": "Name",
"mData": "name" }
]
});
$('#table_id tr').click( function() {
$(this).toggleClass('row_selected');
} );
} );[/code]
The list is displayed correctly, but i've the following error message with FireFox : 'Not well formed'. My problem is the click function is no more working. When i include the data with an array directly in the dataTable function, i don't have this problem, and the click function is working. Any help would be highly appreciated.
I want to display a list of users with a json file. Here is the json file :
[code]
{
"aaData" : [
{
"user_id" : "1",
"name" : "Grace Wilson"
},
{
"user_id" : "2",
"name" : "John Hamilton"
},
{
"user_id" : "3",
"name" : "Amanda Hamilton"
}
]
}
[/code]
I am sure the json is valid, as i've tested it on several sites (e.g http://jsonlint.com/)
Here is my code for displaying the table :
[code]
$(document).ready(function() {
var oTable = $("#table_id").dataTable({
"bInfo": false,
"bPaginate": false,
"sScrollY": "200",
"bScrollCollapse": false,
"sAjaxSource": "arrays.txt",
"aoColumns": [
{ "sTitle": "Name",
"mData": "name" }
]
});
$('#table_id tr').click( function() {
$(this).toggleClass('row_selected');
} );
} );[/code]
The list is displayed correctly, but i've the following error message with FireFox : 'Not well formed'. My problem is the click function is no more working. When i include the data with an array directly in the dataTable function, i don't have this problem, and the click function is working. Any help would be highly appreciated.
This discussion has been closed.
Replies
Allan
Allan
When i link the click event to a table row, the table is not fully loaded and ready. At first, i didn't have this problem because the data were hard coded in the html.
There is several solutions, but i think the use of delegated events is elegant :
[code]
$("#table_id tbody").on( "click", "tr", function() {
$(this).toggleClass('row_selected');
} );
[/code]
I found the solution on this page : http://api.jquery.com/on/