从购物车中移除后更新总数

huangapple go评论64阅读模式
英文:

Updating a total after removing from the cart

问题

Good evening guys, this is my first time using Stackoverflow, so please be kind.

我是晚上好,这是我第一次使用Stackoverflow,请多多关照。

I have been working on a restaurant order-type app for staff.

我一直在为员工开发一款餐厅点餐类应用程序。

When I delete an item from the cart I would like the total to update.

当我从购物车中删除一个项目时,我希望总额会更新。

Let me know if you need any further assistance with your code translation.

英文:

Good evening guys, this is my first time using Stackoverflow, so please be kind.

I have been working on a restaurant order-type app for staff.

When I delete an item from the cart I would like the total to update.

I know there is probably a simple solution but any help would be much appreciated.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let subtotal = 0;

function Delete(currentEl) {
  currentEl.parentNode.parentNode.removeChild(currentEl.parentNode);
}

const calculateserviceCharge = subtotal =&gt; {
  const serviceCharge = subtotal * .12;
  const formattedserviceCharge = serviceCharge.toFixed(2);
  return formattedserviceCharge;
};

const calculateTotal = subtotal =&gt; {
  const serviceCharge = calculateserviceCharge(subtotal);
  const total = parseFloat(subtotal) + parseFloat(serviceCharge);
  const formattedTotal = total.toFixed(2);

  return formattedTotal;
};

$(&#39;.btn&#39;).on(&#39;click&#39;, function() {
  const title = $(this).data(&#39;title&#39;);
  const price = $(this).data(&#39;price&#39;);

  const element = `
    &lt;p class=&quot;cart-item-heading &quot;&gt;${title} &#163;${price}   &lt;button id=&quot;delete&quot; onclick=&quot;Delete(this);&quot;&gt;X&lt;/button&gt;`;

  $(&#39;.cart-items&#39;).append(element);


  subtotal = subtotal + price;

  const formattedSubtotal = subtotal.toFixed(2.3);
  const serviceCharge = calculateserviceCharge(subtotal);
  const total = calculateTotal(subtotal);

  $(&#39;.cart-math&#39;).html(`
    &lt;p class=&quot;cart-math-item&quot;&gt;
    &lt;span class=&quot;cart-math-header&quot;&gt;Subtotal:&lt;/span&gt;
    &lt;span class=&quot;g-price subtotal&quot;&gt;
    &lt;break&gt;
    &#163;${formattedSubtotal}&lt;/span&gt;
    &lt;/p&gt;
    &lt;p class=&quot;cart-math-item&quot;&gt;
    &lt;span class=&quot;cart-math-header&quot;&gt;Service Charge(12%):&lt;/span&gt;
    &lt;span class=&quot;g-price tax&quot;&gt;&#163;${serviceCharge}&lt;/span&gt;
    &lt;/p&gt;
    &lt;p class=&quot;cart-math-item&quot;&gt;
    &lt;span class=&quot;cart-math-header&quot;&gt;Total:&lt;/span&gt;
    &lt;span class=&quot;g-price total&quot;&gt;&#163;${total}&lt;/span&gt;
    &lt;/p&gt;
    
    `);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;button class=&#39;hide-3&#39; id=&quot;hide&quot; style=&quot;background-color: #edb90f&quot;&gt;Close&lt;/button&gt;
&lt;div class=&quot;mains&quot; id=&quot;subject&quot;&gt;
  &lt;div id=&quot;cart-items&quot; class=&quot;screen-cart&quot;&gt;
    &lt;h2&gt;Your Order&lt;/h2&gt;
    &lt;ul class=&quot;cart-items&quot; id=&quot;cart-items&quot; &quot;&gt;
        &lt;/ul&gt;
    
        &lt;div class=&quot;cart-math &quot; id=&quot;cart-math &quot;&quot;&gt;

  &lt;/div&gt;
&lt;/div&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Duck&quot; data-price=&quot;21.95&quot;&gt;Duck
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Chicken&quot; data-price=&quot;18.95&quot;&gt;Chicken
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Salmon Fillet&quot; data-price=&quot;19.95&quot;&gt;Salmon Fillet
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Orzotto&quot; data-price=&quot;12.95&quot;&gt;Orzotto
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Vegan Burger&quot; data-price=&quot;15.45&quot;&gt;Vegan Burger
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Sea Bass&quot; data-price=&quot;16.95&quot;&gt;Sea Bass
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Classic Burger&quot; data-price=&quot;15.45&quot;&gt;Classic Burger
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Tuna&quot; data-price=&quot;22.95&quot;&gt;Tuna
    &lt;/button&gt;
&lt;button class=&quot;btn&quot; style=&quot;background-color: #edb90f&quot; data-title=&quot;Whole Sea Bream&quot; data-price=&quot;49.95&quot;&gt;Whole Sea Bream
    &lt;/button&gt;
&lt;/div&gt;
&lt;/dialog&gt;

<!-- end snippet -->

答案1

得分: 1

这是我的解决方案,我看到您的按钮将价格存储为数据属性。因此,不必每次添加或删除项目时都进行加法/减法运算,只需每次添加或删除项目时重新计算即可。

循环遍历按钮,将按钮中的价格添加到一个本地变量中。在同一个函数中执行您的服务费等操作,然后将结果输出到您的div中。

您可以通过单击我的删除按钮来查看它的工作原理。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn" data-price="5">REMOVE 5</button>
<button class="btn" data-price="15">REMOVE 15</button>
<button class="btn" data-price="3">REMOVE 3</button>

<div class="cart-math"></div>
const calculateserviceCharge = subtotal => {
  const serviceCharge = subtotal * 0.12;
  const formattedserviceCharge = serviceCharge.toFixed(2);
  return formattedserviceCharge;
};

function calcTotal() {
  let subtotal = 0;

  $('.btn').each(function() {
    subtotal += $(this).data('price');
  })

  const formattedSubtotal = subtotal.toFixed(2.3);
  const serviceCharge = calculateserviceCharge(subtotal);
  const total = (parseFloat(serviceCharge) + parseFloat(subtotal)).toFixed(2.3);

  $('.cart-math').html(`
    <p class="cart-math-item">
      <span class="cart-math-header">Subtotal:</span>
      <span class="g-price subtotal">
        <break>
        &#163;${formattedSubtotal}
      </span>
    </p>
    <p class="cart-math-item">
      <span class="cart-math-header">Service Charge(12%):</span>
      <span class="g-price tax">&#163;${serviceCharge}</span>
    </p>
    <p class="cart-math-item">
      <span class="cart-math-header">Total:</span>
      <span class="g-price total">&#163;${total}</span>
    </p>
  `);
}

calcTotal();

$(".btn").on("click", function() {
  $(this).remove();
  calcTotal();
});

请注意,这段代码使用了jQuery库,确保您已经包含了jQuery库的链接。

英文:

There are many solutions. Here is mine, I see that your btn has the price as a data attribute. So instead of adding/subtracting just recalculate each time you add an item or remove it.

Loop through the buttons adding to a local variable the price from the button. Do your service charges etc.. in the same function then output it to your div.

You can see it working by clicking my remove buttons.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const calculateserviceCharge = subtotal =&gt; {
  const serviceCharge = subtotal * .12;
  const formattedserviceCharge = serviceCharge.toFixed(2);
  return formattedserviceCharge;
};

function calcTotal() {
  let subtotal = 0;

  $(&#39;.btn&#39;).each(function() {
    subtotal += $(this).data(&#39;price&#39;);
  })
  
  const formattedSubtotal = subtotal.toFixed(2.3);
  const serviceCharge = calculateserviceCharge(subtotal);
  const total = (parseFloat(serviceCharge) + parseFloat(subtotal)).toFixed(2.3);

  $(&#39;.cart-math&#39;).html(`
                        &lt;p class=&quot;cart-math-item&quot;&gt;
                        &lt;span class=&quot;cart-math-header&quot;&gt;Subtotal:&lt;/span&gt;
                        &lt;span class=&quot;g-price subtotal&quot;&gt;
                        &lt;break&gt;
                        &#163;${formattedSubtotal}&lt;/span&gt;
                        &lt;/p&gt;
                        &lt;p class=&quot;cart-math-item&quot;&gt;
                        &lt;span class=&quot;cart-math-header&quot;&gt;Service Charge(12%):&lt;/span&gt;
                        &lt;span class=&quot;g-price tax&quot;&gt;&#163;${serviceCharge}&lt;/span&gt;
                        &lt;/p&gt;
                        &lt;p class=&quot;cart-math-item&quot;&gt;
                        &lt;span class=&quot;cart-math-header&quot;&gt;Total:&lt;/span&gt;
                        &lt;span class=&quot;g-price total&quot;&gt;&#163;${total}&lt;/span&gt;
                        &lt;/p&gt;
                        
                        `);

}

calcTotal();

$(&quot;.btn&quot;).on(&quot;click&quot;, function() {
  $(this).remove();
  calcTotal();
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;button class=&quot;btn&quot; data-price=&quot;5&quot;&gt;REMOVE 5&lt;/button&gt;
&lt;button class=&quot;btn&quot; data-price=&quot;15&quot;&gt;REMOVE 15&lt;/button&gt;
&lt;button class=&quot;btn&quot; data-price=&quot;3&quot;&gt;REMOVE 3&lt;/button&gt;

&lt;div class=&quot;cart-math&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

只需在从购物车中删除物品时每次调用计算总额的函数。

英文:

Just call the function that calculates your total everytime you delete from the cart.

答案3

得分: 0

以下是您要翻译的内容:

"I would rather edit my HTML as HTML so I used a pair of <template elements for the totals and new items. Then I add the totals and the individual items where they should be.

When I add a new item. I trigger an event to update the totals by whatever items are in the cart at that time so an add or a delete, I just trigger that event on the cart to update.

Just because, I added some CSS to make it slightly more presentable that just the raw html; but also this shows what is where slightly better."

"我宁愿将我的HTML编辑为HTML,因此我使用了一对<template元素来处理总计和新项目。然后,我将总计和各个项目添加到它们应该位于的位置。

当我添加新项目时,我触发一个事件来根据购物车中的当前项目更新总计,因此添加或删除,我只需在购物车上触发该事件来进行更新。

仅仅因为我添加了一些CSS使其看起来比原始HTML更具可读性;但同时这也更好地展示了各个部分的位置。"

英文:

I would rather edit my HTML as HTML so I used a pair of &lt;template elements for the totals and new items.
Then I add the totals and the individual items where they should be.

When I add a new item. I trigger an event to update the totals by whatever items are in the cart at that time so an add or a delete, I just trigger that event on the cart to update.

Just because, I added some CSS to make it slightly more presentable that just the raw html; but also this shows what is where slightly better.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const totalTemplate = document.getElementById(&quot;cartTotal&quot;);
const itemTemplate = document.getElementById(&quot;cartItem&quot;);

function deleteItem(event) {
  $(this).closest(&#39;.cart-item-container&#39;).remove();
  cart.trigger(&#39;update-ticket&#39;);
}

function addItem(title, price) {
  const cart = $(&#39;#cart-items&#39;);
  const newItem = itemTemplate.content.firstElementChild.cloneNode(true);
  cart[0].appendChild(newItem);
  const newCart = cart.find(&#39;.cart-item-container&#39;).last();
  newCart.data(&quot;cost&quot;, price)
    .find(&#39;.cart-item-heading&#39;).html(title);
  newCart.find(&#39;.price-value&#39;).html(price);

  cart.trigger(&#39;update-ticket&#39;);
}

function getSubtotal(el) {
  return el.dataset.subtotal * 1;
}

function setup() {
  const t = totalTemplate.content.firstElementChild.cloneNode(true);
  document.querySelector(&#39;.cart-math&#39;).appendChild(t);
}

function getCost(cart) {
  let sum = 0;
  cart.find(&#39;.cart-item-container&#39;).each(function(e) {
    sum = sum + ($(this).data(&#39;cost&#39;) * 1);
  });
  return sum;
}
const calculateserviceCharge = subtotal =&gt; {
  const serviceCharge = subtotal * .12;
  const formattedserviceCharge = serviceCharge.toFixed(2);
  return formattedserviceCharge;
};

const calculateTotal = subtotal =&gt; {
  const serviceCharge = calculateserviceCharge(subtotal);
  const total = parseFloat(subtotal) + parseFloat(serviceCharge);
  const formattedTotal = total.toFixed(2);
  return formattedTotal;
};

const cart = $(&#39;#cart-items&#39;)
  .on(&#39;update-ticket&#39;, function() {
    let subtotal = getCost($(this));
    const formattedSubtotal = subtotal.toFixed(2.3);
    const serviceCharge = calculateserviceCharge(subtotal);
    const total = calculateTotal(subtotal);
    $(&#39;.cart-math&#39;).find(&#39;.subtotal-value&#39;).html(formattedSubtotal);
    $(&#39;.cart-math&#39;).find(&#39;.tax-value&#39;).html(serviceCharge);
    $(&#39;.cart-math&#39;).find(&#39;.total-value&#39;).html(total);
  })
  .on(&#39;click&#39;, &#39;.delete&#39;, deleteItem);

$(&#39;.btn.food-item&#39;).on(&#39;click&#39;, function(event) {
  const title = $(this).data(&#39;title&#39;);
  const price = $(this).data(&#39;price&#39;);
  addItem(title, price);
});
setup();

<!-- language: lang-css -->

body {
  font-size: 16px;
  box-sizing: border-box;
  margin: 0px;
  padding: 0px;
}

.screen-cart-container {
  display: grid;
  place-items: center;
}

.food-item {
  background-color: #edb90f;
}

.order-header {
  display: block;
  font-size: 1.5em;
  margin-top: 0.83em;
  margin-bottom: 0.83em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}

.cart-items .cart-item-container {
  border: solid 1px #00000033;
  display: grid;
  grid-template-columns: 6em 3em 2em;
  grid-auto-rows: minmax(3em, auto);
  gap: 0.25em;
  align-items: center;
  padding: 0.25em;
}

.total-container {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(3, 2em);
  place-items: center;
  gap: 1em;
  border: 1px solid green;
  padding: 1em;
}

.menu-container {
  display: flex;
  gap: 0.25em;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;dialog&quot;&gt;
  &lt;button class=&#39;hide-3&#39; id=&quot;hide&quot; style=&quot;background-color: #edb90f&quot;&gt;Close&lt;/button&gt;
  &lt;div class=&quot;mains&quot; id=&quot;subject&quot;&gt;
    &lt;div id=&quot;cart-container&quot; class=&quot;screen-cart screen-cart-container&quot;&gt;
      &lt;div class=&quot;order-header&quot;&gt;Your Order&lt;/div&gt;
      &lt;div class=&quot;cart-items&quot; id=&quot;cart-items&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;cart-math&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;menu-container&quot;&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Duck&quot; data-price=&quot;21.95&quot;&gt;Duck
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Chicken&quot; data-price=&quot;18.95&quot;&gt;Chicken
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Salmon Fillet&quot; data-price=&quot;19.95&quot;&gt;Salmon Fillet
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Orzotto&quot; data-price=&quot;12.95&quot;&gt;Orzotto
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Vegan Burger&quot; data-price=&quot;15.45&quot;&gt;Vegan Burger
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Sea Bass&quot; data-price=&quot;16.95&quot;&gt;Sea Bass
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Classic Burger&quot; data-price=&quot;15.45&quot;&gt;Classic Burger
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Tuna&quot; data-price=&quot;22.95&quot;&gt;Tuna
    &lt;/button&gt;
      &lt;button class=&quot;btn food-item&quot; type=&quot;button&quot; data-title=&quot;Whole Sea Bream&quot; data-price=&quot;49.95&quot;&gt;Whole Sea Bream
    &lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;template id=&quot;cartItem&quot;&gt;
&lt;div class=&quot;cart-item-container&quot; data-cost=&quot;${price}&quot;&gt;
    &lt;div class=&quot;cart-item-heading&quot;&gt;&lt;/div&gt;&lt;div&gt;&#163;&lt;span class=&#39;price-value&#39;&gt;&lt;/span&gt;&lt;/div&gt;&lt;button type=&quot;button&quot; class=&quot;delete&quot;&gt;X&lt;/button&gt;&lt;/div&gt;
&lt;/template&gt;
&lt;template id=&quot;cartTotal&quot;&gt;
  &lt;div class=&quot;total-container&quot;&gt;
    &lt;div class=&quot;cart-math-item&quot;&gt;
    &lt;span class=&quot;cart-math-header&quot;&gt;Subtotal:&lt;/span&gt;
    &lt;span class=&quot;g-price subtotal&quot;&gt;&#163;&lt;span class=&quot;subtotal-value&quot;&gt;&lt;/span&gt;
       &lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;cart-math-item&quot;&gt;
    &lt;span class=&quot;cart-math-header&quot;&gt;Service Charge(12%):&lt;/span&gt;
    &lt;span class=&quot;g-price tax&quot;&gt;&#163;&lt;span class=&quot;tax-value&quot;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;cart-math-item&quot;&gt;
    &lt;span class=&quot;cart-math-header&quot;&gt;Total:&lt;/span&gt;
    &lt;span class=&quot;g-price total&quot;&gt;&#163;&lt;span class=&quot;total-value&quot;&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 04:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定