I am new in knockout.js, I have tried to add and remove observable to an observableArray. I just take the first observable from the array and assign it to an input element to track the changes from the array. i.e.) When I add some observable to the observableArray it needs to be reflect in the input element.
Please refer my code below
HTML:
B[0] Value: <input data-bind="value: b"/>
<br/><br/>
<table>
<thead>
<tr>
<th>A</th><th>B</th>
</tr>
</thead>
<tbody data-bind="foreach: result">
<tr>
<td><input data-bind="value: a()"/></td>
<td><input data-bind="value: b()"/></td>
</tr>
</tbody>
</table>
<br/>
<button data-bind="click: add">Add</button>
<button data-bind="click: remove">Remove</button>
JS:
function generate(data) {
self = this;
self.a = ko.observable(data.a);
self.b = ko.observable(data.b);
}
var arr = [{a:1, b:2}, {a:2, b:3}, {a:3, b:4}]
var obsarr = $.map(arr, function(data){ return new generate(data); });
var obsArray = {
result : ko.observableArray(obsarr),
b: ko.observable(obsarr[0].b)(),
add: function(){
var obj = new generate({a:4, b:4});
obsArray.result.splice(0, 0, obj);
},
remove: function(){
obsArray.result.splice(0, 1);
}
}
ko.applyBindings(obsArray);
But when I adding the observable it doesn't reflect to the input and when I change the input value after adding observable, it reflect to the second position (B1) of the array.
Is there anything I missed or I have implement anything wrong?
Please refer the JSFiddle for the above.
Thanks in advance :-)
Updated:
Two directional changes: Two-Directional ObservableArray