3

I need to calculate transaction totals (total price and tax etc) when entering data into SP2013. The following JS code works very well in the New and Edit views but when I try and implement in a SP list view, even using CSR, I cannot get the values to update dynamically.

I have scoured the forums and have successfully implemented color coding using JS Link but I cannot get any field to dynamically update the calculation. I have tried calculated columns but these only populate on submitting the record, not as the user is completing the List View form fields.

    $(document).ready(function(){
        fQuantity = $("input[id*='Quantity']");
        fTaxPrice = $("input[id*='TaxPrice']");
        fUnitPrice = $("input[id*='UnitPrice']");
        fTotalPrice = $("input[id*='TotalPrice']");
        fTaxRate = $("select[id*='LookupPurchaseTax'] option:selected");
        fPurchaseTax = $("select[id*='LookupPurchaseTax']");
        fTaxInclusive = $("select[id*='TaxInclusive']");
        $(fQuantity).val("10"); 
        $(fUnitPrice).val("1.20"); 
        $(fQuantity).change(function() {
            CalcPriceValues($("select[id*='LookupPurchaseTax'] option:selected").text()); 
        });
        $(fUnitPrice).change(function() {
            CalcPriceValues($("select[id*='LookupPurchaseTax'] option:selected").text()); 
        });
        $(fPurchaseTax ).change(function() {
            CalcPriceValues($("select[id*='LookupPurchaseTax'] option:selected").text()); 
        });
        $(fTaxInclusive).change(function() {
            CalcPriceValues($("select[id*='LookupPurchaseTax'] option:selected").text()); 
        });
    });


    function CalcPriceValues(sTaxCode) {
        $.ajax({
                url : "/_api/web/lists/getByTitle('Purchase Tax Rates')/items?$select=Title,TaxRate&$filter=Title eq '" + sTaxCode + "'",
                method : "GET",
                headers : {"accept" : "application/json;odata=verbose"},
                success : function(data) {
                    if (data.d.results.length > 0) {
                        dTaxRate = parseFloat(data.d.results[0].TaxRate);
                        dTotalPrice = (parseFloat($(fQuantity).val()) * parseFloat($(fUnitPrice).val()));
                        dTotalPrice = Math.floor(dTotalPrice*100)/100;
                        switch ($("select[id*='TaxInclusive'] option:selected").text()) {
                            case "Inclusive":
                                dTaxPrice = (dTotalPrice - (dTotalPrice / (1 + dTaxRate)));
                                dTaxPrice = Math.floor(dTaxPrice*100)/100;
                                break;
                            case "Exclusive":
                                dTaxPrice = (dTotalPrice * dTaxRate);
                                dTaxPrice = Math.floor(dTaxPrice*100)/100;
                                dTotalPrice = dTotalPrice + parseFloat($(fTaxPrice).val());
                                break;
                            case "No Tax":
                                dTaxPrice = "0";
                                break;
                        }
                        $(fTaxPrice).val(dTaxPrice.toFixed(2));
                        $(fTotalPrice).val(dTotalPrice.toFixed(2));
                    }
                },
                error: function (data) {
                    alert("Error: " + data);
                }
        });
    }

Thanks for any help.

  • Do you mean your users are using the Quick Edit View? What is the CSR code you tried? The above is only for Forms with a single ListItem displayed – Danny '365CSI' Engelman Jun 3 '17 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.