Why is my if/else statement not working

48 views
Skip to first unread message

mostwanted

unread,
Sep 13, 2019, 3:31:23 AM9/13/19
to web...@googlegroups.com
This conditional statement should be simple & execute without any issues, I have created a point of Sale system, in a view it shows all the items & their available quantities, what i want is that when a product's quantity goes below 10 the quantity figure should turn red & a please refill message should be displayed but i am not getting this, the if condition is ignored altogether, only the else statement is executed!

VIEW CODE
<style>
   
.refill
   
{
    display
: none;
    color
: red;
    font
-weight: bold;
    padding
: 2px;
   
}
</style>
{{for idx, p in enumerate(products, start=1):}}
   
 
<tr id="soldItems">
     
<script>
        $
(document).ready(function()
                               
{
           
if({{=p.Quantity}} <10)
           
{
            $
('span.quantity').css({'color':'red'});
            $
('.refill').show();
           
}
           
else
           
{
            $
('span.quantity').css({'color':'green'});
            $
('.refill').hide();
           
}
       
});
   
</script>
{{pass}}     
<td><span style="font-weight: bold; font-size: 15px;">{{=idx}}</span></td>
     
<td><span class="quantity" style="font-weight: bold; font-size: 15px;">{{=p.Quantity}}</span> <span class="refill">Please Refill</span></td>
...............
...............
...............

...............
...............
...............


Please assist

Regards;

Mostwanted

Jirawat Kusolpreedee

unread,
Sep 13, 2019, 3:44:27 AM9/13/19
to web...@googlegroups.com
every python code must be enclosed in {{}} also you need a {{pass}} at the end of if-else statement

On Fri, Sep 13, 2019 at 2:31 PM mostwanted <godir...@gmail.com> wrote:
This conditional statement should be simple & execute without any issues, I have created a point of Sale system, in a view it shows all the items & their available quantities, what i want is that when a product's quantity goes below 10 the quantity figure should turn red & a please refill message should be displayed but i am not getting this, the if condition is ignored altogether, only the else statement is executed!

VIEW CODE
<style>
   
.refill
   
{
    display
: none;
    color
: red;
    font
-weight: bold;
    padding
: 2px;
   
}
</style>
{{for idx, p in enumerate(products, start=1):}}
   
 
<tr id="soldItems">
     
<script>
        $
(document).ready(function()
                               
{
           
if({{=p.Quantity}} <10)
           
{
            $
('span.quantity').css({'color':'red'});
            $
('.refill').show();
           
}
           
else
           
{
            $
('span.quantity').css({'color':'green'});
            $
('.refill').hide();
           
}
       
});
   
</script>

     
<td><span style="font-weight: bold; font-size: 15px;">{{=idx}}</span></td>
     
<td><span class="quantity" style="font-weight: bold; font-size: 15px;">{{=p.Quantity}}</span> <span class="refill">Please Refill</span></td>
...............
...............
...............

...............
...............
...............


Please assist

Regards;

Mostwanted

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/c26be28b-585d-452d-a255-6ba9be30f67b%40googlegroups.com.


--
โทร 086-187-5643

Paco Bernal

unread,
Sep 15, 2019, 6:30:35 AM9/15/19
to web...@googlegroups.com


Maybe this can help

<style>
   
.refill {

        color
: red;
        font
-weight: bold;
        padding
: 2px;
   
}

   
.bold15 {
        font
-weight: bold;
        font
-size: 15px;
   
}
   
.red {
        color
: red;
   
}
   
.green {
        color
: green;

   
}
</style>
{{for idx, p in enumerate(products, start=1):}}
 
<tr id="soldItems">

   
<td>
       
<span class="bold15">{{=idx}}</span>
   
</td>
   
<td>
        <span class="bold15 {{='red' if p.Quantity<10 else 'green'}}">{{=p.Quantity}}
</span>
        <span class="refill {{='' if p.Quantity<10 else '
hidden'}}">Please Refill</span>
   
</td>
 
</tr>  
{{pass}}  

Regards

Dave S

unread,
Sep 15, 2019, 3:19:46 PM9/15/19
to web2py-users


On Fri, Sep 13, 2019 at 2:31 PM mostwanted <godir...@gmail.com> wrote:
This conditional statement should be simple & execute without any issues, I have created a point of Sale system, in a view it shows all the items & their available quantities, what i want is that when a product's quantity goes below 10 the quantity figure should turn red & a please refill message should be displayed but i am not getting this, the if condition is ignored altogether, only the else statement is executed!

 

On Friday, September 13, 2019 at 12:44:27 AM UTC-7, Jirawat Kusolpreedee wrote:
every python code must be enclosed in {{}} also you need a {{pass}} at the end of if-else statement


But he's writing JS here.  His problem, I think, is that  $(document).ready only runs once ... when the document status becomes "ready"  It looks like Paco B has a solution.

/dps


mostwanted

unread,
Sep 16, 2019, 1:01:55 AM9/16/19
to web2py-users
Hey Dave, the $(document).ready(function(){}) statement is repeated as many times as as allowed by the for loop, I checked with the Firefox Web Developer-Debugger plugin, its repeated as per the number of elements in the for loop,its just not respecting the the rules of the condition i don't know why!!!

mostwanted

unread,
Sep 16, 2019, 1:08:14 AM9/16/19
to web2py-users
Thank you so much Paco Bernal, I should be thinking the way you do. Your solution solved my problem, I just defined the "show" and "hidden" classes in my css for the refill class because initially the refill span was just appearing regardless of the condition the I realized the hidden class we pass in the codition was not defined in your solution. Nonetheless it works like a charm. Thank you very much.

Regards;

Mostwanted
Reply all
Reply to author
Forward
0 new messages