Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/12/2015, 16:20
jbravov
 
Fecha de Ingreso: diciembre-2015
Ubicación: Mexico
Mensajes: 1
Antigüedad: 8 años, 4 meses
Puntos: 0
Pregunta enviar arreglo de objetos con elementos json

Buenas tardes
Estoy realizando un arreglo que guarde los elementos de la siguiente forma

var testData = [
{id: 1, name: 'My Organization', parent: 0},
{id: 2, name: 'CEO Office', parent: 1},
{id: 3, name: 'Division 1', parent: 1},
{id: 4, name: 'Division 2', parent: 1},
{id: 6, name: 'Division 3', parent: 1},
{id: 7, name: 'Division 4', parent: 1},
{id: 8, name: 'Division 5', parent: 1},
{id: 5, name: 'Sub Division', parent: 3},
];

La información la extraigo de mysql por medio de la siguiente archivo

datos.php
try{

$conn = conectaDb();

$sql ="SELECT ID,nombre,Padre_ID FROM entidades;";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
$conn=null;
echo json_encode($result);
}
catch (PDOException $e){
echo "<p>Error: No puede conectarse con la base de datos. <Mantenimiento de banco></p>\n";

}

Se guarda en json, para que posteriormente la lea y le de el formato que requiere la variables tesData.
Para ello realizo lo siguiente:

<!DOCTYPE html>
<html>
<head>
<title>NODOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="http://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
<link href="jquery.orgchart.css" media="all" rel="stylesheet" type="text/css" />
<style type="text/css">
#orgChart{
width: auto;
height: auto;
}

#orgChartContainer{
width: 1000px;
height: 500px;
overflow: auto;
background: #eeeeee;
}

</style>
</head>
<body>
<ul>
</ul>
<script type="text/javascript"><!--

//-->
</script>

</script></div>
</div>
</div>
</div>
<div id="orgChart"></div>
</div>
</div>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="jquery.orgchart.js"></script>
<script type="text/javascript">

var testData=[];

$(function(){
$.getJSON("datos.php", function(result){
$.each(result, function(i, field){

//Doy formato al item

testData[i]= "{id: "+field.ID+", name:'"+field.nombre+"', parent:"+field.Padre_ID+"},";
//console.log(testData[i]);
});
// verifico datos
$.each( testData, function( key, value ) {
//alert( key + ": " + value );
console.log(key + ": " + value );
});
});

//Aqui llamo la funcion y es donde me marca error
org_chart = $('#orgChart').orgChart
({
data: testData2,
showControls: true,
});
});

// just for example purpose
function log(text){
$('#consoleOutput').append('<p>'+text+'</p>')
}
</script>
</body>
</html>

Hasta donde esta la funcion $.getJSON extraigo los valores y cuando los imprimo en consola se visualiza el formato que necesito testData, sin embargo me marca el error

Uncaught TypeError: Cannot read property 'render' of undefined

Estoy tratando de realizar la creacion del arbol, por medio de este ejemplo
http://www.jqueryscript.net/chart-graph/Create-An-Editable-Organization-Chart-with-jQuery-orgChart-Plugin.html

Pero no me deja, no se si el error es por que estoy creando mal el arreglo, o bien por que no se donde colocarlo cuando inicia jquery