Verify if a string contains a substring.

1,366 views
Skip to first unread message

Roger Delisle

unread,
Oct 6, 2016, 3:44:41 PM10/6/16
to KnockoutJS
It's my second day using Knockout and I need some help.

In my ko if: I would like to compare if a value is within a variable.
If the task exist in the listOfTasks it will display a checkbox.

I'm only able to make it work if its a direct compare. listOfTasks === task

 <td data-bind="text: listOfTasks"></td>

 
<!-- ko if: listOfTasks.contains(task)   -->

 
<td><input id="taskCheckbox" type="checkbox" name="items[]" value=""   /></td>
 
<!-- /ko -->

Thanks for your help.

Roger

Puneet Monga

unread,
Oct 6, 2016, 7:00:19 PM10/6/16
to KnockoutJS
Hi Roger,

I think you can achieve this by the following:

<td data-bind=" visible:listOfTasks.indexOf(task) !== -1"><input id="taskCheckbox" type="checkbox" name="items[]" value=""   /></td> (This will hide the element but its still part of the markup)

or by

<td data-bind=" if:listOfTasks.indexOf(task) !== -1"><input id="taskCheckbox" type="checkbox" name="items[]" value=""   /></td> (This section will not appear in the markup if the condition is met)

Reference:

Thanks,
Puneet Monga

Disclaimer: I have not used KO much but my answer is based on whatever I have read. Please excuse my typos.

Roger Delisle

unread,
Oct 7, 2016, 12:58:20 PM10/7/16
to knock...@googlegroups.com

Hi Puneet,

 

These examples works fine.

 

Thanks for your help.

 

Roger

 

--
You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/hAAVozA70Yc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages