Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I have dropdown with productID as value and productName as text.

My problem is when I select particular product from dropdown, it should get other details such as QuantityPerUnit, UnitPrice and UnitsInStock.

I want to perform using knockout.

I tried following for QuantityPerUnit:
<script>
    $(document).ready(function () {
        $('#divheader').append("<h2>Please place an order</h2>");
        $.ajax({
            type: "Post",
            url: "/Order/getData",
            data: {},
            success: function (data) {

                function ReservationsViewModel() {
                    var self = this;

                    self.orderList = ko.observableArray([
                        new orderReservation(data)
                    ]);
                    self.addOrder = function () {
                        self.orderList.push(new orderReservation(data));
                    }
                }

                function orderReservation(data) {
                    var self = this;
                    self.products = ko.observable(data);
                    self.QPU = ko.computed(function () {
                        return self.products().QuantityPerUnit
                    });
                }

                ko.applyBindings(new ReservationsViewModel());
            }
        });
    });

</script>

<table>
    <thead>
        <tr>
            <th>Product Name</th>
            <th>Quantity/Unit</th>
            <th>Unit Price</th>
            <th>Unit in stock</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: orderList">
        <tr>
            <td>
                <select style="width:250px;" id="ddlProduct" data-bind="options: products, optionsValue: 'ProductID', optionsText: 'ProductName', optionsCaption: 'Choose...'"></select>
            </td>
            <td data-bind="text: QPU"></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
<div>
    <button data-bind="click: addOrder">Reserve another order</button>
</div>


Dropdown is getting populated, but on change on dropdown QuantityPerUnit is not available

May i know where I'm going wrong.

Thanks in advance
Posted

1 solution

you have to pass data in observable array like following
C#
<script>
    $(document).ready(function () {
        $('#divheader').append("<h2>Please place an order</h2>");
        $.ajax({
            type: "Post",
            url: "/Order/getData",
            data: {},
            success: function (data) {
 
                function ReservationsViewModel() {
                    var self = this;
 
                    self.orderList = ko.observableArray([
                        new orderReservation(data)
                    ]);
                    self.addOrder = function () {
                        self.orderList.push(new orderReservation(data));
                    }
                }
 
                function orderReservation(data) {
                    var self = this;
                    self.products = ko.observableArray(data);
                    self.QPU = ko.computed(function () {
                        return self.products().QuantityPerUnit
                    });
                }
 
                ko.applyBindings(new ReservationsViewModel());
            }
        });
    });
 
</script>
 
<table>
    <thead>
        <tr>
            <th>Product Name</th>
            <th>Quantity/Unit</th>
            <th>Unit Price</th>
            <th>Unit in stock</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: orderList">
        <tr>
            <td>
                <select style="width:250px;" id="ddlProduct" data-bind="options: products, optionsValue: 'ProductID', optionsText: 'ProductName', optionsCaption: 'Choose...'"></select>
            </td>
            <td data-bind="text: QPU"></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
<div>
    <button data-bind="click: addOrder">Reserve another order</button>
</div></script>
 
Share this answer
 

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