JSrender没有显示嵌套对象的数据 [英] JSrender not showing data for nested objects
问题描述
我有从PHP返回的数据,然后我解析它。数据显示为名为empName的属性,但不适用于嵌套的属性。我需要在我的javascript代码中纠正有什么问题。下面是从PHP返回的JSON字符串(名为info的变量的输出):
Well i have the data returned from PHP which i then parse it. Data shows up for the attribute called empName but not for the nested ones. What is wrong that i need to correct in my javascript code. Heres the JSON string which returns from PHP (the output of variable called info):
[{"empName":"Sam","skillDetails":[{"skill":"Javascript","level":"Intermediate"}]}]
这是模板(数据不显示技能和等级)
And here' s the template (The data does not show for skill and level)
<script id="tmplEmpDetails" type="text/x-jsrender">
{{if data.length > 0}}
{{for data}}
<tr>
<td class="tg-spn1">{{:#view.data.empName}}</td>
<td class="tg-spn1">{{:#view.data.skillDetails.skill}}</td>
<td class="tg-spn1 ">{{:#view.data.skillDetails.level}}</td>
</tr>
{{/for}}
{{/if}}
</script>
Javascript:
The Javascript:
$.post( "indexBase.php",
$('#str').val(JSON.stringify(empObj)),
function(info){
var result = JSON.parse(info);
$( "#tblEmpDetails tr[id!='hdrEmpDetails']" ).empty();
$("#tblEmpDetails").append($("#tmplEmpDetails").render({
data : result
}));
});
$("#mainForm").submit( function() {
return false;
});
$('.trSkillCls').not(':first').remove();
});
最后HTML(当然我也需要更正我的HTML)
And finally the HTML (Sure i need to correct my HTML too)
<table class="tg" width="100%" id="tblEmpDetails">
<tr id="hdrEmpDetails">
<th class="tg-s6z2" >Employee</th>
<th class="tg-s6z2" >Skill</th>
<th class="tg-s6z2" >Level</th>
</tr>
</table>
这是否意味着我必须在模板中循环两次?
Does that mean i have to for loop twice in my template?
推荐答案
.post( indexBase.php ,
(' #str')。val(< span class =code-sdkkeywo rd> JSON .stringify(empObj)),
function (info){
var result = JSON .parse(info);
('#str').val(JSON.stringify(empObj)), function(info){ var result = JSON.parse(info);
( #tblEmpDetails tr [id!='hdrEmpDetails'])。empty();
( "#tblEmpDetails tr[id!='hdrEmpDetails']" ).empty();
这篇关于JSrender没有显示嵌套对象的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!