Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
grid have price, qty column. i need to calculate the total ie. price * qty. Its working new row only. but am fetching the data from web service and loaded it into the grid. now am chaning the qty value but the calculation is not working. its working adding a new row. Please help..

What I have tried:

<script>

        var quotationViewModel = function (quotations) {
            var self = this;

            self.quotations = ko.observableArray(ko.utils.arrayMap(quotations, function (item) {
                return new Item(item);
            }));

            self.addquotation = function () {
                self.quotations.push(new Item());

                UIValidation();
            };

            self.removequotation = function (quotation) {
                self.quotations.remove(quotation);
            };
            self.savequotation = function () {
                return ko.utils.stringifyJson(self.quotations);
            }
            self.grand_total = ko.computed(function () {
                var total = 0;
                for (var p = 0; p < self.quotations().length; ++p) {
                    total += self.quotations()[p].total();
                }
                return total.toFixed(2);
            });

            self.Tax_Total = ko.computed(function () {
                var tax = 0;
                for (var p = 0; p < self.quotations().length; ++p) {
                    tax += self.quotations()[p].tax_calc();
                }
                return tax.toFixed(2);
            });

            self.grandTax = ko.computed(function () {
                var tax = 0, sum = 0, totalsum = 0;
                for (var p = 0; p < self.quotations().length; ++p) {
                    tax += self.quotations()[p].tax_calc();
                    sum += self.quotations()[p].total();
                    totalsum = tax + sum;
                }
                $('#grandTax').val(totalsum.toFixed(2));
                $('#totalAmt').val(totalsum.toFixed(2));
                $('#TotalVat_Amt').val(tax.toFixed(2));
                return totalsum.toFixed(2);
            });

            function Item(item) {
                var self = this;
                self.P055_ID = ko.observable('');
                self.itemCode = ko.observable('');
                self.itemDesc = ko.observable('');
                self.packing = ko.observable();
                self.VatCode = ko.observable();
                self.VatDesc = ko.observable();
                self.taxID = ko.observable();
                self.tax_perc = ko.observable(0);
                self.quantity = ko.observable(0);
                self.rate = ko.observable(0);
                self.remarks = ko.observable();

                for (var k in item)
                    self[k] = ko.observable(item[k]);

                self.total = ko.dependentObservable(function () {
                    return ((self.quantity() * self.rate()));

                }).bind(self);

                self.tax_calc = ko.dependentObservable(function () {
                    return ((self.tax_perc() * self.total()) / 100);

                }).bind(self);
                self.Tax_Total = ko.observable();

            }
        };

        var quotationModel = new quotationViewModel(@Html.Raw(@Model.gridQuotationDetails));
        ko.applyBindings(quotationModel, document.getElementById('divQuotationDetails'));

    </script>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900