Carlo Lim
Carlo Lim

Reputation: 35

Knockoutjs observable array of objects binding selector by name

I have an observable array of observable objects called FieldsAndValues enter image description here

I'm binding the values of each object by their index using this

<input type="radio" data-bind="checked: DTO.FieldsAndValues()[0].Value, checkedValue: 'true'" class="separate" name="rdo">Yes

<input type="radio" data-bind="checked: DTO.FieldsAndValues()[0].Value, checkedValue: 'false'" class="separate" name="rdo">No

My question is, is there a way to bind the objects using the LeaveFieldName property as a selector and not their index?

Upvotes: 0

Views: 58

Answers (1)

kasperoo
kasperoo

Reputation: 1584

Isn't it just the case of using foreach binding on that array, and not manually specifying the index of each node?

e.g.

<div data-bind="foreach: DTO.FieldsAndValues">
    <div>
        <input type="radio" data-bind="checked: LeaveField.LeaveFieldName, checkedValue: true" class="separate" name="rdo">Yes
        <input type="radio" data-bind="checked: LeaveField.LeaveFieldName, checkedValue: false" class="separate" name="rdo">No
    </div>
</div>

Plus, even if you bind it, your value will be either true / false, not the IsRegularAndProbiSeparate (etc.). That's what checkedValue binding is for. You even stringify boolean which isn't a good idea, should be just checkedValue: true

Alternatively you could use with binding (with: LeaveField) inside the array output and have access to all properties in each recurrence.

Upvotes: 1

Related Questions