Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/09/2016, 09:31
albertt_t
 
Fecha de Ingreso: noviembre-2004
Mensajes: 159
Antigüedad: 19 años, 5 meses
Puntos: 0
Underscore template desde json

Saludos tengo un problema al mandar un arrays de objects (items) en el siguiente json

Código:
{

	"tipo_doc": "PASAPORTE",
	"nro_doc": "123333",
	"razon_social": "JUAN GALLEGO",
	"fecha_inicial": "20/10/2016",
	"fecha_final": "22/10/2016",

	"items": [

		{
			"local": "Tres",
			"anio": 2016,
			"numero": 20,
			"id": 12,
			"pos":1
		}, {
			"local": "dos",
			"anio": 2016,
			"numero": 22,
			"id": 13,
			"pos":2
		}, {
			"local": "uno",
			"anio": 2016,
			"numero": 25,
			"id": 14,
			"pos":3
		}
	]


}


Y esta es mi plantilla a donde inyectare los valores del Json

Cita:

<strong>{{ tipo_doc }} {{ nro_doc }}</strong><br>
<address>{{ razon_social }}</address>
<hr>
<strong>Fecha de N&uacute;meraci&oacute;n</strong><br>
{{ fecha_desde }}<br>
{{ fecha_hasta }}<br>


<table class="table table-striped table-bordered footable">
<thead>
<tr>
<th>C&oacute;digo</th>
<th data-hide="phone, tablet">Local</th>
<th data-hide="phone, tablet">Año</th>
<th data-hide="phone">N&uacute;mero</th>
<th data-hide="phone">Ver</th>
</tr>
</thead>
<tbody>

<%


for( var i in items) { %>
<tr>
<td>{{ items[i].id }}</td>
<td>{{ items[i].local }}</td>
<td>{{ items[i].anio }}</td>
<td>{{ items[i].numero }}</td>


</tr>
<% } %>
</tbody>
</table>
Esta es mi función donde capturo el Json y lo envio

Código:
	var url = 'recibe.php';
			$.ajax({
				method: 'post',
				url: url,
				dataType: 'json',
				success: function (data) {

				showPreview(data);



				}
			})

Código:
 function showPreview (data) {
      

        $('#form-1').addClass('hidden');
        $("#panel-preview").removeClass('hidden');
        $.get('resultados-xhr.html').success(function (rtext) {
            var tpl = _.template(rtext)
            var parsed = tpl(data)
            $("#preview-resultado").html(parsed).find('table').footable();
        })
    }

El problema que tengo es al recorrer el arrays de objects ( Item) en el json.

Me retorna un error de sintaxis

Uncaught ReferenceError: i is not defined. Estoy recorriendo mal el array de objetos en items?.

Como detalle use un for en el template (estatico para verificar )

<% for (i=0; i<3; i++) %>

Pero igual no me sigue reconociendo esa variable para imprimir mi array de objects item.

Gracias por su ayuda de antemano.