Problem with Datatable

Problem with Datatable

linurandylinurandy Posts: 5Questions: 2Answers: 0

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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    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

  • linurandylinurandy Posts: 5Questions: 2Answers: 0
    edited October 2017

    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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Here is what you show as the full JSON response:

    {
    "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"
    }];
    

    I think your response should look like this:

    {
    "draw": "1",
    "recordsFiltered": '1',
    "data": [    << The data objects return should be in an array
    {
    "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"
    }];
    

    The array allows for one or more rows to be returned.

    Kevin

  • linurandylinurandy Posts: 5Questions: 2Answers: 0

    How can i do with python 'cause i use a dictionary and returned as a json.

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    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

  • linurandylinurandy Posts: 5Questions: 2Answers: 0

    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.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    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:

    Error: Parse error on line 3:
    ... "recordsFiltered": '1', "data": {       "d
    -----------------------^
    Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
    

    You have single quotes around '1'. This is not validate JSON format. It states in the at http://www.json.org/ this:

    A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.

    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:

    import json
    
    <code to build the dictionary>
    
    return json.dumps(dictionary)
    

    Kevin

This discussion has been closed.