简体   繁体   English

使用下拉选择在KnockoutJS中使用默认值填充文本框

[英]Using drop down selection to populate a text box with a default in KnockoutJS

I have a fairly simple order creation form on a back office app I'm building. 我在我正在建立的后台应用程序上有一个相当简单的订单创建表单。 The scenario I cannot figure out is under the "Order lines" section of the form. 我无法弄清楚的情况是在表单的“订单行”部分下。 I want it so that you click add row the row appears which contains a drop down containing all the products, then 2 text boxes for quantity and price. 我想要它,以便您单击添加行,出现的行包含一个包含所有产品的下拉列表,然后是2个文本框,用于表示数量和价格。 I want it so that when a product is selected, that products price is set as the default value in the Price text box, but the use is able to change it still. 我想要它,以便在选择产品时,将产品价格设置为价格文本框中的默认值,但是使用仍然可以更改它。

So far I have everything in- you can add rows, you can select the part- the only piece i cannot figure out how to do correctly is to populate the default price. 到目前为止,我拥有所有功能-您可以添加行,可以选择零件-我无法弄清楚如何正确执行的唯一操作是填充默认价格。 So a cut down version of my knockout viewModel looks like this; 因此,我的剔除viewModel的简化版本如下所示:

viewModel = {
        Parts : ko.observableArray(initialData.Parts),
        Sale : ko.observable(initialData.Sale),
        SaleLines : ko.observableArray(initialData.SaleLines),
        addRow: function() {
                this.SaleLines.push({ Id: "00000000-0000-0000-0000-000000000000", SalePrice : 0.00, Qty : 1, PartId: "" });
                $("select").combobox({
                    selected: function (event, ui) {
                        $(ui.item.parentNode).change();
                    }
                });
        }, 
        removeRow: function (r) { 
            this.SaleLines.remove(r); 
        }
    }

The Sale lines are rendered out through a template like this; 销售线通过这样的模板呈现出来;

<script type="text/html" id="saleLineTemplate"> 
    <tr>
        <td>
            <select data-bind='options: viewModel.Parts, optionsText: "Description", optionsCaption: "Select...", optionsValue: "Id", value: PartId, uniqueName: true' class="mustPopulateDropdown"></select>
        </td>
        <td>
            <input data-bind="value: Qty, uniqueName: true" class="required number"/>
        </td>
        <td>
            <input data-bind="value: SalePrice, uniqueName: true" class="required number"/>
        </td>
        <td>
            <a href="#" data-bind="click: function() { viewModel.removeRow($data) }">Delete</a>
        </td>
    </tr>
</script>

The actual Parts collection comes from the backend MVC, and is passed as a List with PartDTO just having Id, Description and Price. 实际的Parts集合来自后端MVC,并作为List传递,PartDTO只有Id,Description和Price。

I just cannot think of the correct way to do this- i figured I maybe make the Id field of each SaleLine observable when I create it then somehow make it update the SalePrice on update, but just can't think how to implement it? 我只是想不出正确的方法来做到这一点 - 我想我可能会在创建它时使每个SaleLine的Id字段可观察,然后以某种方式让它在更新时更新SalePrice,但是却想不到如何实现它?

Thanks in advance for your help! 在此先感谢您的帮助!

How about something like this: http://jsfiddle.net/rniemeyer/VLVuC/ 这样的事情怎么样: http : //jsfiddle.net/rniemeyer/VLVuC/

Basically, make it so the "value" of the dropdown is a "SelectedPart" observable that is set to the corresponding Part object: 基本上,使下拉菜单的“值”是可观察到的“ SelectedPart”,该值设置为相应的Part对象:

<select data-bind='options: viewModel.Parts, optionsText: "Description", optionsCaption: "Select...", value: SelectedPart, uniqueName: true' class="mustPopulateDropdown"></select>

Then, subscribe to the SelectedPart changing and set your SalePrice based on SelectedPart().Price. 然后,订阅SelectedPart的更改,并根据SelectedPart()。Price设置您的SalePrice。

addRow: function() {
    var newRow = {
        Id: "00000000-0000-0000-0000-000000000000",
        Qty: ko.observable(1),
        SalePrice: ko.observable(0),
        SelectedPart: ko.observable()
    };

    newRow.SelectedPart.subscribe(function() {
        this.SalePrice(this.SelectedPart() ? this.SelectedPart().Price : 0);
    }, newRow);

Whenever, the dropdown changes on a row, then the SalePrice will be defaulted to the new selected product's price (unless they pick the "Select..." line). 每当下拉列表连续更改时,SalePrice将默认为新选择产品的价格(除非他们选择“ Select ...”行)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM