Local Javascript JSON to Datatable error
Local Javascript JSON to Datatable error
agustinbus
Posts: 5Questions: 2Answers: 0
Hi all! first sorry for my bad english.
I have a function on javascript that generate a json using the data of form fields:
var animales_list = new Array();
function animal(id_categoria, categoria, id_raza, raza, cabezas, kilos_total, id_estado, adpv, peso_estim_termi, consumo_peso_vivo, id_corral_dest) {
this.id_categoria = id_categoria;
this.categoria = categoria;
this.id_raza = id_raza;
this.raza = raza;
this.cabezas = cabezas;
this.kilos_total = kilos_total;
this.id_estado = id_estado;
this.adpv = adpv;
this.peso_estim_termi = peso_estim_termi;
this.consumo_peso_vivo = consumo_peso_vivo;
this.id_corral_dest = id_corral_dest;
}
function insert_animal() {
id_cat = document.getElementById('edt_categoria_id').options[document.getElementById("edt_categoria_id").selectedIndex].value;
cat = document.getElementById('edt_categoria_id').options[document.getElementById("edt_categoria_id").selectedIndex].text;
id_raz = document.getElementById('edt_raza_id').options[document.getElementById("edt_raza_id").selectedIndex].value;
raz = document.getElementById('edt_raza_id').options[document.getElementById("edt_raza_id").selectedIndex].text;
cab = document.getElementById('edt_cabezas').value;
kg_tot = document.getElementById('edt_kilos_total').value;
id_est = document.getElementById('edt_estado_id').options[document.getElementById("edt_estado_id").selectedIndex].value;
adpv = document.getElementById('edt_adpv').value;
peterm = document.getElementById('edt_peso_estimado_terminacion').value;
conspv = document.getElementById('edt_consumo_peso_vivo').value;
id_cor = document.getElementById('edt_corral_destinado_id').options[document.getElementById("edt_corral_destinado_id").selectedIndex].value;
var ani = new animal(id_cat, cat, id_raz, raz, cab, kg_tot, id_est, adpv, peterm, conspv, id_cor);
animales_list.push(ani);
var animalesListJSON = JSON.stringify(animales_list);
If i do an alert(animalesListJSON) i have de following string:
[{"id_categoria":"9",
"categoria":"Torito",
"id_raza":"2","raza":"Hereford",
"cabezas":"12",
"kilos_total":"1920",
"id_estado":"4",
"adpv":"0.75",
"peso_estim_termi":"340",
"consumo_peso_vivo":"4",
"id_corral_dest":"1"}]
then if i try to display the data on the datatable i have an error:
$("#tbl_animales").dataTable().fnDestroy();
tblAnimales= $('#tbl_animales').dataTable( {
"aaData": animalesListJSON,
"aoColumns": [
{ "mDataProp": "id_categoria" },
{ "mDataProp": "categoria"},
{ "mDataProp": "id_raza"},
{ "mDataProp": "raza"},
{ "mDataProp": "cabezas"},
{ "mDataProp": "kilos_total"}
]
} );
but if i directly put the JSON string on aaData its work ok:
$("#tbl_animales").dataTable().fnDestroy();
//alert(animalesListJSON);
tblAnimales= $('#tbl_animales').dataTable( {
"aaData":
[{"id_categoria":"9",
"categoria":"Torito",
"id_raza":"2","raza":"Hereford",
"cabezas":"12",
"kilos_total":"1920",
"id_estado":"4",
"adpv":"0.75",
"peso_estim_termi":"340",
"consumo_peso_vivo":"4",
"id_corral_dest":"1"}],
"aoColumns": [
{ "mDataProp": "id_categoria" },
{ "mDataProp": "categoria"},
{ "mDataProp": "id_raza"},
{ "mDataProp": "raza"},
{ "mDataProp": "cabezas"},
{ "mDataProp": "kilos_total"}
]
} );
Its a bug?
Help please!
This discussion has been closed.
Answers
As you say, it is a string, not an array or object! Pass in
animales_list
to thedata
option!Allan
I answer myself, the problem was i use directly the animalesListJSON string and it have to parse it like this: var data = JSON.parse(animalesListJSON);
I don't understand why you would encode it to JSON and then decode it. Why not just use it as I suggested?
Allan
Thank you allan! I do it beacuse i have to send a petition to another site!
Ah - that makes sense! Thanks for the update :-)
Allan