2017-08-10 39 views
-1

我有各種.js函數計算模式中的訂單小計,稅額和最終總價格。計算是正確的,但變量似乎沒有在模態退出/模態彈出窗口中清除。這意味着,每一個模態在第一個模態之後都會將它們(正確的)計算添加到前面的模態中,而不是從零開始,因爲我試圖做到這一點。.js當等於0時變量不會返回零。

我已經在.js源代碼中設置了斷點,它告訴我即使在數字傳回之後,在它們上面設置爲零,.js數字也是不會調零的。

這裏是一個圖片設置第一和第二模態調用來說明這個問題(在兩個右下角的數字):

first modal second modal

這裏是我的html,在情況下,它實際上是一個標籤基於問題:

@{ 
    ViewData["Title"] = "Index"; 
    Layout = "~/Views/_Layout.cshtml"; 
} 
<div class="col-sm-3">&nbsp;</div> 
<div class="panel col-sm-6 col-xs-12"> 
    <div class="panel-title text-center" style="padding-top:20px;"> 
     <h3 style="font-weight:bolder">Cart Contents</h3> 
     <img src="/img/cart.png" style="height:10%;width:10%;padding-bottom:5%;" /> 
    </div> 
    <div class="text-center" style="padding-top:10px;"> 
     @{ 
      Dictionary<string, object> cart = Context.Session.Get<Dictionary<string, Object>>("cart"); 
      decimal itemSubTotal = 0; 
      decimal subTotal = 0; 
      decimal itemTaxTotal = 0; 
      decimal taxTotal = 0; 
      decimal salesTaxRate = 0.13M; //m required for a literal 
      decimal orderTotal = 0; 



     } 
     <table class="table table-striped"> 
      <tr style="font-weight:bolder;"> 

       <th class="col-xs-2 text-center">Product Code</th> 
       <th class="col-xs-2 text-center">Qty</th> 
       <th class="col-xs-2 text-center">Item Name</th> 
       <th class="col-xs-2 text-center">Price</th> 
      </tr> 
      @{ 
       if (cart != null) 
       { 

        foreach (var key in cart.Keys) 
        { 
         ProductViewModel item = JsonConvert.DeserializeObject 
         <ProductViewModel> 
         (Convert.ToString(cart[key])); 

         if (item.Qty > 0) 
         { 

          subTotal += item.MSRP * item.Qty; 


          <tr> 
           <td class="col-xs-2 text-center">@item.Id</td> 
           <td class="col-xs-2 text-center">@item.Qty</td> 
           <td class="col-xs-2 text-left">@item.ProductName</td> 
           <td class="col-xs-2 text-center">@string.Format("{0:C}", @item.MSRP)</td> 
          </tr> 

         } 
        } 

        taxTotal += Decimal.Multiply(subTotal, salesTaxRate); 
        orderTotal += subTotal + taxTotal; 
       } 
      } 
     </table> 
     <hr /> 
     <table class="table table-striped"> 
      <tr> 
       <th colspan="4" class="col-xs-4 text-left" style="font-size:large;font-weight:bold;"> 
        Cart 
        Totals 
       </th> 
      </tr> 
      <tr> 
       <td class="col-xs-2 text-right">Subtotal: </td> 
       <td class="col-xs-4 text-left" id="subtotal">@string.Format("{0:C}", @subTotal)</td> 
      </tr> 
      <tr> 
       <td class="col-xs-2 text-right">Tax Total: </td> 
       <td class="col-xs-4 text-left" id="taxtotal">@string.Format("{0:C}", @taxTotal)</td> 
      </tr> 
      <tr> 
       <td class="col-xs-2 text-right">Order Total: </td> 
       <td class="col-xs-4 text-left" id="ordertotal">@string.Format("{0:C}", @orderTotal)</td> 
      </tr> 
      @{ 
       @subTotal = 0; 
       @taxTotal = 0; 
       @orderTotal = 0; 
      } 
     </table> 
     <div class="text-center"> 
      <form asp-controller="Cart" asp-action="AddCart" method="post" role="form"> 
       @if (Context.Session.GetString(SessionVars.User) != null) 
       { 
        <button type="submit" class="btn btn-sm btn-primary" id="modalbtn">Add Cart</button> 
       } 
       &nbsp;<a href="/Cart/ClearCart" class="btn btn-sm btn-success">Clear Cart</a> 
      </form> 
     </div> 
    </div> 
    </div> 

這裏是.js文件,其中計算正在做:

var link = '/GetOrders'; 
    var detailslink = '/GetOrderDetails/'; 
    var subtotal = 0; 
    var finalPrice = 0; 
    var taxAmount = 0; 
    // register modal component 
Vue.component('modal', { 
    template: '#modal-template', 
    props: { 
     item: {}, 
     modalItem: {}, 
     details: [] 
    }, 
}) 
new Vue({ 
    el: '#orders', 
    methods: { 
     GetOrders: function() { 
      var self = this 
      axios.get(link).then(function (response) { 
       self.orders = response.data; 

      }, function (error) { 
       console.log(error.statusText); 
      }); 
     }, 
     loadAndShowModal: function() { 
      var self = this 
      axios.get(detailslink + this.modalItem.id).then(function (response) { 
       self.details = response.data; 
       self.showModal = true; 
      }, function (error) { 
       console.log(error.statusText); 
      }); 
     }, 
    }, 
    mounted: function() { 
     this.GetOrders(); 
    }, 
    data: { 
     orders: [], 
     showModal: false, 
     modalItem: {}, 
     details: [] 
    } 
}); 
function formatDate(date) { 
    var d; 
    if (date === undefined) { 
     d = new Date(); //no date coming from server 
    } 
    else { 
     var d = new Date(Date.parse(date)); // date from server 
    } 
    var _day = d.getDate(); 
    var _month = d.getMonth() + 1; 
    var _year = d.getFullYear(); 
    var _hour = d.getHours(); 
    var _min = d.getMinutes(); 
    if (_min < 10) { _min = "0" + _min; } 
    return _year + "-" + _month + "-" + _day + " " + _hour + ":" + _min; 
} 


function calcLinePrice(qtySold, msrp) 
{ 

    var linePrice = qtySold * msrp; 
    subtotal += linePrice; 
    finalPrice += linePrice; 
    return linePrice.toFixed(2); 

    finalPrice = 0; 
    subtotal = 0; 

} 

function calcSubtotal() 
{ 

    return subtotal.toFixed(2); 
    subtotal = 0; 

    finalPrice = 0; 
    subtotal = 0; 
} 

function calcTax() { 

    taxAmount = finalPrice * 0.13 

    return taxAmount.toFixed(2); 
    taxAmount = 0; 

} 


function calcFinalPrice() { 
    var total = 0; 
    total = finalPrice + taxAmount; 
    return total.toFixed(2); 

    finalPrice = 0; 
    subtotal = 0; 

} 

正如您所看到的,我在每次計算中將finalTotal和小計歸零以試圖解決這個問題。看起來,不管我做什麼,他們拒絕任何東西,但頁面重新加載。任何幫助?

編輯:我試圖在錯誤的地方歸零。我解決了這個通過改變我的calcFinalPrice()函數(即最後一個函數),以這樣的:

function calcFinalPrice() { 
    var total = 0; 
    total = finalPrice + taxAmount; 
    taxAmount = 0; 
    subtotal = 0; 
    finalPrice = 0; 
    return total.toFixed(2); 


} 
+0

檢查瀏覽器開發者工具控制檯(這是調試101) - 你應該看到一條消息,告訴你你做錯了什麼 - 提示,它與函數中的返回語句的地址是 –

+1

無代碼將在當前上下文中的'return'語句後執行 – MysterX

+0

@JaromandaX我知道,我正在使用它來查找.js中變量的值,以確認它是.js問題而不是標籤問題。感謝提示,我沒有意識到js函數不會繼續在內部處理過去的回報。 – Jaeriko

回答

1

您重新分配「0」變量之前返回。重新分配值後使用返回值。

+0

沒錯,但是如果要清除下一個模態的唯一方法是在返回之前將其歸零,我該如何傳回計算出的值?我試着創建一個持有者變量,我在函數的開始處將其置零,但它現在對持有者變量只有相同的問題。 – Jaeriko

+0

而不是使用全局變量,使用道具,這將有所幫助。 :) –