I have two drop down list. They have foreign key so they're parent-child 
structure. I want user to pick category from first list, which will execute 
onchange event to dynamically update second list.

So far, I can confirm onchange event is working to return a string value by 
calling 'echo' function but Nothing is returned for 'model' function.

I have a stupid submit button called 'Update Model' and I am thinking if we 
can just simply submit the form with onchange event as if user click the 
button.

Any ideas??

<https://lh5.googleusercontent.com/-CSnuTO2l3wc/TnoMWX7FuLI/AAAAAAAAAEY/CxCeva0_CRQ/firstJPG.JPG>

<https://lh5.googleusercontent.com/-5OYHEgfRK5k/TnoMdakVn1I/AAAAAAAAAEg/JLo5YiBQOFM/second.JPG>


DB.PY
--------------------
db.define_table('Category',
    Field('Code', 'integer'),
    Field('Name'))

db.define_table('Model',
    Field('Code', 'integer'),
    Field('Name'),
    Field('Category_ID', db.Category),
    Field('Note', 'text'),
    Field('Image', comment='e.g. Model/abc.jpg'),
    Field('Coding_Image', comment='e.g. Coding/abc.jpg'),
    Field('Unit'))   

Controllers/default.py
-------------------------------------
def index():
    if request.vars.model_name:
        lists = 
db(db.Product.Model_ID==request.vars.model_name).select(db.Product.ALL)
        themodels = 
db(db.Model.id==request.vars.model_name).select(db.Model.ALL)
    else:
        lists = db(db.Product.Model_ID==1).select(db.Product.ALL)
        themodels = db(db.Model.id==1).select(db.Model.ALL)
    
    categories = db().select(db.Category.ALL)
    
    if request.vars.category_name:
        models = 
db(db.Model.Category_ID==request.vars.category_name).select(db.Model.ALL)
    else:
        models = db(db.Model.Category_ID==1).select(db.Model.ALL)
    return dict(lists=lists, categories=categories, models=models, 
themodels=themodels)

def model():
    if request.vars.category_name:
        models = 
db(db.Model.Category_ID==request.vars.category_name).select(db.Model.ALL)
    else:
        models = db(db.Model.Category_ID==1).select(db.Model.ALL)
    return dict(models=models)    
    
def echo():
    return request.vars.category_name


default/index.html
--------------------------------
<form enctype="multipart/form-data" action="{{URL()}}" method="post">
    <select name='category_name' onchange="ajax('echo', ['category_name'], 
'target')">
    {{for category in categories:}}
    <option value="{{=category.id}}" 
    {{=" selected='selected'" if 
str(category.id)==request.vars.category_name else ""}}>
    {{=category.Name}}
    </option>
    {{pass}}
    </select>
    <input type="submit" value='Update Model'> 
    
    <select>
        <option value='a' id='target'>a</option>
    </select>
    
    <select name='model_name' >
    {{for model in models:}}
    <option value="{{=model.id}}" 
    {{=" selected='selected'" if str(model.id)==request.vars.model_name else 
""}}>
    {{=model.Name}}</option>
    {{pass}}
    </select>
    <input type="submit" value='Show prices   '>    
</form>

Reply via email to