dataTable, ajax, json
dataTable, ajax, json
silens
Posts: 101Questions: 40Answers: 0
Hola, tengo esta funcion ajax
$(document).ready (function() {
$.ajax({
url: "php/identidades.php",
success : function(data) {
var jsonObject = JSON.parse(data);//A la variable le asigno el json decodificado
$('#tbl_entidad').dataTable( {
data : jsonObject,
columns: [
{"data" : "id"},
{"data" : "name"},
{"data" : "cif"}
],
});
}
});});
Quiero usar Datatable.
Pero no funciona, Que puede estar mal?
Muchas Gracias
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Well take a look at the object, e.g. through a dump or in your debugger.
I suspect you need to change this:
('''
Muchas gracias, dice que la tabla no tiene registros....No data avaiable in table
Esta es la estructura del Json. Que puede ser???
Yo pienso que lo que esta mal es la llamada a los registros.
Muchas gracias.
''')
in you screenshot I see that the object is called "JSON", not "o"?!
is it rather "o.JSON.ent[0].id" ??
Maybe another idea if you can't find the solution: Change your approach more according to the "data tables standard" (if it exists ... sometimes data tables is maybe a bit too flexible).
Here is what I believe would be closer to the standard. I would code it like this:
and on the PHP side in "identidades.php"
Please note the spelling of "DataTable". I think it is quite important that you spell it with a capital "D".
Take a look at this: https://datatables.net/reference/type/DataTables.Api
And if you take a look at the examples you'll find that it is always "DataTable" and never "dataTable" ...
Thank you very much for your insistence, I am new to this and at the beginning it is complicated
This function works fine, now I want to weight the content into columns as does DataTable.
'''
function saldos(dni){
var parametros = {
}'''
Sorry, I have no idea what you want to do with this. It was clearer to me in Spanish ... Are you using Google translate or one of those services? No funcionan estos servicios ...
Es posible esto mismo pero con django y python
Le pido disculpas. Esta funcion trabaja correctamente pero yo no la quiero usar de esta manera, prefiero usarla con DataTable.
Necesito transformar esta funcion en una funcion que trabaje con Datatable, añadiendole columnas.
Dejo esta funcion que trabaja como ejemplo, necesito saber, que hay que cambiar en esta funcion para que trabaje con DataTable.
function saldos(dni){
var parametros = {
}
Le pido disculpas. Esta funcion trabaja correctamente pero yo no la quiero usar de esta manera, prefiero usarla con DataTable.
Necesito transformar esta funcion en una funcion que trabaje con Datatable, añadiendole columnas.
Dejo esta funcion que trabaja como ejemplo, necesito saber, que hay que cambiar en esta funcion para que trabaje con DataTable.
function saldos(dni){
var parametros = {
}
Esta funcion que pongo abajo, no funciona, dice que no hay datos.
$(document).ready (function() {
$.ajax({
url: "php/identidades.php",
type: "POST",
success : function(data) {
var o = JSON.parse(data);//A la variable le asigno el json decodificado
$('#tbl_entidad').DataTable( {
data : o,
columns: [
{"data" : "o.ent[0].id"},
{"data" : "o.ent[0].name"},
{"data" : "o.ent[0].cif"}
],
});
}
});
});
Well, I don't know exactly why your last function does not work but I wouldn't code it that way. Please revisit my detailed suggestion above.
I think you can make any of your functions work with datatables. All you need to do is make sure the JSON response is in the format that datatables expects.
You can render individual fields in one column but also arrays. No problem.
The easiest way is certainly to use both Data Tables and Editor at the front end and also on the server with PHP. That is what I am doing. I took that decision before I even started my project because I hate front end programming and wanted to have it as easy as possible. But there are additional advantages: You don't need to worry about the data format (e.g. what does my JSON object look like etc.), Editor does that for you. You don't need to code SELECT, INSERT, UPDATE and DELETE statements yourself. All you need to do is to set up one Editor instance on the back end site and one at the front end. An Editor license is cheap. It costs only 85 sterling which at the moment is less than €94 and just over $110.
Here is an example that handles a table with approx. 300,000 rows. As you can see Editor also has a server side processing feature handling all the sorting, paging and what have you very fast and efficiently on the server side in case your tables are large. It also has multi language support etc ...
Javascript:
If you don't want to edit the table, fine. Makes it even easier; like this:
And on the server side you have this (PHP):
Again: If you don't want to edit the table it is even simpler:
'''
[code]
El problema debe de ser el origen de los datos, esta funcion trabaja bien
$(document).ready(function() {
$('#tbl_entidad').DataTable({
ajax: "php/data.php",
});
Esto es el data.php:
$dato = array(
"data"=>array(
array(
"name"=>"Peter",
"lastname"=>"Griffin",
"city"=>"Quahog",
"gender"=>"male"
)
);
$data=json_encode($dato);
echo $data;
Aqui, data.php devuelve un array en php. Esto funciona bien, el problema es que mi pagina identidades.php, toma los datos de una web externa que los devuelve en formato json.
Esto seria identidades.php
$url="http://..........mi url" ;
$json = file_get_contents($url);
echo $json;
Este el el json que devuelve url
{
"count": 1000,
"total_count": 11667,
"ent": [
{
"id": 1,
"name": "fsadfsdfsdfsdfs.",
"pai": 1,
"cif": "324",
"fch": "2014-02-12T00:00:00.000Z"
}
}
Si uso la web data.php. Funcion bien, pero con identidades.php no.
No consigo entender cual es el problema
[/code]'''.)
Creo que mi problema es igual que este:
https://datatables.net/forums/discussion/42374
Can you please post your code using Markdown as described below. You only need to use the three back ticks and your code is legible so much better.
It really seems like you have the same issue. You will need to transform your JSON data into the format Data Tables expects! The JSON the URL gives you does not have the correct format.
Just use this to tranform the data:
https://datatables.net/reference/option/ajax.dataSrc
Search for "Manipulate the data returned from the server" and you'll find an example. Buena suerte!!
If you look at the JSON response below you can see that it does not comply with what Data Tables expects. Please also look at the ajax examples: https://datatables.net/examples/ajax/
Muchisimas gracias.
Yo necesito acceder a ent.id, ent.name, etc , count y total_count no vale...
Creo que tambien tengo que usar datasource=""
Estoy buscando para ver si encuentro como hacerlo.
Muchas gracias por su insistencia.
You can also transform the data on the server side with PHP and then return precisely what Data Tables expects. Puede ser la solución más fácil.
Otra posibilidad: In your PHP script you read the data from the external site, insert it into an SQL database and subsequently use Editor and Data Tables to display and edit the data, as per my example above. I do that a lot with data that I get from screen scraping. Works fine!
Ya lo he solucionado, he formateado la entrada de datos para dejar solo los que me interesaban. con el file_get_contents.
Al mandar el json directamente bien formateado, la funcion es realmente facil.
Muchisimas gracias por todo.
De nada. I guess I also learned something. So far I haven't used data tables without Editor but I might do that for the reporting I am getting started with right now ...