Cannot read property 'length' of undefined while using stateLoadCallback()
Cannot read property 'length' of undefined while using stateLoadCallback()
i am using the save states to save the data from the table in my sql database, i can save the data and pull the data from the database just fine but when i try to load the object with stateloadcallback i get this error Cannot read property 'length' of undefined. it appears to be in another plugin but most deffinately being caused by the load callback. here is my code, i am currently trying to load it from a hard coded object, but i get the error weather i use the database calls or the hard coded object
function initDataTable(){
console.log(viewName);
//$('.table-cpf-datatable').colResizable();
// initiate datatables on lead search table
var table = $('#table-accounts').DataTable({
"stateSave": true,
"stateLoadCallback": function (settings) {
console.log('test');
var o;
// $.ajax({
// data:{
// vName: viewName
// },
// "async": false,
// url: basePath + '/state_load.php',
// dataType: 'json'
// }).done(function(data){
// console.log("load");
// console.log(data);
// o = data;
// });
o = {
"time": 1447777052268,
"start": 0,
"length": 0,
"columns": [
{
"visible": true
},
{
"visible": true
},
{
"visible": true
},
{
"visible": false
},
{
"visible": true
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": false
},
{
"visible": true
}
],
"colReorder": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
],
"colResize:": {
"columns": [
"48px",
"95px",
"",
"144px",
"180px",
"120px",
"101px",
"109px",
"118px",
"83px",
"69px",
"73px",
"117px",
"112px",
"128px",
""
],
"tableSize": 1667
},
};
console.log(o);
return o;
},
"stateDuration": 0,
"scrollX": true,
"autoWidth": false,
//iCookieDuration: 0, //60*60*24, // 1 day
"stateSaveParams": function (settings, data) {
delete data.search;
for (var ii=0;ii<data.columns.length;ii++)
{delete data.columns[ii].search};
},
"stateSaveCallback": function (settings, data) {
// Send an Ajax request to the server with the state object
//console.log("save");
console.log(data);
var dataString = JSON.stringify(data);
//console.log(dataString);
$.ajax( {
"url": basePath + "/state_save.php",
"data": {
dataString: data,
vName: viewName
},
"data": {
this: data,
vName: viewName
},
"dataType": "json",
"type": "POST",
"success": function () {}
} );
},
//"iDisplayStart": 0,
//"iDisplayLength": 10,
"deferRender": true,
"displayStart":0,
"displayLength": -1,
//"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, 'All']],
"language": {
"search": "Narrow Search: "//,
//"sEmptyTable": "No lead campaign data found"
},
"columnDefs":[{
// Sort column 1 (formatted date) by column 6 (hidden seconds)
"orderData":[ 3 ], "targets": [ 4 ]
},
{ "type": "natural", targets: 3 }],
"columns": [
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "width":"170px"},
{"visible": false, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": true, "min-width":"45px"},
{"visible": false, "min-width":"45px"}
],
"sorting": [],
"dom": "RJ<'dataTables_info_top'i><'row'<'dataTables_control'l><'dataTables_control'Cf>r>t",
//"sDom": "<'row'<'col-xs-6'i><'col-xs-6'f>r>t<'row'<'col-xs-6'><'col-xs-6'>>",
//"dom": 'R C<"clear">lfrtip',
"colReorder": {
"order": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
},
"colResize":{},
"colVis": {
"buttonText": "<span title='Change column visibility'<i class='fa fa-columns'></span>",
"aiExclude": [ 1, 3 ],
"sAlign": "right",
"label": function ( index, title, th ) {
switch (index){
case 0: return "left Buttons";
break;
case 16: return "Right Buttons";
break;
default: return title;
}
}
}
});
in state_load.php;
```
<?php
$DEBUG = true;
require_once('../../../../config.inc.php');
$vname = !empty($_REQUEST['vName']) ? $_REQUEST['vName'] : null;
$stmt = Database::$db->prepare("SELECT view_data FROM views WHERE user_id = :uid AND view_table = 'admin_accounts' AND view_name = :vname");
$params = array(
':uid' => $u['id'],
':vname' => $vname
);
$stmt->execute($params);
echo $stmt->fetchColumn();
some help on this issue would be awesome! i have been stuck with this for quite some time. if you need anything from me to help you help me feel free to ask, thank you everyone
-Tylor
Answers
You need to actually look into the error... Change the DataTables JS file from the min to the full JS file (if it isnt already), then when it shows the error, view the line number, and you can follow back through the backtrace of the error to see what line in your code generated it.
But whenever I run into this via DataTables, its usually because the Ajax response doesn't have the data formatted properly, or no data at all...
If I can replicate it, then this would be much easier to help diagnose :)