There are many javascript errors present in the code.
I have forked the jsFiddle. Since there are other elements present in the view, which are not present in the view model, the errors for the same show up. I have deliberately left it like that, so that you can continue from where you had stopped. The basic idea on how to achieve it is present.
The jsFiddle is present http://jsfiddle.net/rupesh_kokal/k7LHu/3/
Below is the View (or html):
<table>
<tbody data-bind="template: {name: 'budgets', foreach: BudgetLine}">
</tbody>
</table>
<script id='budgets' type="text/html">
<tr>
<td>
<input type="text" data-bind="value:Title"></input>
</td>
<td>
<input type="text" data-bind="value:Amount"></input>
</td>
<td>
<span data-bind="text:Modified" />
</td>
<td>
<select data-bind="options:$root.Account,optionsCaption:'select Account', optionsText:'Title', OptionsValue:'Id', selectedOptions:Account().Id",size="5", multiple="true"></select>
</td>
<td>
<span data-bind="text:CostCenterId"></span>
</td>
<td>
<select data-bind="options:$root.Costcenter,optionsCaption:'cost center', optionsText:'Title', optionsValue:'Id', value:CostCenterId "></select>
</td>
<td>
<span data-bind="text:CostCenter().Company().Title"></span>
</td>
<td>
<input type="button" data-bind="click: $root.deleteItem" value="Delete" />
<input type="button" data-bind="click: $root.saveItem" value="save" />
</td>
</tr>
</script>
Below is the view model:
function ModViewModel() {
var self = this;
self.Accounts = ko.observableArray([
{
Id: 1,
Title: "Account #1"},
{
Id: 2,
Title: "Account #2"},
{
Id: 3,
Title: "Account #3"},
{
Id: 2,
Title: "Account #1"}
]);
self.BudgetLine = ko.observableArray([]);
self.BudgetLine.push(new BudgetLineItem(1, "Budget #1", [1, 2], self));
self.BudgetLine.push(new BudgetLineItem(2, "Budget #2", [1, 2], self));
}
function BudgetLineItem(id, title, accountIds, parentModel) {
var self = this;
self.Id = id;
self.Title = title;
self.Account = ko.observableArray([]);
if (accountIds.length > 0) {
for (var i = 0, len = accountIds.length; i < len; i++) {
self.Account.push(new AccountLineItem(accountIds[i], parentModel));
}
}
else {
self.Account.push(new AccountLineItem(accountIds, parentModel));
}
}
function AccountLineItem(id, rootModel) {
var self = this;
self.Id = id;
self.Value = ko.computed(function() {
return rootModel.Accounts()[self.Id];
});
}
ko.applyBindings(new ModViewModel());