Reputation: 680
Im using datatables to show data and I want to fetch language settings from my server in a nice way. The problem is to format the data in a way that makes this possible, I would prefer to use json format with as few modifications as possible.
I want to use this:
(as an argument for my datatable creation, what is this? array? object? json string?)
var oLanguage =
{
"oLanguage": {
"sLengthMenu": "_MENU_ per page"
}
};
I try to obtain this data from the server using this method:
$.getJSON( '/olan', function(data){
oLanguage=data;
});
SERVER CODE: (PHP, SLIM)
$app->get('/olan', function() use ($app, $lan){
$oLanguage = array('oLanguage' => array('sLengthMenu' => '_MENU_ per page'));
$response = $app->response();
$response['Content-Type'] = 'application/json';
$response->body(json_encode($oLanguage));
});
I was able to iterate through the data in the $.getJSON method and all information is there, must be something with the format/object handling. I don't yet have a good understanding of the type of object that i need, i can declare it, but i don't know what it is:) I hope its some kind of json object so i can just easily get it as i try to do. I could maybe declare it using $.each iteration but it would be ugly.
This is my first question here on stackoverflow, thank you for helping me out
-------------------------Answer (cannot post it the first 8 hours)---------------
Turns out,
thanks to Shinosha
, that there is a build in feature of datatables to get the language settings from server. By creating the table like this:
$('#tableid').dataTable({
"oLanguage": {
"sUrl": "/olan"
}
});
and editing my server code: (changing the $oLanguage variable)
$app->get('/olan', function() use ($app, $lan){
$oLanguage = array('sLengthMenu' => '_MENU_ per page');
$response = $app->response();
$response['Content-Type'] = 'application/json';
$response->body(json_encode($oLanguage));
});
It works!
In Javascript, [] denotes array and {} denotes object. You can nest objects and arrays as required. Use a json validator like jsonlint.com to validate your server response. , gvmani
oLanguage is an object with the attribute oLanguage which is an object with the attribute sLengthMenu. sLengthMenu have the value "MENU per page"
var oLanguage =
{
"oLanguage": {
"sLengthMenu": "_MENU_ per page"
}
};
Upvotes: 1
Views: 138
Reputation: 1600
In Javascript, [] denotes array and {} denotes object. You can nest objects and arrays as required. Use a json validator like jsonlint.com to validate your server response.
Upvotes: 1