:: Forum >>

From JSON to CellData. ¿HOW?

I get this result from my server

/* JSON format */
var inventario = {
     "total" : 5,
        "columnas" : ["id_cecm","nroge","nombre_centro","ubicacion","tipo_estado","fecha"],
        "valor" :
            [
                {"id_cecm":"1463","nroge":"443","nombre_centro":"cocina centralizada","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"00/00/0000"},
                {"id_cecm":"7553","nroge":"4367","nombre_centro":"panaderia \"baragua\"","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"00/00/0000"},
                {"id_cecm":"7554","nroge":"4368","nombre_centro":"panaderia \"matancera\"","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"00/00/0000"},
                {"id_cecm":"1535","nroge":"4290","nombre_centro":"fabrica de panques","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"01/08/2006"}
            ]
    }

How convert to this one to fill a Grid?
/* CellData values */
var CellData = [["1463","443","cocina centralizada","bcapi","almacen central","00/00/0000"],["7553","4367","panaderia baragua","bcapi","almacen central","00/00/0000"],["7554","4368","panaderia matancera","bcapi","almacen central","00/00/0000"],["1535","4290","fabrica de panques","bcapi","almacen central","01/08/2006"]];
Alain
Thursday, May 17, 2007
After awhile of analysis, with the following fragment of code,
var cell = '';
arreglo1 = new Array(inventario.valor.length);
for(i=0; i<inventario.valor.length; i++){
arreglo2 = new Array(inventario.columnas.length);
for(j=0; j<inventario.columnas.length; j++){
eval ( "cell = inventario.valor[i]."+inventario.columnas[j]+";" );
arreglo2[j] = cell;
}
arreglo1[i] = arreglo2;
}

I managed to solve the raised thing previously, I think that it is not the optimal solution but at the moment solves, I ask for the community to share ideas after managing to enrich the raised solution.

Greetings...
Alain
Thursday, May 17, 2007
Here is the complete code:
<script>
var inventario = {
"total" : 5,
"columnas" : ["id_cecm","nroge","nombre_centro","ubicacion","tipo_estado","fecha"],
"valor" :
[
{"id_cecm":"1463","nroge":"443","nombre_centro":"cocina centralizada","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"00/00/0000"},
{"id_cecm":"7553","nroge":"4367","nombre_centro":"panaderia \"baragua\"","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"00/00/0000"},
{"id_cecm":"7554","nroge":"4368","nombre_centro":"panaderia \"matancera\"","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"00/00/0000"},
{"id_cecm":"1535","nroge":"4290","nombre_centro":"fabrica de panques","ubicacion":"bcapi","tipo_estado":"almacen central","fecha":"01/08/2006"}
]
};
    
var cell = '';
    myCells = new Array(inventario.valor.length);
    for(i=0; i<inventario.valor.length; i++){
aux = new Array(inventario.columnas.length);
for(j=0; j<inventario.columnas.length; j++){
eval ( "cell = inventario.valor[i]."+inventario.columnas[j]+";" );
aux[j] = cell;
     }
     myCells[i] = aux;
    }

var obj = new AW.UI.Grid;

obj.setCellText(myCells);
obj.setColumnCount(inventario.columnas.length);
obj.setRowCount(inventario.valor.length);

document.write(obj);
</script>


Enjoy it!!!
Alain
Thursday, May 17, 2007
Great!
How to get myData from server?

<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1251"></meta>
<title>untitled1</title>
<link href="css/aw.css" rel="stylesheet" type="text/css" />
<script src="JS\aw.js" type="text/javascript"></script>
</head>
<body>
<script>
var myData = [
["A","A1", "A2"],
["B","B1", "B2"],
["C","C1", "C2"],
["D","D1", "D2"],
];

</script>

</body>
</html>


Thanks a lot.
Igor K.
Friday, November 16, 2007
Comment to the original post - that was not possible in AW 2.0 but now JSON object format is fully supported in AW 2.5, for example

var myData = [
{price: 123.00, qty: 50; total:...},
{...}
]

grid.setColumnIndices(['price', 'qty', 'total' ...]);


@Igor - you can request any JSON text string from the server using AW.HTTP.Request object and then convert it to js object with eval() function.
Alex (ActiveWidgets)
Friday, November 16, 2007
Alex!
Thanks very much for your reply.
Maybe sample how to convert with eval() function for this code.
Local it works.

var myColumns=["id_cecm","nroge","nombre_centro","ubicacion"];
var inventario = {
"columnas" : myColumns,
};
..........
grid.setHeaderText(myColumns);


Thanks a lot.
Igor K.
Saturday, November 17, 2007

This topic is archived.


Back to support forum

Forum search