Observable Array and Foreach Binding in Knockoutjs

Introduction

 
In one of my previous articles I explained how to use an Observable Property to create dynamic output with Knockoutjs in an ASP.NET Application.
 
This article explains how to use an "Observable Array" and "foreach binding" in Knockoutjs. As I said earlier, the Observable Property changes in the UI whenever an item is added or removed, so this Observable Array will also work like other Observables and will show the instant result of any changes made.
 
Step 1
 
First of all, you need to add Knockout to the ASP.NET Application; for that you can either download it from its home site or you can download my Zip file provided above and then fetch Knockout and use it in your application.
 
After downloading this file go to your application and right-click on it then select "Add Existing Item", then browse to the file where you stored it and add it.
 
knockout1.jpg
 
Now this file will be added to the Solution Explorer of your application, now drag it to the Head Section of your application.
  1. <head runat="server">  
  2.     <title></title>  
  3.      <script src="knockout-2.3.0.js"></script>  
  4. </head>  
Step 2
Now we will work on the ViewModel of our application.
  1. <script>  
  2. function x(car, features) {  
  3.     var self = this;  
  4.     self.name = car;  
  5.     self.feature = ko.observable(features);  
  6. }  
  7.   
  8. function y() {  
  9.     var self = this;  
  10.     self.carModel = [  
  11.         { carName: "I20", price: 600000 },  
  12.         { carName: "Verna", price: 1200000 },  
  13.         { carName: "Duster", price: 800000 }  
  14.     ];  
  15.   
  16.     self.cars = ko.observableArray([  
  17.         new x("Anubhav", self.carModel[1]),  
  18.         new x("Mohit", self.carModel[0])  
  19.     ]);  
  20. }  
  21.   
  22. ko.applyBindings(new y());  
  23. </script>  
In the function x() we create a class that will store the Buyer Name with the Car Selection made by him. Notice that the "feature" property is made observable.
 
After that a new function y() is created in which the Car Name and it's prices are declared under the carModel and after that in self.cars the selection made by the buyer is shown along with his name; self.cars is the Observable Array; that means that it will show the result instantly on the UI.
 
At the end, as always, the Bindings are applied to the y() that will be bound to the View of Knockout.
 
Step 3
 
Our View Model is now complete. We will now work on the View; that means the design part of the application.
  1. <table>  
  2.     <thead><tr>  
  3.         <th>Buyer Name</th><th>Car Name</th><th>Price</th><th></th>  
  4.     </tr></thead>  
  5.     <tbody data-bind="foreach: cars">  
  6.         <tr>  
  7.             <td data-bind="text: name"></td>  
  8.             <td data-bind="text: feature().carName"></td>  
  9.             <td data-bind="text: feature().price"></td>  
  10.         </tr>     
  11.     </tbody>  
  12. </table>  
Here I had used a foreach for the Observable Array.
 
After that in the first <td> I bound the Name of the Buyer and in the next two <td> the Car Name and it's prices are shown. As you can see, in the View Model I made self.feature as Observable so here it should be used as a function to inherit it's value.
 
My complete code is as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication28.WebForm1" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <script src="knockout-2.3.0.js"></script>  
  8.     </head>  
  9.   
  10.     <body>  
  11.         <form id="form1" runat="server">  
  12.             <table>  
  13.                 <thead>  
  14.                     <tr>  
  15.                         <th>Buyer Name</th>  
  16.                         <th>Car Name</th>  
  17.                         <th>Price</th>  
  18.                         <th></th>  
  19.                     </tr>  
  20.                 </thead>  
  21.                 <tbody data-bind="foreach: cars">  
  22.                     <tr>  
  23.                         <td data-bind="text: name"></td>  
  24.                         <td data-bind="text: feature().carName"></td>  
  25.                         <td data-bind="text: feature().price"></td>  
  26.                     </tr>  
  27.                 </tbody>  
  28.             </table>  
  29.             <script>  
  30.             function x(car, features) {  
  31.                 var self = this;  
  32.                 self.name = car;  
  33.                 self.feature = ko.observable(features);  
  34.             }  
  35.   
  36.             function y() {  
  37.                 var self = this;  
  38.                 self.carModel = [  
  39.                     { carName: "I20", price: 600000 },  
  40.                     { carName: "Verna", price: 1200000 },  
  41.                     { carName: "Duster", price: 800000 }  
  42.                 ];  
  43.   
  44.                 self.cars = ko.observableArray([  
  45.                     new x("Anubhav", self.carModel[1]),  
  46.                     new x("Mohit", self.carModel[0])  
  47.                 ]);  
  48.             }  
  49.   
  50.             ko.applyBindings(new y());  
  51.             </script>  
  52.         </form>  
  53.     </body>  
  54.   
  55. </html>  
Output
 
After completing the code run your application, it will look something like this:
 
observable array1.jpg


Similar Articles