Function within aoColumns
Function within aoColumns
gusadolfo
Posts: 3Questions: 0Answers: 0
Hi I was wondering if it was possible to create a function() inside an aoColumns, because the object I receive through Ajax is different as the one needed for the DataTables, but I also use that same object for a plotting function.
This is an example of the object I receive:
[code]{"IdCentro":1,"FechaDesde":"2013-02-12","FechaHasta":"2013-11-18","Dimension":"TipoAtencion",
"ListaCentrosCostos":{
"NESTLE":{
"FUNDACION NESTLE(JUBILADOS)":
"Atributo":{
"Tipo":"LOCALIDAD"},
"ATENCION MEDICA PRIMARIA":{
"Siniestrados":4,
"PorcentajeSiniestros":0.47,
"PorcentajeMonto":0.00,
"MontoSiniestro":12075000.00},
"ODONTOLOGICO":{
"Siniestrados":1,
"PorcentajeSiniestros":0.12,
"PorcentajeMonto":0.00,
"MontoSiniestro":487500.00},
},
"Totales":{
"Siniestrados":845,
"PorcentajeSiniestros":100.03,
"MontoSiniestro":569672686.00,
"PorcentajeMonto":99.98
}}}} [/code]
is a much larger than that, but I was wondering if it's possible to create a function(), using mootools, to get the object.key for the sTitle and the object.value for the mData in:
[code]"aoColumns": [{ "sTitle": Object.key , "mData": Object.value , "sClass": "center" },][/code]
I need this because the object I'll receive can and will change a lot. If anyone has a solution for this issue I'd really appreciate it.
Thanks in advanced for any possible help!!
This is an example of the object I receive:
[code]{"IdCentro":1,"FechaDesde":"2013-02-12","FechaHasta":"2013-11-18","Dimension":"TipoAtencion",
"ListaCentrosCostos":{
"NESTLE":{
"FUNDACION NESTLE(JUBILADOS)":
"Atributo":{
"Tipo":"LOCALIDAD"},
"ATENCION MEDICA PRIMARIA":{
"Siniestrados":4,
"PorcentajeSiniestros":0.47,
"PorcentajeMonto":0.00,
"MontoSiniestro":12075000.00},
"ODONTOLOGICO":{
"Siniestrados":1,
"PorcentajeSiniestros":0.12,
"PorcentajeMonto":0.00,
"MontoSiniestro":487500.00},
},
"Totales":{
"Siniestrados":845,
"PorcentajeSiniestros":100.03,
"MontoSiniestro":569672686.00,
"PorcentajeMonto":99.98
}}}} [/code]
is a much larger than that, but I was wondering if it's possible to create a function(), using mootools, to get the object.key for the sTitle and the object.value for the mData in:
[code]"aoColumns": [{ "sTitle": Object.key , "mData": Object.value , "sClass": "center" },][/code]
I need this because the object I'll receive can and will change a lot. If anyone has a solution for this issue I'd really appreciate it.
Thanks in advanced for any possible help!!
This discussion has been closed.
Replies
Allan
[code]var cc = Object.keys(data.ListaCentrosCostos)[0];
var r = data.ListaCentrosCostos[cc];
var aaData = [];
Object.each(r, function (val, nVal) {
var fila = {};
fila["Atributo"] = val["Atributo"]["Tipo"];
Object.erase(val, "Atributo");
Object.each(val, function (tipoAtn, tipoAtnN) {
fila["TipodeAtencion"] =tipoAtnN;
fila["Siniestrados"] = tipoAtn["Siniestrados"];
fila["PorcentajeSiniestros"] = tipoAtn["PorcentajeSiniestros"];
fila["MontoSiniestro"] = tipoAtn["MontoSiniestro"];
fila["PorcentajeMonto"] = tipoAtn["PorcentajeMonto"];
});
aaData.push(fila);
});
jQuery('#tabla-reporte').dataTable({
"aoColumns": [
{ "sTitle": "Atributo" },
{ "sTitle": "TipodeAtencion" },
{ "sTitle": "Siniestrados" },
{ "sTitle": "PorcentajeSiniestros" },
{ "sTitle": "MontoSiniestro" },
{ "sTitle": "PorcentajeMonto" }
],
"aaData": aaData
});
[/code]
but my object arrives like this:(from the chrome debugger doing a console.log(aaData))
[code]
[Object, Object, Object, $family: function, $constructor: function, each: function, clone: function, clean: function…]
0: Object
Atributo: "LOCALIDAD"
MontoSiniestro: "1993625.00"
PorcentajeMonto: "3.50"
PorcentajeSiniestros: "3.08"
Siniestrados: "26"
TipodeAtencion: "REEMBOLSO"
[/code]
and the aaData still doesn't load it and i get this error: "DataTables warning (table id='tabla-reporte'):Requested unknown parameter '0' from the data source row 0"
[code]
"aoColumns": [
{ "sTitle": "Atributo", "mData": "Atributo" },
{ "sTitle": "Tipo de Atención", "mData": "TipodeAtencion" },
{ "sTitle": "Siniestrados", "mData": "Siniestrados" },
{ "sTitle": "% Siniestros", "mData": "PorcentajeSiniestros" },
{ "sTitle": "Monto del Siniestro", "mData": "MontoSiniestro" },
{ "sTitle": "% Monto", "mData": "PorcentajeMonto" }
],
[/code]
that fixed the issue