{"id":1033,"date":"2022-08-30T15:11:16","date_gmt":"2022-08-30T15:11:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/datatables-typeerror-adata-is-undefined-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:16","modified_gmt":"2022-08-30T15:11:16","slug":"datatables-typeerror-adata-is-undefined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/datatables-typeerror-adata-is-undefined-collection-of-common-programming-errors\/","title":{"rendered":"Datatables TypeError: aData is undefined-Collection of common programming errors"},"content":{"rendered":"<p>I want to populate datatables with database values in a table. Below is my code listing, everything seems to be ok but an error is returned &#8220;TypeError: aData is undefined&#8221; so datatables is not created<\/p>\n<pre><code>\n$(document).ready(function() {\n    $('#listax').dataTable({\n        \"bProcessing\": true,\n        \"bLengthChange\": true,\n        \"bFilter\": true,\n        \"bSort\": true,\n        \"bInfo\": true,\n        \"bAutoWidth\": true,\n        \"bServerSide\": true,\n        \"sServerMethod\": \"POST\",\n        \"sAjaxSource\": \"ajax_live\/getUsers\",\n        \"aoColumns\": [\n            null,\n            null \/\/put as many null values as your columns\n        ]\n    });\n});\n\n\n<\/code><\/pre>\n<table id=\"listax\"><\/table>\n<p>Server side<\/p>\n<pre><code> function getUsers(){\n     $this-&gt;db-&gt;select('id,username');\n     $query = $this-&gt;db-&gt;get('user');\n     $data = $query-&gt;result();\n     echo json_encode($data); \n }\n<\/code><\/pre>\n<p>This is the data returned by json encode<\/p>\n<pre><code>[\n    {\"id\":\"6\",\"username\":\"Lab23\"},\n    {\"id\":\"11\",\"username\":\"MaryMM\"}\n]\n<\/code><\/pre>\n<ol>\n<li>\n<p>When I have done this I have had to define the columns. Example array being sent back;<\/p>\n<pre><code>Array\n(\n    [0] =&gt; stdClass Object\n        (\n            [id] =&gt; 6\n            [username] =&gt; \"Lab23\"\n        )\n    [1] =&gt; stdClass Object\n        (\n            [id] =&gt; 11\n            [username] =&gt; \"MaryMM\"\n        )\n)\n<\/code><\/pre>\n<p>So if your json encoded array has key values of <strong>id<\/strong> and <strong>username<\/strong> the datatables would have the following segment in it.<\/p>\n<pre><code>$(document).ready(function() {\n    $('#listax').dataTable({\n        \"bProcessing\": true,\n        \"bLengthChange\": true,\n        \"bFilter\": true,\n        \"bSort\": true,\n        \"bInfo\": true,\n        \"bAutoWidth\": true,\n        \"bServerSide\": true,\n        \"sServerMethod\": \"POST\",\n        \"sAjaxSource\": \"ajax_live\/getUsers\",\n        \"aoColumns\":[\n            {\"mDataProp\":\"id\"},\n            {\"mDataProp\":\"username\"}\n        ]\n    });\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:16:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I want to populate datatables with database values in a table. Below is my code listing, everything seems to be ok but an error is returned &#8220;TypeError: aData is undefined&#8221; so datatables is not created $(document).ready(function() { $(&#8216;#listax&#8217;).dataTable({ &#8220;bProcessing&#8221;: true, &#8220;bLengthChange&#8221;: true, &#8220;bFilter&#8221;: true, &#8220;bSort&#8221;: true, &#8220;bInfo&#8221;: true, &#8220;bAutoWidth&#8221;: true, &#8220;bServerSide&#8221;: true, &#8220;sServerMethod&#8221;: &#8220;POST&#8221;, &#8220;sAjaxSource&#8221;: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1033","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1033"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1033\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}