c# - knockout 购物车,统计商品的最终价格以及所有商品的小计

标签 c# javascript .net json knockout.js

看看我当前的工作示例:http://jsfiddle.net/6vBsm/1/

如何计算每行的最终价格?

计算方式应为数量 * 价格 - 折扣%

它还应该是动态的,以便数量、价格或折扣的任何更改都会重新计算最终价格。

最后,如何将小计指向新的最终价格而不是基本价格的总和?

当前计算小计的代码:

 // Computed data
    self.totalSurcharge = ko.computed(function () {
        var total = 0;
        for (var i = 0; i < self.SelectedComponents().length; i++)
        total += self.SelectedComponents()[i].Price;
        return total;
    });

最佳答案

你的问题是你的字段不可观察,我使用 ko.mapping 插件自动使事物可观察。

http://jsfiddle.net/keith_nicholas/dzZLW/

所以你的 JavaScript 现在是:-

function ViewModel() {
        var self = this;

        self.Components = [{
            "ID": "1",
                "Name": "Tennis Ball",
                "Description": "Basic Yellow Tennis Ball 9",
                "Quantity": 0,
                "Price": 1.99,
                "Discount": 0.0
        }, {
            "ID": "2",
                "Name": "Hockey Stick",
                "Description": " Premium Carbon Fiber Construction",
                "Quantity": 0,
                "Price": 67.99,
                "Discount": 0.0
        }, {
            "ID": "3",
                "Name": "Cycling Helmet",
                "Description": " For going fast.",
                "Quantity": 0,
                "Price": 226.99,
                "Discount": 0.0

        }];

        self.componentToAdd = ko.observable();
        self.SelectedComponents = ko.observableArray([]);


        // Computed data
        self.totalSurcharge = ko.computed(function () {
            var total = 0;
            for (var i = 0; i < self.SelectedComponents().length; i++)
            {                        
              total += self.SelectedComponents()[i].Price() * self.SelectedComponents()[i].Quantity() *  (100-self.SelectedComponents()[i].Discount())/100;
            }
            return total;
        });


        //Operations
        self.addComponent = function () {        

            var mycopy = {};
            ko.mapping.fromJS(self.componentToAdd(), {}, mycopy);
            self.SelectedComponents.push(mycopy);
        };

    }

你的 html 需要是

<div data-bind="visible: SelectedComponents().length > 0">
    <table class="koSubTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Discount %</th>

                <th>Final Price</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: SelectedComponents">
            <tr>
                <td data-bind="text: Name" style="width:90px;"></td>
                <td data-bind="text: Description" style="width:300px;"></td>
                <td style="width:90px;">
                    <input data-bind="value: Quantity" size="5" />
                </td>
                <td style="width:90px;">
                    <input data-bind="value: Price().toFixed(2)" size="8" />
                </td>
                <td style="width:90px;">
                    <input data-bind="value: Discount" size="5" />
                </td>

                <td style="width:120px;">???</td>
            </tr>
        </tbody>
    </table>
    <div>
        <br>Subtotal $<span data-bind="text: totalSurcharge().toFixed(2)"></span>

    </div>
</div>
<br>
<!--Bind Json List to this drop down-->
<select id="myComponent" data-bind="options: Components, optionsText: 'Name', value:  componentToAdd"></select>
<!--Click on this button-->
<button data-bind="click: addComponent">Add to list</button>

关于c# - knockout 购物车,统计商品的最终价格以及所有商品的小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15103159/

相关文章:

c# - Azure IoT 中心 - 可移植库中的设备注册

c# - 我试图从网站上读取 RSS 提要,但希伯来语字体看起来很乱,我该如何修复它?

javascript - asp.net mvc 如何将 View 模型和输入文本传递给 Controller ​​?

.net - 确定程序集是否是 .NET 框架的一部分

c# - LINQ Where 忽略重音和大小写

c# - 在 C# 中清理/刷新托盘区域

javascript - 在两个 meteor 模板之间推送数据

javascript - Rollup Handlebars 助手

c# - 数据绑定(bind)到附加属性

c# - 如何实现 IComparable<T>?