I understand this a popular issue, and I have read all the similar questions here on Stack Overflow and other sites (including the datatables website).
To clarify, I am using
- PHP Codeigniter
- Materliazecss
I have also made sure that I received the JSON array correctly:
[{"name_en":"hello","phone":"55555555"},{"name_en":"hi","phone":"00000000"}]
My HTML table looks like this:
<table id="customer_table">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
</tr>
</thead>
</table>
And here is my document.ready
function:
$(document).ready(function(){
//$('#customer_table').DataTable();
$('#customer_table').DataTable( {
"ajax": 'json',
"dataSrc": "",
"columns": [
{ "data": "email" },
{ "data": "name_en" }
]
});
});
The error I am getting is
Uncaught TypeError: Cannot read property 'length' of undefined
CAUSE
This errors
TypeError: Cannot read property 'length' of undefined
usually means that jQuery DataTables cannot find the data in the response to the Ajax request.By default jQuery DataTables expects the data to be in one of the formats shown below. Error occurs because data is returned in the format other than default.
Array of arrays
Array of objects
SOLUTION
Use default format or use
ajax.dataSrc
option to define data property containing table data in Ajax response (data
by default).See Data array location for more information.
LINKS
See jQuery DataTables: Common JavaScript console errors for more details.
When you have JSON data then the following error appears
A better solution is to assign a
var data
for the local json array object, details see: https://datatables.net/manual/tech-notes/4This is helps you to display table contents.