In this jsfiddle I have a simple example of what I want to do working. It's all working just fine. But it seems like there has to be a better way to create these arrays with some loops or something. I've been trying all day and haven't succeeded. Can someone tell me if
- it's possible
- what voodoo I need to perform in order for it to work.
Here's the code from the fiddle.
View:
<table>
<tbody>
<tr>
<td></td>
<!-- ko foreach: topvals -->
<td >
<input type="text" data-bind="value: val"/>
</td>
<!-- /ko -->
</tr>
<tr>
<td><input type="text" data-bind="value:sidevals()[0].val"/></td>
<!-- ko foreach: intersections()[0] -->
<td><span data-bind="text: val"></span></td>
<!-- /ko -->
</tr>
<tr>
<td ><input type="text" data-bind="value:sidevals()[1].val"/></td>
<!-- ko foreach: intersections()[1] -->
<td><span data-bind="text: val"></span></td>
<!-- /ko -->
</tr>
</tbody>
</table>
Viewmodel:
function ViewModel() {
this.topvals = ko.observableArray([
{ val: ko.observable(6) },
{ val: ko.observable(1) },
{ val: ko.observable(1) },
{ val: ko.observable(1) }
]);
this.sidevals = ko.observableArray([
{ val: ko.observable(1) },
{ val: ko.observable(1) },
{ val: ko.observable(1) }
]);
this.intersections = ko.observableArray([
[
{ val: ko.computed(function () { return this.topvals()[0].val() * this.sidevals()[0].val(); }, this) },
{ val: ko.computed(function () { return this.topvals()[1].val() * this.sidevals()[0].val(); }, this) },
{ val: ko.computed(function () { return this.topvals()[2].val() * this.sidevals()[0].val(); }, this) },
{ val: ko.computed(function () { return this.topvals()[3].val() * this.sidevals()[0].val(); }, this) }
],
[
{ val: ko.computed(function () { return this.topvals()[0].val() * this.sidevals()[1].val(); }, this) },
{ val: ko.computed(function () { return this.topvals()[1].val() * this.sidevals()[1].val(); }, this) },
{ val: ko.computed(function () { return this.topvals()[2].val() * this.sidevals()[1].val(); }, this) },
{ val: ko.computed(function () { return this.topvals()[3].val() * this.sidevals()[1].val(); }, this) }
]
]);
}
ko.applyBindings(new ViewModel());