var Foo = function(selected) {
this.id = ko.observable();
this.name = ko.observable();
this.fooItems = ko.observableArray([]);
this.isSelected = ko.computed(function() {
return selected() === this;
}, this);
this.selectedItem = ko.observable('quack');
};
var FooItem = function() {
this.id = ko.observable();
this.selectedItemId = ko.observable();
this.selectedName = ko.observable();
this.remarks = ko.observable();
};
var foosFromDb = [{
id: 1,
name: 'foo1'
}, {
id: 2,
name: 'foo2'
}, {
id: 3,
name: 'foo3'
}, {
id: 4,
name: 'foo4'
}];
var fooItemsFromDb = [{
id: 1,
fooItem: 'fooItem1'
}, {
id: 2,
fooItem: 'fooItem2'
}, {
id: 3,
fooItem: 'fooItem3'
}, {
id: 4,
fooItem: 'fooItem4'
}, {
id: 5,
fooItem: 'fooItem5'
}, ];
var vm = (function() {
var
foos = ko.observableArray([]),
fooItemsList = ko.observableArray([]),
loadFoos = function() {
for (var i = 0; i < foosFromDb.length; i++) {
foos.push(new Foo(selectedFoo)
.id(foosFromDb[i].id)
.name(foosFromDb[i].name));
}
selectFoo(foos()[0]);
},
loadFooItemsList = function() {
for (var i = 0; i < fooItemsFromDb.length; i++) {
fooItemsList.push({
id: fooItemsFromDb[i].id,
name: fooItemsFromDb[i].fooItem
});
}
},
selectedFoo = ko.observable(),
selectFoo = function(item) {
selectedFoo(item);
},
newFoo = function(item) {
var id = foos().length + 1;
var aFoo = new Foo(selectedFoo)
.id(id)
.name('');
foos.push(aFoo);
selectFoo(aFoo);
},
addFooItem = function(foo) {
var itemData = foo.selectedItem(),
item = new FooItem();
item.id(itemData.id);
item.selectedName(itemData.name);
var r = vm.remark();
console.debug("Remark:", r);
item.remarks(r);
foo.fooItems.push(item);
};
return {
foos: foos,
loadFoos: loadFoos,
newFoo: newFoo,
selectedFoo: selectedFoo,
selectFoo: selectFoo,
loadFooItemsList: loadFooItemsList,
fooItemsList: fooItemsList,
remark: ko.observable(),
addFooItem: addFooItem
};
}());
vm.loadFoos();
vm.loadFooItemsList();
ko.applyBindings(vm);
.container {
float: left;
width: 200px;
height: 250px;
border: 1px solid #ccc;
}
.selected {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="container">
<a href="#" data-bind="click: newFoo">New Foo</a>
<table>
<tr>
<th>Foo Id</th>
<th>Foo Name</th>
</tr>
<tbody data-bind="foreach: foos" style="cursor: pointer">
<tr data-bind="click: $root.selectFoo, css: { selected: isSelected }">
<td data-bind="text: id"></td>
<td data-bind="text: name"></td>
</tr>
</tbody>
</table>
</div>
<div data-bind="with: selectedFoo">
<div class="container">
<label>Id</label>
<br />
<input type="text" data-bind="value: id" />
<br />
<label>Name</label>
<br />
<input type="text" data-bind="value: name" />
<br />
</div>
<div class="container">
<label>Foo Item</label>
<br />
<select data-bind="options: $root.fooItemsList,
optionsText: 'name',
value: selectedItem"></select>
<br />
<label>Remarks</label>
<br />
<input type="text" data-bind="value: $root.remark" />
<br />
<a href="#" data-bind="click: $root.addFooItem">Add Foo Item</a>
</div>
<div class="container">
<table>
<thead>
<tr>
<th>FooItem</th>
<th>Remarks</th>
</tr>
</thead>
<tbody data-bind="foreach: fooItems">
<td data-bind="text:selectedName"></td>
<td data-bind="text:$root.remark"></td>
</tbody>
</table>
</div>
</div>
<div style="clear: both"></div>
<pre data-bind="text: ko.toJSON($root.selectedFoo, null, 2)"></pre>