I need to set tabindex to the dynamically generated controls using knockout. I databind the attributes in UI level, but it seems not working. Following is the HTML code for the databinding:
<tbody data-bind="foreach: Week1">
<tr class="formFields" style="vertical-align: top;">
<td class="formFields" width="8%" data-bind="text: Day">
</td>
<td class="formFields" width="5%" align="center">
<select class="combobox" data-bind="value:Required, attr:{tabindex: 42 + Date + $index()}" >
<option value="E">Eligible</option>
<option value="O">On</option>
<option value="F">Off</option>
</select>
</td>
<td class="formFields" width="10%" align="center" >
<input class="txtbox" type="text" placeholder="S:" data-bind="value:SetupTime, attr: { tabindex: 43 + Date + $index() }/>
<input class="txtbox" type="text" placeholder="R:" data-bind="value:CloseTime, attr: { tabindex: 44 + Date + $index() }" />
</td>
<td class="formFields" width="10%" align="center">
<input class="txtbox" type="text" placeholder="Min:" data-bind="value:MinHrsPerDay, attr: { title: tabindex: 45 + Date + $index() }"/>
<input class="txtbox" type="text" placeholder="Max:" data-bind="value:MaxHrsPerDay, attr: { title: tabindex: 46 + Date + $index() }"/>
</td>
<td class="formFields" width="6%" align="center">
<input class="txtbox" align="middle" type="text" data-bind="value:MinWorkShift, attr: { tabindex: 47 + Date + $index() }"/>
</td>
</tr>
</tbody>
Here Date is a property of my viewmodel which represents days as 0-6. Since there can be multiple rows, I'm using $index() to calculate the tab index of the control in each row.
But this is what I see at runtime:
<input type="text" data-bind="value:SetupTime, attr: { tabindex: 43 + Date + $index() }" placeholder="S:" class="txtbox" tabindex="43function d(){if(0<arguments.length){if(!d.equalityComparer||!d.equalityComparer(c,arguments[0]))d.H(),c=arguments[0],d.G();return this}b.r.Wa(d);return c}2">
Could someone please help me correct this?
Thanks in Advance
Because your Date
property is an ko.observable
so you need to get its value by calling it as a function, so with Date()
.
So you need to change your bindings to:
data-bind="value:SetupTime, attr: { tabindex: 43 + Date() + $index() }"
You only need to call it with Date()
if you are usign your observable inside an expression like 43 + Date() + $index()
(note the ()
also after the $index
.)
If you directly binding to an observable then you don't need the ()
like in the data-bind="value:Required"
, because in this case KO will automatically "unwraps" the value for you.