I'm trying to render a datatable with dynamic columns using JSON but keep getting the following error:
Uncaught TypeError: Cannot read property 'length' of undefined.
Any help would be greatly appreciated.
Thanks!!
JS:
<link href="http://cdn.datatables.net/1.10.4/css/jquery.dataTables.css" rel="stylesheet" type="text/css"/>
<script src="http://cdn.datatables.net/1.10.4/js/jquery.dataTables.js" type="text/javascript"></script>
jQuery(document).ready(function() {
var dataObject = '[{"COLUMNS":[{ "sTitle": "NAME"}, { "sTitle": "COUNTY"}],"DATA":[["John Doe","Fresno"],["Billy","Fresno"],["Tom","Kern"],["King Smith","Kings"]]}]';
var columns = [];
jQuery.each(dataObject.COLUMNS, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
});
jQuery('#example').dataTable({
"bProcessing": true,
"bPaginate": true,
"sPaginationType": "full_numbers",
"aaData": dataObject.DATA,
"aoColumns": columns
});
});
HTML:
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<tr><thead>column1</thead></tr>
<tbody></tbody>
</table>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…