मैंने डेटाबेस से कुल आइटम लाने के लिए एक लूप बनाया है। मैं ईजेएस का उपयोग कर रहा हूं और यह मेरा कोड नीचे दिखाया गया है।
<h2>Summary</h2>
<% if(typeof items.cart!=="undefined"){
var amount = 0; %>
<% items.cart.forEach(function(file) {
amount +=parseInt(file.price); %>
<ul class="summary-table" id="summary-table">
<li><span>subtotal:</span> <span><%= amount %></span></li>
<li><span>delivery:</span> <span>Free</span></li>
<li><span>discount:</span> <span></span></li>
<li><span>total:</span> <span><%= amount %></span></li>
</ul>
<% }); %>
<% } %>
मुझे कुल सामान मिला है। लेकिन जब कीमत कुल राशि में जुड़ जाएगी तो सारांश कई बार प्रिंट होगा।
उन्हें रोकने के लिए कोई सुझाव
1
Harish
21 फरवरी 2019, 06:11
1 उत्तर
सबसे बढ़िया उत्तर
ऐसा लगता है कि आपका <ul>
टैग प्रत्येक लूप के अंदर है, इसलिए यह items.cart
के प्रत्येक तत्व के लिए मुद्रित है। मैं टेम्पलेट के अलावा कहीं और कुल मूल्य की गणना करने और केवल प्रतिपादन के लिए टेम्पलेट का उपयोग करने की अनुशंसा करता हूं।
मैं ईजेएस से परिचित नहीं हूं, लेकिन आप इसे आजमा सकते हैं:
<h2>Summary</h2>
<% if (typeof items.cart !== "undefined") {
var amount = 0; %>
<% items.cart.forEach(function (file) {
amount += parseInt(file.price); }); %>
<ul class="summary-table" id="summary-table">
<li><span>subtotal:</span> <span><%= amount %></span></li>
<li><span>delivery:</span> <span>Free</span></li>
<li><span>discount:</span> <span></span></li>
<li><span>total:</span> <span><%= amount %></span></li>
</ul>
<% } %>
1
WofWca
21 फरवरी 2019, 03:36