Dropdown lists

90 views
Skip to first unread message

Omi Chiba

unread,
Jan 26, 2015, 3:28:20 PM1/26/15
to web...@googlegroups.com
I have a three dropdown and the value will be dynamically changed using ajax. It's working fine but something is wrong. In second dropdown (id="lead_name"), I specify jQuery('#model_name') but it's actually jQuery('#leadl_name') but then when the value for the second drop changed, the value disappear from the second and third. When I keep the current way (which is wrong name) it's working as expected.... Do you guys know what's wrong?

This is my view

<form enctype="multipart/form-data" action="{{URL()}}" method="post">
               
<tr>
               
<td><select name='model_name' onchange="jQuery('#model_name').empty();
                    ajax
('lead_ajax', ['model_name'], 'lead_name');">
                    {{for model in models:}}
                       
<option value="{{=model.Name}}"
                        {{
=" selected='selected'" if str(model.Name)==request.vars.model_name else ""}}>
                        {{=model.Name}}
                       
</option>
                    {{pass}}
               
</select></td>
               
<td><select id='lead_name' name='lead_name' onchange="jQuery('#model_name').empty();
                    ajax
('block_ajax', ['lead_name'], 'block_name');">
                    {{for lead in leads:}}
                       
<option value="{{=lead.Name}}"
                        {{
=" selected='selected'" if str(lead.Name)==request.vars.lead_name else ""}}>
                        {{=lead.Name}}
                       
</option>
                    {{pass}}
               
</select></td>
                 
<td><select id='block_name' name='block_name'>
                    {{for block in blocks:}}
                       
<option value="{{=block.Name}}"
                        {{
=" selected='selected'" if str(block.Name)==request.vars.block_name else ""}}>
                        {{=block.Name}}
                       
</option>
                    {{pass}}
               
</select></td>
                   
<td></td>
                   
<td></td>
               
<td><input type="submit" value='Submit'></td>
               
</tr>
           
</form>

My controller

def index():
    response
.title='KR Quick Delivery Service'
   
   
if request.vars.model_name:
        lists
= db((db.KR_Product.Model==request.vars.model_name) & (db.KR_Product.Lead==request.vars.lead_name) & (db.KR_Product.Block==request.vars.block_name)).select(db.KR_Product.ALL)
   
else:
        lists
=''
    models
= db().select(db.KR_Model.ALL)
    leads
= db(db.KR_Lead.ModelName == '20').select(db.KR_Lead.ALL)
    blocks
= db(db.KR_Block.LeadName == '01').select(db.KR_Block.ALL)
       
   
return dict(lists=lists,models=models,leads=leads,blocks=blocks)
   
def lead_ajax():
    leads
= db(db.KR_Lead.ModelName==request.vars.model_name).select(db.KR_Lead.ALL)
    result
= ''
   
for lead in leads:
        result
+= "<option value='" + lead.Name + "'>" + lead.Name + "</option>"  
   
return XML(result)
   
def block_ajax():
    blocks
= db(db.KR_Block.LeadName==request.vars.lead_name).select(db.KR_Block.ALL)
    result
= ''
   
for block in blocks:
        result
+= "<option value='" + block.Name + "'>" + block.Name + "</option>"  
   
return XML(result)



screen.png

Derek

unread,
Jan 27, 2015, 12:15:50 PM1/27/15
to web...@googlegroups.com
Well, where do I start? It looks like you are a victim of copy and paste coding. Delete your two functions and start over again.

That said, you are mixing your view with your controller, please don't do that. I would suggest you use this web2pyslice as a starting point. (yes, I wrote it)

Derek

unread,
Jan 27, 2015, 12:19:01 PM1/27/15
to web...@googlegroups.com
What I mean by 'you are mixing your view with your controller' is that you are using your controller to build HTML that will be used as-is. You should never create HTML in your controllers, leave that to the views. The whole point of MVC is that each part has a very specific purpose. Don't create your models in your controllers or views, don't create html in your models or controllers, and don't put logic in models and views.

Omi Chiba

unread,
Jan 27, 2015, 1:04:44 PM1/27/15
to web2py-users
Thank you for your feedback!

I found " onchange="jQuery('#model_name').empty();" this is unnecessary operation for my purpose so it's removed!

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ZnZtTw-HXFc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Vézina

unread,
Jan 27, 2015, 2:13:09 PM1/27/15
to web2py-users
Derek,

I understand your point, but from the testing point of view, the only option remaining for testing view is Selenium HQ which is slow, so if you want to be able to test your controller that depend a lot of the view, what I can only see is to limit as much as possible the view to unpack web2py controller variables packed with everything...

Do I am wrong in thinking that?

Richard

--
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.

Richard Vézina

unread,
Jan 27, 2015, 2:15:21 PM1/27/15
to web2py-users
Also, consider tools like SQLFORM.grid(), it's generate everything...

Richard

Derek

unread,
Jan 27, 2015, 3:42:07 PM1/27/15
to web...@googlegroups.com
You are wrong. If you need to test your views, you would have to use some kind of testing tool like Selenium, of course. However, you should not be generating views within the controllers, they have separate functions. It makes no sense. Besides, what are you going to test, the generation of the html?

Richard Vézina

unread,
Jan 27, 2015, 4:38:49 PM1/27/15
to web2py-users
I would test the fact that html generated work properly, I guess Selenium is the only way, but if I can interact with html object from python with someting like HTMLTestRunner (http://tungwaiyip.info/software/HTMLTestRunner.html)... I never had time to think about test cases worth doing over generated html... Actually, you maybe right that it only allow to see if html get generated or compare html generated to an reference html (mean something like having app serving to test another app to make sure notting get modify on one hand without the other know it... Mean a lot of work merging stuff between repo in orther to maintain test case = a lot of works).

Could you provide an example of good controller and view codes? Model is easier...

In web2py, I am not sure we can really divide each differents part in M-V-C container... Look how you define validators over model field definition (I know old raised point).

Richard

Niphlod

unread,
Jan 27, 2015, 4:49:54 PM1/27/15
to web...@googlegroups.com
testing html generation is one-now-more-than-ever-shrinking-part of testing an app.
In this particular case, you won't have noticed a thing: the important bits were/are played by javascript interaction.

Richard Vézina

unread,
Jan 27, 2015, 5:05:10 PM1/27/15
to web2py-users
Yes, that is another issue...

I am thinking more and more to learn Angularjs more then what I really know or start using Ractive.js that seems have easier learning curve...

web2py component is great, but I feel really limited in developping complex "form" like user interraction for complex data management with them... I have made a few thing, but code get messy rappidly... I feel, angularjs deep linking missing with web2py component... But I may just didn't find how to make it happen with web2py component as well...

TDD may be easier...

Richard

Richard Vézina

unread,
Jan 27, 2015, 5:05:37 PM1/27/15
to web2py-users
TDD may be easier with angularjs...

I would say...

Richard

Niphlod

unread,
Jan 27, 2015, 5:15:23 PM1/27/15
to web...@googlegroups.com
it seems that you're rather "full" of issues and ideas but "empty" on the attempts side.

Richard Vézina

unread,
Jan 28, 2015, 9:39:49 AM1/28/15
to web2py-users
no times... I am alone supporting lab data management for 200+ employees startup... And in my personel life, I buy a new house this year and renovate it from basement to second floors, starting with a new drain around the basement earlier last summer (of course, this is done now)...

:-P

On Tue, Jan 27, 2015 at 5:15 PM, Niphlod <nip...@gmail.com> wrote:
it seems that you're rather "full" of issues and ideas but "empty" on the attempts side.

--

Richard Vézina

unread,
Jan 28, 2015, 10:57:29 AM1/28/15
to web2py-users
Forget the most important, I got my second one in june...

:)
Reply all
Reply to author
Forward
0 new messages