lante
lante

Reputation: 7336

Get an item from another array with the same index in knockout

The situation is: I have two tables with the same amount of rows and columns (for this explanation i will call them table1 and table2).

Basically I have to show the values from table1 beside the value from the same row and column in table2. eg:

table1:

1 | 2
3 | 4

table2:

5 [1: this value comes from the same row and column from table1] | 6 [2]
7 [3] | 8[4]

Now, I have a matrix (an array, with arrays) with the following structure in knockout:

var vm = header([{ 
    lots: [1, 2], 
    otherLots: [3, 4] 
}, { 
    lots: [5, 6], 
    otherLots: [7, 8] 
}]);

And then I apply this model to some bindings as you see in this fiddle.

The problem, is that $parent.lots[$index].count doesn't return anything as I expected. I also tried with ko.computed and it didn't work.

Also this values are ko.observable so when I modify it in table1, this change should be reflected in table2

Is there a workaround for this requirement to work?

Upvotes: 0

Views: 206

Answers (1)

Chris Ellenburg
Chris Ellenburg

Reputation: 86

It seems you simply aren't calling the underlying observables. See my fork of your fiddle.

http://jsfiddle.net/cellenburg/kWh2V/1/

<p data-bind="text: $parent.lots()[$index()] ? $parent.lots()[$index()].count : ('nothing to show at index ' + $index())"  />

I changed the previous line to call the observable $index() and lots().

Hope this helps.

Upvotes: 1

Related Questions