简体   繁体   中英

Knockoutjs observable array of objects binding selector by name

I have an observable array of observable objects called FieldsAndValues 在此处输入图片说明

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?

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

eg

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM