How to access observable element from observable array in knockout.js and change its value


Shashank

I create a ViewModel as:

function PurchaseOrderViewModel() {
    var self = this;
    self.seqNo = ko.observable(1);
    self.barcode = ko.observable('');
    self.expDate = ko.observable('');
    self.importDate = ko.observable('');
    self.name = ko.observable('');
}

and execute the addOrder event on the barcode's keypress event:

    function OrderCollection() {
        var self = this;
        self.ShirtOrder = ko.observableArray([new PurchaseOrderViewModel()]);

        self.addOrder = function (data, event) {
            if (event.keyCode == 13) {
//Here before pushing new object to self.ShirtOrder. I want to access current object and change its expDate and importDate value.

                var _SO = new PurchaseOrderViewModel();
                _SO.seqNo = $("#SOBody > tr").length + 1;
                self.ShirtOrder.push(_SO);
            }
        };
    }

HTML:

<tbody data-bind="foreach: ShirtOrder()" id="SOBody">
   <tr>
      <td>
         <input type="text" value="1" class="req measurment" data-bind="value: seqNo" />
      </td>
      <td>
         <input type="text" class="" data-bind="value: barcode, valueUpdate: 'afterkeydown', 
            event: { keypress: $parent.addOrder }" />
      </td>
      <td>
         <input type="date" class="req measurment" data-bind="value: expDate" />
      </td>
      <td>
         <input type="date" class="req measurment" data-bind="value: importDate" />
      </td>
      <td>
         <input type="text" class="req measurment" data-bind="value: name" />
      </td>
   </tr>
</tbody>

When the Enter key event is pressed in the barcode text box. ImportDate and ExportDate values ​​should change and a new TR should also be created. I cannot change this value.

super cool

Well, you just have to do something like this

View the model:

    function OrderCollection() {
            var self = this;
            self.ShirtOrder = ko.observableArray([new PurchaseOrderViewModel()]);
            self.addOrder = function (data,event) {
                if (event.keyCode == 13) {
                    data.expDate('2014-10-27'); // you get current instance here 
                    data.importDate('2015-01-20');
                    var _SO = new PurchaseOrderViewModel();
                    self.ShirtOrder.push(_SO);
                }
            };
        }

     $(function() {
         ko.applyBindings(new OrderCollection());
    });

type="date"The caveat when using it is that it should always be set using the yyyy-MM-dd format ( W3C standard ).

work here

Related


Typescript Angular - Observable: How to change its value?

Johannes: Maybe I'm missing something. I can't find a simple tutorial on Observable and its syntax. I'm using Angular and I need to call a function (defined in a component) from a service. I read this solution . But I don't know how to change the value in the

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout Observable Array of Observable Array

Craig I have passed a model from .Net to my view model which is an object with several fields and a list, each field has another list. So, an object with a list of lists. In my view, I represent this as some data, with a list of tabs (the first list), and then

Typescript Angular - Observable: How to change its value?

Johannes: Maybe I'm missing something. I can't find a simple tutorial on Observable and its syntax. I'm using Angular and I need to call a function (defined in a component) from a service. I read this solution . But I don't know how to change the value in the

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout.js cannot map observable array from json

last name Unable to map json to observable array. I used the code from the tutorial ( http://learn.knockoutjs.com/ ). function Movie(data) { this.name = ko.observable(data.name); this.description = ko.observable(data.description); this.duration = k

How to bind knockout Observable array length to html element

Raul Montrock Suppose I have the following view model var ViewModel = function() { this.fruits = ko.observableArray(["Apple", "banana", "orange"]); }; ko.applyBindings(new ViewModel()); I want to be able to bind the length of the observable array on the HT

How to bind knockout Observable array length to html element

Raul Montrock Suppose I have the following view model var ViewModel = function() { this.fruits = ko.observableArray(["Apple", "banana", "orange"]); }; ko.applyBindings(new ViewModel()); I want to be able to bind the length of the observable array on the HT

How to bind last index value of observable array in knockout js

what I need to display the latest comment to the user when the front page loads, like after adding a new comment, the new comment is successfully added to the observable array, but I am not able to display the latest comment, I am trying this code and it doesn

Typescript Angular - Observable: How to change its value?

Johannes: Maybe I'm missing something. I can't find a simple tutorial on Observable and its syntax. I'm using Angular and I need to call a function (defined in a component) from a service. I read this solution . But I don't know how to change the value in the

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout.js cannot map observable array from json

last name Unable to map json to observable array. I used the code from the tutorial ( http://learn.knockoutjs.com/ ). function Movie(data) { this.name = ko.observable(data.name); this.description = ko.observable(data.description); this.duration = k