Search code examples
jsonjsrender

is it possible to have a for loop in JSRender with incremental variable i?


i have template (an html file) with is getting renders with JSON data using JSRender.

My Sample Template is as follows

<ul id="membersList">
  {{for UserConnection}}
        <li>
        <div>
            <a title="{{:Fullname}}">
            <br />
            <img src="sample.png"/><br />
            {{:Fullname}}</a>
        </div>
        </li>
    {{/for}}

My Json data is

[
{
    "ApplicationName": "appone", 
    "Title": "Title one", 
    " UserConnection ": [
        {
            "Id": 210, 
            " email ": " [email protected] ", 
        }, 
        {
            "Id": 0, 
            " email ": " ", 
        },
        {
            "Id": 211, 
            " email ": " [email protected] ", 
        }
    ]
}

];

Now my requirement is the i should hide of div if in particular the 2nd item of the JSON has the its id value as 0.

Is it possibel in JSRender to check some thing which we do in c# like

for (i = 0 ; i < 10 ; i++)
{
  if (userConnection[i] = 0)
      // show the div
  else 
     // dont show

}

i.e. if i can access UserConnection[i] even in JSRender, then i can show or hide the div. Is it possible?


Solution

  • You can look at this example to see how to get to the parent data https://www.jsviews.com/#parentdata

    and there is #index while in the for loop to get access to the current index.

    I think really the answer to your question is to use a helper to construct the div with either display on or off at the time it is created.

    A helper, converter, and customer tags are all general javascript functions that you register and then use.