Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery template - else if construct

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>
like image 865
L G Avatar asked Feb 14 '11 06:02

L G


3 Answers

The syntax is correct as per JQuery API. Maybe try removing the '(' parentheses?

{{if Amount == ""}}
   -
{{else balance == ""}}
   *                      
{{else}}
   ${Amount}
{{/if}}
like image 88
The_Butcher Avatar answered Nov 07 '22 23:11

The_Butcher


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}: &quot;${$value}&quot; |
             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

like image 21
Adrien Be Avatar answered Nov 07 '22 23:11

Adrien Be


The correct answer is:

{{else [Condition]}}

Make sure that you import the jquery template js.

like image 3
Michael Avatar answered Nov 08 '22 01:11

Michael