Displaying for loop results only once

43 views
Skip to first unread message

mostwanted

unread,
Jan 18, 2020, 6:00:04 PM1/18/20
to web2py-users
In my quotation system I have come up with a way to remind users of UN-deleted quotations. This helps them from creating new quotations on top of old quotations.
How it works is that when a company has been quoted its name is added to a list called the control list and a selected company's name is checked against this control list, if it is in the control list it will mean that it has been quoted and a blue badge titled quoted will be displayed next to the company's name. The problem I am facing now is that if the company has several items quoted under it it means that its name will appear several  times in the control list, this causes the green badge to appear as many times as the company name appears in the control list, I want avoid this, is there a way to get the badge to appear only once regardless of the number of times the company name appears in the control list?

CONTROLLER
def registeredClients():
    bookings
=db(db.Client_Details).select(db.Client_Details.ALL, orderby=db.Client_Details.company)
    quotes
=db(db.quotation).select()
   
return locals()

CSS
.disc
{
border
: solid 2px transparent;
border
-radius: 50%;
width
: 40px;
padding
: 5px;
}

.blue
{
background
-color: blue;
color
: white;
font
-weight: bold;
font
-size: x-small;
}

VIEW
{{for bookings in bookings:}}

{{for q in quotes:}}
{{quoted=[q.customer.company]}}
<span class="disc {{='blue' if bookings.company in quoted else 'hidden'}}">Quoted</span>
{{pass}}

{{pass}}

Please help!!!

Regards;

Mostwanted

Tim Nyborg

unread,
Jan 21, 2020, 7:26:35 AM1/21/20
to web2py-users
Simplest fix: use "any" instead:

{{for bookings in bookings:}}

 
{{if any(bookings.company in q.customer.company for q in quotes):}}

   
<span class="disc {{='blue' if bookings.company in quoted else 'hidden'}}">Quoted</span>
 
{{pass}}

{{pass}}


To avoid all this looping, you could have a separate query join the two tables and count the matches.

mostwanted

unread,
Jan 23, 2020, 9:03:30 AM1/23/20
to web2py-users
Thanks alot Tim, this is all i did:

{{for bookings in bookings:}}
<span class="disc {{='blue' if any(bookings.company in q.customer.company for q in quotes) else 'hidden'}}">Quoted</span>
{{pass}}
The red line above replaced alot of clearly unnecessary code!

Regards;

Mostwanted
Reply all
Reply to author
Forward
0 new messages