Reputation: 4985
Is there a way to check if an array contains a certain value, using knockoutJS, within my HTML?
I have the following checkbox:
<td><input type="checkbox" name="group" data-bind="checked: $parent.name in groupList" /></td>
It would be nice if a certain statement inside my data-bind
attribute ($parent.name in groupList
) would work, but obviously it doesn't. With twig it's easy:
{% if myVar is in_array(array_keys(someOtherArray)) %}
But I cannot find a way to do this with Knockout JS. groupList
contains an array with names and I would like to check if it contains a certain name. If it does, the checkbox needs to be checked, else not.
Upvotes: 2
Views: 2299
Reputation: 7641
You can use indexOf of an observable array:
data-bind="checked: groupList().indexOf(ko.unwrap($parent.name)) !== -1"
Upvotes: 4