2017-02-23 27 views
0

Form API У меня есть данные JSON, как показано ниже. Я новичок в node.js, поэтому не знаю, как разбирать данные JSON.Json data Разбор в node.js handlebars

[ 
    { 
    "Detail": " Rs. 1001 Full Talktime Topup Rs.1001 Full Talktime", 
    "Amount": "1001", 
    "Validity": "Unlimited" 
    }, 
    { 
    "Detail": " Rs. 1000 Full Talktime Topup Rs.1050 more than Full Talktime", 
    "Amount": "1000", 
    "Validity": "Unlimited" 
    }, 
    { 
    "Detail": " Rs. 899 Full Talktime Topup Rs.899 Full Talktime", 
    "Amount": "899", 
    "Validity": "Unlimited" 
    }, 
    { 
    "Detail": " Rs. 501 Full Talktime Topup Rs.501 Full Talktime", 
    "Amount": "501", 
    "Validity": "Unlimited" 
    }, 
    { 
    "Detail": " Rs. 500 Full Talktime Topup Rs.500 Full Talktime", 
    "Amount": "500", 
    "Validity": "Unlimited" 
    }, 
    { 
    "Detail": " Rs. 225 Full Talktime Topup Rs.225 Full Talktime", 
    "Amount": "225", 
    "Validity": "30 days" 
    }, 
    { 
    "Detail": " Rs. 59 Full Talktime Topup Rs.59 Full Talktime", 
    "Amount": "59", 
    "Validity": "7 days" 
    }, 
    { 
    "Detail": " Rs. 32 Full Talktime Topup Rs.32 Full Talktime", 
    "Amount": "32", 
    "Validity": "3 days" 
    } 
] 

handlebars.registerHelper является

hbs.registerHelper("FTT", function(author){ 
    return JSON.stringify(code); 
}); 

и в шаблоне Рули

{{#each FTT}} 
    <tr> 
     <td>Hello</td> 
     <td>{{FTT.[0].Detail}}</td> 
     <td>{{this.[0].Amount}}</td> 
     <td>{{Validity}}</td> 
    </tr> 
{{/each}} 

В FTT ничего не displaing. Я также пытаюсь рассеять сущность, но все равно ничего не получаю. Любой sugession,

+0

использование 'JSON.parse (код),' JSON.stringify (код) 'преобразует его в строка. –

+0

Я также пытаюсь это сделать, но каждый не зацикливает FTT, можете ли вы предложить какой-либо живой пример для этого –

ответ

0

Вот рабочий пример, я надеюсь, что это помогает:

$(function() { 
 
    var context = {}; 
 

 
    Handlebars.registerHelper('ftt', function(author) { 
 
    Object.assign(context, { 
 
     FTT: jsonData 
 
    }); 
 
    }); 
 

 
    var theTemplateScript = $("#ftt-html").html(); 
 
    var theTemplate = Handlebars.compile(theTemplateScript); 
 
    var theCompiledHtml = theTemplate(context); 
 

 
    $('.content-placeholder').html(theCompiledHtml); 
 

 
}); 
 

 

 
var jsonData = [{ 
 
    "Detail": " Rs. 1001 Full Talktime Topup Rs.1001 Full Talktime", 
 
    "Amount": "1001", 
 
    "Validity": "Unlimited" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 1000 Full Talktime Topup Rs.1050 more than Full Talktime", 
 
    "Amount": "1000", 
 
    "Validity": "Unlimited" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 899 Full Talktime Topup Rs.899 Full Talktime", 
 
    "Amount": "899", 
 
    "Validity": "Unlimited" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 501 Full Talktime Topup Rs.501 Full Talktime", 
 
    "Amount": "501", 
 
    "Validity": "Unlimited" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 500 Full Talktime Topup Rs.500 Full Talktime", 
 
    "Amount": "500", 
 
    "Validity": "Unlimited" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 225 Full Talktime Topup Rs.225 Full Talktime", 
 
    "Amount": "225", 
 
    "Validity": "30 days" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 59 Full Talktime Topup Rs.59 Full Talktime", 
 
    "Amount": "59", 
 
    "Validity": "7 days" 
 
    }, 
 
    { 
 
    "Detail": " Rs. 32 Full Talktime Topup Rs.32 Full Talktime", 
 
    "Amount": "32", 
 
    "Validity": "3 days" 
 
    } 
 
];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script> 
 

 
<script id="ftt-html" type="text/x-ftt-template"> 
 
    
 
    {{ftt}} 
 

 
    {{#each FTT}} 
 
    <tr> 
 
    <td>Hello</td> 
 
    <td>{{Detail}}</td> 
 
    <td>{{Amount}}</td> 
 
    <td>{{Validity}}</td> 
 
    </tr> 
 
    {{/each}} 
 

 
</script> 
 

 
<div class="content-placeholder"></div>

 Смежные вопросы

  • Нет связанных вопросов^_^