Loading with AJAX Problem: aLAyout[0] is undefined?>
Loading with AJAX Problem: aLAyout[0] is undefined?>
I get the error message "aLayout[0] is undefined" in firebug when i try to load my datatable.
I want to load it using an ajax call.
This is how the table appears in the document before trying to initialize the table:
[code]
jQuery(document).ready(function() {
jQuery('#foodtable').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '../ajax/getglobalfood.php'
} );
});
[/code]
This is the php script that processes the script:
[code]
<?php
$columns = array( 'foodid', 'food', 'brand', 'ftype', 'sa', 'ss', 'cal', 'fat', 'sat', 'trans', 'chol', 'sod', 'carb', 'fib', 'sug', 'pro' );
$sIndexColumn = "foodid";
$table = "globalfood";
$gaSql['user'] = "root";
$gaSql['password'] = "";
$gaSql['db'] = "mealchamp";
$gaSql['server'] = "localhost";
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
$query = "SELECT foodid, food, brand, ftype, sa, ss, cal, fat, sat, trans, chol, sod, carb, fib, sug, pro FROM globalfood";
$result = mysql_query( $query, $gaSql['link'] ) or die(mysql_error());
$output = array( "sEcho" => 0, "iTotalRecords" => 0, "iTotalDisplayRecords" => 0, "aaData" => array() );
while ( $row = mysql_fetch_array($result) ) {
$newRow = array();
for ( $i = 0; $i < count($columns); $i++ ) {
$newRow[] = $row[ $columns[$i] ];
$count = $i;
}
$output['aaData'][] = $newRow;
}
$output["iTotalRecords"] = $count;
$output["iTotalDisplayRecords"] = $count;
$z = json_encode( $output );
echo $z;
?>
[/code]
and this is the JSON sent back by the script, which i got using a different function than datatable just to see if it is working and is valid, which it is.
[code]
{"sEcho":0,"iTotalRecords":15,"iTotalDisplayRecords":15,"aaData":[["1","Mozzarella cheese","Generic","Cheeses","1\/4 inch cube","30 g","110.00","8.00","5.00","0.00","25.00","230.00","1.00","0.00","0.00","8.00"],["2","Pancake and Waffle Mix","Generic","Cereal Grains and Rices","","34 g","120.00","1.50","0.40","0.00","15.00","510.00","23.00","1.00","3.00","3.00"],["3","100% :Parmesan-Cheese!!","Generic","Cheeses","","12 g","45.00","2.50","1.50","0.10","5.00","230.00","1.00","0.00","0.00","5.00"],["4","Boney Nut Os Cereal","Generic","Breakfeast Cereals","","30 g","120.00","0.50","0.00","0.00","0.00","150.00","26.00","2.00","12.00","2.00"],["5","Canned Cut Asparagus","Generic","Vegetables","1\/2 cup","","25.00","0.30","0.00","0.00","0.00","260.00","4.00","2.00","2.00","2.00"],["6","Teriyaki Style Rice","Generic","Cereal Grains and Rices","1\/45 package","","160.00","0.50","0.20","0.00","0.00","520.00","34.00","0.00","4.00","3.00"],["7","Chinese Fried Rice","Generic","Cereal Grains and Rices","","42 g","150.00","0.50","0.20","0.00","0.00","370.00","33.00","1.00","1.00","3.00"],["8","Cooked Turkey Breast","Generic","Meat - Poultry & Egg Products","2 slices","","40.00","0.50","0.30","0.00","20.00","460.00","1.00","0.00","1.00","8.00"],["9","Diet-Coke","Generic","Beverages","1 serving","250 mL","0.00","0.00","0.00","0.00","0.00","35.00","0.00","0.00","0.00","0.10"],["10","100% Whole-wheat bread","Generic","Breads and Crackers","2 slices","","180.00","2.00","0.50","0.00","0.00","240.00","33.00","4.00","3.00","7.00"],["11","Boneless Chicken Breast","Generic","Meat - Poultry & Egg Products","1 breast","125 g","110.00","2.00","0.50","0.00","65.00","650.00","0.00","0.00","0.00","22.00"],["12","Hot dogs","Generic","Meat - Beef","1 wiener","37 g","120.00","10.00","3.50","0.10","20.00","410.00","2.00","0.00","0.00","5.00"],["13","Ketchup","Generic","Condiments, Dressings, and Spreads","1 tbsp","15 mL","20.00","0.00","0.00","0.00","0.00","140.00","5.00","0.00","4.00","0.30"],["14","Milk Chocolate Crispie Joys","Generic","Desserts and Treats","About 4","40 g","190.00","10.00","6.00","0.00","5.00","55.00","26.00","1.00","18.00","3.00"],["15","Ranchers Choice Calorie Wise","Generic","Fats and Oils","1 Tbsp.","15 mL","35.00","2.50","0.40","0.00","5.00","170.00","3.00","0.00","1.00","0.10"],["16","Strawberry Cereal Bars","Generic","Breakfeast Cereals","1 bar","38 g","130.00","2.50","0.20","0.00","5.00","210.00","25.00","3.00","10.00","2.00"],["17","Whole Kernel Corn","Generic","Vegetables","15\/234 cup","125 mL","80.00","0.50","0.00","0.00","0.00","10.00","15.00","2.00","1.00","2.00"]]}
[/code]
QUESTION 1: why is this not working? I think it must have to do with i havnt told datatables the column names, but i'm not sure where and how to do this.
QUESTION 2: do i rly need to enable "bServerSide": true ? From what i can tell i dont need any processing, just returning aaData array is enough. I only included sEcho, iTotalRecords, and iTotalDisplayRecords in the return array because that's the example they give at http://www.datatables.net/release-datatables/examples/data_sources/server_side.html, which does more than i require for my purposes.
Thanks in advance, G
O and here's a possible clue. before i added the thead, tbody, and tfoot tags to the table i was getting a similar error, something like "nThr is undefined". do i need to prebuild the table maybe? but i have no way of knowing what i need before the query.
I want to load it using an ajax call.
This is how the table appears in the document before trying to initialize the table:
[code]
jQuery(document).ready(function() {
jQuery('#foodtable').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '../ajax/getglobalfood.php'
} );
});
[/code]
This is the php script that processes the script:
[code]
<?php
$columns = array( 'foodid', 'food', 'brand', 'ftype', 'sa', 'ss', 'cal', 'fat', 'sat', 'trans', 'chol', 'sod', 'carb', 'fib', 'sug', 'pro' );
$sIndexColumn = "foodid";
$table = "globalfood";
$gaSql['user'] = "root";
$gaSql['password'] = "";
$gaSql['db'] = "mealchamp";
$gaSql['server'] = "localhost";
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
$query = "SELECT foodid, food, brand, ftype, sa, ss, cal, fat, sat, trans, chol, sod, carb, fib, sug, pro FROM globalfood";
$result = mysql_query( $query, $gaSql['link'] ) or die(mysql_error());
$output = array( "sEcho" => 0, "iTotalRecords" => 0, "iTotalDisplayRecords" => 0, "aaData" => array() );
while ( $row = mysql_fetch_array($result) ) {
$newRow = array();
for ( $i = 0; $i < count($columns); $i++ ) {
$newRow[] = $row[ $columns[$i] ];
$count = $i;
}
$output['aaData'][] = $newRow;
}
$output["iTotalRecords"] = $count;
$output["iTotalDisplayRecords"] = $count;
$z = json_encode( $output );
echo $z;
?>
[/code]
and this is the JSON sent back by the script, which i got using a different function than datatable just to see if it is working and is valid, which it is.
[code]
{"sEcho":0,"iTotalRecords":15,"iTotalDisplayRecords":15,"aaData":[["1","Mozzarella cheese","Generic","Cheeses","1\/4 inch cube","30 g","110.00","8.00","5.00","0.00","25.00","230.00","1.00","0.00","0.00","8.00"],["2","Pancake and Waffle Mix","Generic","Cereal Grains and Rices","","34 g","120.00","1.50","0.40","0.00","15.00","510.00","23.00","1.00","3.00","3.00"],["3","100% :Parmesan-Cheese!!","Generic","Cheeses","","12 g","45.00","2.50","1.50","0.10","5.00","230.00","1.00","0.00","0.00","5.00"],["4","Boney Nut Os Cereal","Generic","Breakfeast Cereals","","30 g","120.00","0.50","0.00","0.00","0.00","150.00","26.00","2.00","12.00","2.00"],["5","Canned Cut Asparagus","Generic","Vegetables","1\/2 cup","","25.00","0.30","0.00","0.00","0.00","260.00","4.00","2.00","2.00","2.00"],["6","Teriyaki Style Rice","Generic","Cereal Grains and Rices","1\/45 package","","160.00","0.50","0.20","0.00","0.00","520.00","34.00","0.00","4.00","3.00"],["7","Chinese Fried Rice","Generic","Cereal Grains and Rices","","42 g","150.00","0.50","0.20","0.00","0.00","370.00","33.00","1.00","1.00","3.00"],["8","Cooked Turkey Breast","Generic","Meat - Poultry & Egg Products","2 slices","","40.00","0.50","0.30","0.00","20.00","460.00","1.00","0.00","1.00","8.00"],["9","Diet-Coke","Generic","Beverages","1 serving","250 mL","0.00","0.00","0.00","0.00","0.00","35.00","0.00","0.00","0.00","0.10"],["10","100% Whole-wheat bread","Generic","Breads and Crackers","2 slices","","180.00","2.00","0.50","0.00","0.00","240.00","33.00","4.00","3.00","7.00"],["11","Boneless Chicken Breast","Generic","Meat - Poultry & Egg Products","1 breast","125 g","110.00","2.00","0.50","0.00","65.00","650.00","0.00","0.00","0.00","22.00"],["12","Hot dogs","Generic","Meat - Beef","1 wiener","37 g","120.00","10.00","3.50","0.10","20.00","410.00","2.00","0.00","0.00","5.00"],["13","Ketchup","Generic","Condiments, Dressings, and Spreads","1 tbsp","15 mL","20.00","0.00","0.00","0.00","0.00","140.00","5.00","0.00","4.00","0.30"],["14","Milk Chocolate Crispie Joys","Generic","Desserts and Treats","About 4","40 g","190.00","10.00","6.00","0.00","5.00","55.00","26.00","1.00","18.00","3.00"],["15","Ranchers Choice Calorie Wise","Generic","Fats and Oils","1 Tbsp.","15 mL","35.00","2.50","0.40","0.00","5.00","170.00","3.00","0.00","1.00","0.10"],["16","Strawberry Cereal Bars","Generic","Breakfeast Cereals","1 bar","38 g","130.00","2.50","0.20","0.00","5.00","210.00","25.00","3.00","10.00","2.00"],["17","Whole Kernel Corn","Generic","Vegetables","15\/234 cup","125 mL","80.00","0.50","0.00","0.00","0.00","10.00","15.00","2.00","1.00","2.00"]]}
[/code]
QUESTION 1: why is this not working? I think it must have to do with i havnt told datatables the column names, but i'm not sure where and how to do this.
QUESTION 2: do i rly need to enable "bServerSide": true ? From what i can tell i dont need any processing, just returning aaData array is enough. I only included sEcho, iTotalRecords, and iTotalDisplayRecords in the return array because that's the example they give at http://www.datatables.net/release-datatables/examples/data_sources/server_side.html, which does more than i require for my purposes.
Thanks in advance, G
O and here's a possible clue. before i added the thead, tbody, and tfoot tags to the table i was getting a similar error, something like "nThr is undefined". do i need to prebuild the table maybe? but i have no way of knowing what i need before the query.
This discussion has been closed.
Replies
2) bServerSide could be false, in which case you just need to return a valid object with aaData, and the client side will perform searching, sorting, etc. with the data it has. bServerSide is more effective when you have a large data set and don't want to send it all to the client all at once.
No - you can use Ajax sourced data for client-side processing - as shown in this example: http://datatables.net/release-datatables/examples/data_sources/ajax.html
There is some discussion on the different data source types here:
http://datatables.net/usage/#data_sources
Allan