stateLoadCallback causing strange error on nextPage click and leading zeros on page info
stateLoadCallback causing strange error on nextPage click and leading zeros on page info

Hi,
I've uploaded a config report ref: afebor
I'm saving and loading state info to and from a MySql source via ajax using typical ajax methods. Everything works perfectly well except for two strange glitches.
- The
"info"
string is displaying like:- Showing 01 to 025 of 52 entries (Notice leading zeros) - The
"pagingType": "full_numbers"
is working OK except for the NEXT button. When clicked, the console reports the error:-TypeError: w is undefined
My Save and Load code in the dataTables script is as follows:-
var tbl = $('#' + tableId).DataTable( {
stateSave: true,
stateSaveCallback: function (settings, ssData) {
$.ajax({
url: '" . URL . "myaccount/saveview/' + tableId,
data: ssData,
dataType: 'json',
type: 'POST',
success: function(){}
});
},
stateLoadCallback: function (settings, callback) {
$.ajax( {
url: '" . URL . "myaccount/loadview/' + tableId,
dataType: 'json',
success: function (json) {
callback(json);
}
});
}
My php code in my controller is:-
public function loadView($table_id)
{
$json = UserModel::getMyTableView($table_id);
echo $json;
}
public function saveView($table_id)
{
$json = json_encode($_POST);
// if we don't clean the POSTed data then "columns":[{"visible":"false" wont work on stateLoadCallback(). it needs to be "columns":[{"visible":false
$clean = str_replace('"false"', 'false', $json);
echo UserModel::saveView($table_id, $clean);
}
and in my model, the code is:-
public static function getMyTableView($table_id)
{
$sql = "SELECT state_save FROM user_views WHERE user_id = :user_id AND table_id = :table_id LIMIT 1";
$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare($sql);
$query->execute(array(':user_id' => Session::get('user_id'), ':table_id' => $table_id));
$row = $query->fetch();
if (empty($row))
{
return json_encode(array());
} else {
return $row->state_save;
}
}
public static function saveView($table_id, $post)
{
$sql = "INSERT INTO user_views (user_id, table_id, state_save) VALUES (:user_id, :table_id, :state_save) ON DUPLICATE KEY UPDATE table_id = :table_id, state_save = :state_save";
$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare($sql);
//$query->execute(array(':table_id' => $post['table_id'], ':column_list' => $post['column_list'], ':user_id' => Session::get('user_id')));
$query->execute(array(':table_id' => $table_id, ':state_save' => $post, ':user_id' => Session::get('user_id')));
if ($query->rowCount()) {
return 'Layout Saved';
}
return 'The layout could not be saved';
}
I've ran the debugger on my table and received no errors or warnings and I can't find anything that refers to this sort of problem
Thank you
This question has an accepted answers - jump to answer
Answers
That suggests that the state being loaded contains strings for the number values, rather than numbers - e.g.
compared to:
Can you show me the JSON that you are loading as the state?
Thanks,
Allan
Bang on Allan!! Thank you for your help.
I have added the
JSON_NUMERIC_CHECK
option to thejson_encode
function before the save like:-$json = json_encode($_POST, JSON_NUMERIC_CHECK);
It has corrected the issue now and key: value pairs are saved like
"start": 0
instead of"start": "0"
The JSON before the correction (typical)
{"time":"1580867942647","start":"0","length":"25","order":[["0","asc"]],"search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"},"columns":[{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}}],"ColReorder":["0","2","1","4","3","5"]}
And after the
JSON_NUMERIC_CHECK
option was added to thejson_encode
function (differnent table){"time":1580930224417,"start":0,"length":25,"order":[[0,"asc"]],"search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"},"columns":[{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}},{"visible":"true","search":{"search":"","smart":"true","regex":false,"caseInsensitive":"true"}}],"ColReorder":[0,1,2,3,4,5,6]}
Perfect! Thanks for letting me know about
JSON_NUMERIC_CHECK
- I wasn't aware of that option.Allan