Options Binding in Drop-Down List Using Knockout

Today's article explains Options Binding with a Drop-Down List using Knockout.

Introduction

In my previous article I explained:

Today's article explains Options Binding with a Drop-Down List using Knockout.

Step 1

First of all you need to add an external Knockout js file into your application, you can either download it from the internet or can download my application that is available at the begining of this article in Zip Format and then can use the file attached with this Zip file.

After downloading the file you need to call it in the head section of your application.

<head runat="server">

    <title></title>

    <script src="knockout-2.3.0.js"></script>

</head>

Step 2

Now we can work on our application. First we will work on the ViewModel. For this you need to add a Script tag under the Body Section and then need to add this code:

        <script type="text/javascript">

            function Car(name, price) {

                this.carName = name;

                this.price = price;

            };

 

            function x() {

                availableCars = ko.observableArray([

                    new Car("i10", 450000),

                    new Car("120", 700000),

                    new Car("Verna", 1200000)

                ]);

                selectedCar = ko.observable();

            };

            ko.applyBindings(new x());

        </script>

Here first I had declared a function named Car, in this function carName and price are declared.

Then another function that is the main function is named "x()" is declared, in this function an Observable and an Observable Array is declared.

In function x() an array is declared that is made Observable. In this array some carName with their Price is declared.

Then an Observable is created named selectedCar.

At the end binding is applied to the function "x()."

Step 3

Now we will work on the view of our application. Write this code in the view part of your application:

    <div>

    <label>Choose Your Car</label>

    <select data-bind="options: availableCars, optionsText: 'carName', value: selectedCar, optionsCaption: 'Choose...'"></select>

    <br />

    <br />

    <div data-bind="visible: selectedCar">

       Choosed Car is of Rs

       <span data-bind="text: selectedCar() ? selectedCar().price : 'unknown'"></span>.

    </div>

    </div>

Here I used a Drop Down List that bound to the avilableCars array. This List will show the Car Name that is provided through the carName. It will hold the value of the selected by using the selectedCar Observable.

Then a Div is created that will be visible only when the user selects a car from the available options.

This div contains a span that will show the price of the car that is selected by the user.

Now our application is ready to be debugged.

The complete code of this application is as follows:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script src="knockout-2.3.0.js"></script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <label>Choose Your Car</label>

    <select data-bind="options: availableCars, optionsText: 'carName', value: selectedCar, optionsCaption: 'Choose...'"></select>

    <br />

    <br />

    <div data-bind="visible: selectedCar">

       Choosed Car is of Rs

       <span data-bind="text: selectedCar() ? selectedCar().price : 'unknown'"></span>.

    </div>

    </div>

        <script type="text/javascript">

            function Car(name, price) {

                this.carName = name;

                this.price = price;

            };

 

            function x() {

                availableCars = ko.observableArray([

                    new Car("i10", 450000),

                    new Car("120", 700000),

                    new Car("Verna", 1200000)

                ]);

                selectedCar = ko.observable();

            };

            ko.applyBindings(new x());

</script>

    </form>

</body>

</html>

Output

First of all this type of output window will be shown on your screen.

options binding using knockout1

Now if I click on the drop down then all the options will be shown in the list.

options binding using knockout2

Now I am selecting an option from the available ones and you will see that it's price will be shown automatically.

options binding using knockout3