I was trying to use an "else if" statement inside the jquery template block.I have seen in other samples that instead of writing if else, we can just put else.But this is not working for me, its throwing syntax error on the template.can any one help pls?
The acutual way it should work is like
if(Amount == "")
{
-
}
else if(balance == "")
{
*
}
else
{
Amount
}
I have mentioned below the jquery template syntax which i wrote to acheive this.
<script id="MyTemplate" type="text/x-jquery-tmpl">
<tr style="background-color:#EFF3FB"><td onclick="test();">${CardNumber}</td><td>${PostDate}</td>
<td>{{if (Amount == "")}}-
{{else(balance == "")}}*
{{else}}${Amount}
{{/if}}
</td>
<td>${Description}</td></tr>
</script>
The syntax is correct as per JQuery API. Maybe try removing the '('
parentheses?
{{if Amount == ""}}
-
{{else balance == ""}}
*
{{else}}
${Amount}
{{/if}}
I had a similar issue when performing an if statement inside a loop, when doing the if statement on an item of the loop.
Assuming you are having an issue for the same reason (I see that your question includes some td
and tr
so you're probably iterating through items and displaying the their content in a table. Then here is my answer:
<script type="text/x-jquery-tmpl" id="jqTemplate">
<ul>
{{each data.messagetext}}
<li>
Message # ${$index+1}: "${$value}" |
Test result: {{if $value}}exists{{else}}does not exist{{/if}}
</li>
{{/each}}
</ul>
</script>
notice the dollar sign in {{if $value}}
, this is the trick on the regular {{if value}}
if you want to check the field of an item you are iterating on you can do {{if $value.fieldname}}exists{{else}}does not exist{{/if}}
fiddle http://jsfiddle.net/x2Tac/
jquery-template doc http://www.jquerysdk.com/api/template-tag-if
The correct answer is:
{{else [Condition]}}
Make sure that you import the jquery template js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With