Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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):

JavaScript
[{"empName":"Sam","skillDetails":[{"skill":"Javascript","level":"Intermediate"}]}]


And here' s the template (The data does not show for skill and level)

JavaScript
<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>


The Javascript:

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();
}); 


And finally the HTML (Sure i need to correct my HTML too)

HTML
<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?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900