** I have replied the email you sent also **

Well I can only provide you some pointers on how to get this done -
you will have to take it up from there.....

*Note that this code is not tested and maybe incorrect. However I
think the concept  is sound and that really is what you are trying to
figure out*

In your helpers you can use some webhelpers (a python package that
helps with building html form elements) to help with building the
options.

So you need to import these into helpers.py

from webhelpers.html.tags import *

for the table in your html template file (the one rendered to display
the form) you will need something like :-

<table>
<tr><th>country</th><th>state</th></tr>
<tr>
       <td>${h.select('country', selected_values=[],
options=c.available_countries)}</td>
       <td id='states_td'>${h.select('state', selected_values=[],
options=c.available_states)}</td>
</tr>
</table>


in your controller you will need to assign values to
c.available_countries and c.available_states (or whatever you
specified for 'option' in the h.select()) that facilitate populating
the options in the template

i.e. something like

c.available_countries= countryOptions()
c.available_states= stateOptions(country=country) #available states
depend on the country

and those functions will be defined to return the lists of countries
and states (depending on the current country)

def countryOptions():
     return [(1,'USA'), (2,'UK')]

def stateOptions(country=1):
     if country=1:
         return [(1,'Arkansas').......(33,'Texas')]
     else: #country==2!
         return [(1,'UK_State_1'), (2,'UK_State_2')]

This is a very simplified example to show how the state options depend
on which country was selected. You will probably replace the hard
coded lists with another list that is populated using rows returned
from a database call in both functions.

The tricky part is changing the state options displayed when the
country is changed. For that you need to know some ajax.

You need to attach an onclick (or onchange) event to the country
select field that.

1. identifies which country was selected
2. makes an ajax call to a controller action (passing it the country
selected). this action might use a data lookup to determine which
states are in the selected country and returns them to the call back
of the ajax call
3. the call back receives the returned data and then replaces the
current available options for the state selection field to correspond
with the new states returned.

I use the Jquery library. So here is what I would do in the javascript
code using jquery:-

function replace_states(data){
   $('#states_td')[0].innerHTML=data.new_states; // replace the entire
contents of the outer td element with id states_td
}

$(function() {
    $('#country').click(function (event) { // attach the click event
to the country select field
        var country=$(this).val();
        $.post('/controller/get_new_states', {country:country },
            function(data){
                replace_states(data); // replace_states is the call
back function that takes the new states and changes them in the html
form
            },'json');

    });
});


That leaves the controller function get_new_states which returns the
states for the selected country in a dictionary with key new_states:-

Here I am using json to receive and return the data from the browser.
So the controller function will look like so :-

import formencode
from pylons.decorators import jsonify,validate

@jsonify
@validate(schema=formencode.Schema(country=validators.Int()))
def get_new_states(self,country=None):
    country=self.form_result['country']
    states_q=dbsession.query(State).filter
(State.country_country_id==country)
    states={'new_states':h.select('states',  selected_values=[],
options=[(state.state_id, state.state_name) for state in states_q])}
    return states

Here I am using SqlAlchemy and the State and Country tables are in a
1:M relationship on country_id, where country_country_id (FK) in the
State table references country_id (PK) in the Country table

Anyhow this should give you a bit of an idea of how to proceed.

good luck!


On Jul 6, 10:36 am, afrotypa <[email protected]> wrote:
> That is a broad question. Perhaps 2 questions I think really.
>
> 1. How to create a singleselect field in a template?
>
> 2. How to update the database with the value of a selection field when
> the form is submitted?
>
> There is some good pylons documentation for that stuff here :-
>
> http://pylonsbook.com/en/1.0/working-with-forms-and-validators.html
>
> On Jul 4, 11:13 am, sreepathy reddy <[email protected]> wrote:
>
> > how to create drop-down list in pylons and how to assin the drop-down values
> > to mysql tables values
>
> > plz send me the code........plz n tq
> > --
> > sreepathyreddy.m
> > [email protected]
> > 09989456431
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to