The .js mast passes through the JSON data.

Can anyone advise how my template loop should loop through JSON data in the following example?

DEMO here: http://jsfiddle.net/Seefeld/LbVEH/

{ "0": { "Dosage": "25", "Drug": "Indocin", "Patient": "David", "Date": "15/11/2012 14:29:14" }, "1": { "Dosage": "50", "Drug": "Enebrel", "Patient": "Sam", "Date": "15/11/2012 14:29:14" }, "2": { "Dosage": "10", "Drug": "Hydralazine", "Patient": "Christoff", "Date": "15/11/2012 14:29:14" }, "3": { "Dosage": "21", "Drug": "Combivent", "Patient": "Janet", "Date": "15/11/2012 14:29:14" }, "4": { "Dosage": "100", "Drug": "Dilantin", "Patient": "Melanie", "Date": "15/11/2012 14:29:14" } } 

All the examples I saw on mustache.js assume that you know the name of the object. Any suggestion is much appreciated.

+4
source share
1 answer

You can convert data to a genuine array (which should be anyway):

 var drugs = []; for (var i = 0, drug; (drug = data[i]); ++i) { drugs.push(drug); } var template = "{{#drugs}}<p>{{Drug}}</p>{{/drugs}}"; var html = Mustache.to_html(template, {drugs: drugs}); $(html).appendTo("#cnt"); 

or view the data yourself:

 var template = "<p>{{Drug}}</p>"; for (var i = 0, drug; (drug = data[i]); ++i) { var html = Mustache.to_html(template, drug); $(html).appendTo("#cnt"); } 
+8
source

Source: https://habr.com/ru/post/1446257/


All Articles