Problem with Datatable
Problem with Datatable
I made a datatable but when i do an json object work perfect but when i use ajax don't appear. There is the code:
var table = $("#table_activos").dataTable({
"processing": true,
"serverSide": true,
"pagingType": "full_numbers",
"ajax": {
url: "activos/mostrar/",
// dataSrc: 'data'
type: "GET"
},
// "data": datos,
"columns": [
{ "data": "icon" },
{ "data": "nombre" },
{ "data": "direccion_ip" },
{ "data": "tipos_dispositivos" },
{ "data": "sistema_operativo" },
{ "data": "valor_activo" },
{ "data": "vuln_planif" },
{ "data": "disp_planif" }
],
"language": {
"zeroRecords": "No hay activos disponibles"
}
});
there is the javascript object:
var datos = [{
"draw": "1",
"recordsFiltered": '1',
"data":{
"direccion_ip": "1.1.1.1",
"vuln_planif": "no",
"nombre": "er",
"sistema_operativo": "1",
"disp_planif": "no",
"icon": "<img src='#' class= 'ui-icon ui-icon-circle-plus' />",
// "icon": "2",
"tipos_dispositivos": "1",
"DT_RowData": {
"pkey": "1"
},
"valor_activo": "2",
"DT_RowId": "1"
},
"recordsTotal": "1"
}];
when them came from the network:
{
"draw": "1",
"recordsFiltered": '1',
"data":{
"direccion_ip": "1.1.1.1",
"vuln_planif": "no",
"nombre": "er",
"sistema_operativo": "1",
"disp_planif": "no",
"icon": "<img src='#' class= 'ui-icon ui-icon-circle-plus' />",
// "icon": "2",
"tipos_dispositivos": "1",
"DT_RowData": {
"pkey": "1"
},
"valor_activo": "2",
"DT_RowId": "1"
},
"recordsTotal": "1"
}];
Answers
I think the problem is that your JSON
data
object(s) should be in an array, as shown here:https://datatables.net/manual/data/#Objects
Kevin
i do something i passed a function to dataSrc: function(json){
console.log(json.data);
}
and returned me:
Object { direccion_ip: "1.1.1.1", sistema_operativo: 1, DT_RowData: Object, icon: "<img src=' # ' class= ' ui-icon ui-…", disp_planif: "no", vuln_planif: "no", nombre: "er", tipos_dispositivos: 1, valor_activo: "2", DT_RowId: 1 }
i think the object is well constructed.
that is an error that appear down:
TypeError: c is undefined
Here is what you show as the full JSON response:
I think your response should look like this:
The array allows for one or more rows to be returned.
Kevin
How can i do with python 'cause i use a dictionary and returned as a json.
Unless Kevin knows Python (sorry, can't remember if you do or not Kevin!) you'd need to ask on StackOverflow or a Python forum.
Allan
The dictionary example you have contains one row of data. You need to place it into a list. Let's say that your dictionary is called
data
. You could do something like this in Python:data = [data]
Then you can use
json.dumps
or whatever to create the json response.However, I suspect you are going to return more than one row. I use MySql with pymsql library. With that library there is a method to return one response which is a dictionary. There is another to return all rows from the SQL query, which is a list of dictionaries. Each dictionary is a row of data.
It might be easier to use another method with your DB library to receive a list of dictionaries even if its one row.
Kevin
I ask in starckflow about everything but nothing. I think that the problem is not the python code, you can see that in https://es.stackoverflow.com/questions/106778/datatable-con-jquery.
Is the way that datatables read json.
Looked closer at your JSON and it doesn't look like its formatted correctly so I used jsonlint to validate it. This is the first of many errors:
You have single quotes around
'1'
. This is not validate JSON format. It states in the at http://www.json.org/ this:You need to use double quotes.
How are you generating the JSON string from your dictionary?
I use Python's builtin JSON library like this:
Kevin