Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockoutjs template foreach, special first item

So i have been busting my brain figuring this out. I have a foreach, spitting out templates, and i want the first element to have a special attribute. So far solutions i have found havent worked.

This is the foreach:

<h3 class="question">Geografi</h3>   
        <p class="answer" data-bind="template: { name: 'geographyTmpl', foreach: geographyList,  templateOptions: { selections: selectedGeographies } }"></p>

This is the template:

<script id="geographyTmpl" type="text/html">
<input class="geoCheckList" validate="required:true, minlength:2" name="geoCheckList[]" type="checkbox" data-bind='value: $data, attr: { id: "Geo"+$index()},checked: $root.selectedGeographies' />
<label data-bind="text: $data, attr: { 'for': 'Geo'+$index()}"></label>

And i want to add: "validate="required:true, minlength:2" to the first element.

What do i need to do?

If it helps, its for jQuery validation.

like image 925
Frederik T Avatar asked Mar 11 '13 13:03

Frederik T


1 Answers

check my answer for another question about the first element in KnockoutJS foreach

Skip item in foreach knockout js array?

<div data-bind="text: ItemsArray[0].someProperty"></div>
<div data-bind="foreach: ItemsArray">
<!-- ko if: $index() == 0 -->
     <div data-bind="text: someProperty"></div>
 <!-- /ko -->
<!-- ko if: $index() != 0 -->
    <div data-bind="text: anotherDifferentProperty"></div>    
 <!-- /ko -->
</div>
like image 183
ebram khalil Avatar answered Nov 17 '22 06:11

ebram khalil