I'm trying to find the most straightforward way to accomplish this task
and I'm not sure what it is.
I have a form with a bunch of fields. But in particular, I have a
SingleSelectField 'Client', and the 'options' for SingleSelectField
'Projects' depends upon what is selected in 'Client'.
The best I have come up with is to make an asynchronous request
"onchange" when the client is selected, the results of which (the html
of the Projects widget) then gets put into a <div></div> target by
innerHTML.
This means that I have to define the whole form using
display_field_for() rather than using the more convenient TableForm,
since I don't know how to embed my <div></div> target within a
TableForm.
I'd like to use AjaxForm, but it requires that you specify the "action"
to be the asynchronous request, and I want the form to submit, in the
normal, synchrounous way, to its "save" method, and for the
asynchronous request to occur only when 'Client' changes.
I imagine I'm missing something simple, and any hints would be
appreciated.
For reference, I'm including the test I've cobbled together for how I
might do this.
(Yes, I'm new at this.) ;-)
Thanks,
Steve
controllers.py
=============================
class remoteselect_fields(widgets.WidgetsList):
client_select = SingleSelectField(label='Client:',
validator=validators.NotEmpty())
submit=widgets.SubmitButton()
remote_select_form = widgets.Form(fields=remoteselect_fields(),
template="testproj.templates.custom")
class Root(controllers.RootController):
@expose(template="testproj.templates.welcome")
def index(self):
return dict()
@expose(template="testproj.templates.remoteselect")
def remoteselect(self):
options=dict(client_select=(("1", "FGM"),("2", "MGN")))
values={}
attrs=dict(client_select=dict(onchange="javascript:get_projects()"))
return dict(form=remote_select_form,
options = options,
values = values,
attrs=attrs)
@expose(allow_json=True)
def get_projects(self):
t = SingleSelectField(label="Project",
validator=validators.NotEmpty())
options=(("1", "General Support"),("2", "Installation"),('3',
"Reports"))
return dict(hello=t.render(value="2", options=options))
========================
The widget template:
========================
<form xmlns:py="http://purl.org/kid/ns#"
name="${name}"
action="${action}"
method="${method}"
options="${options}"
py:attrs="form_attrs">
<h1> Choose a date: </h1>
${display_field_for("client_select")}
<div id="project_div"></div>
${display_field_for("submit")}
</form>
========================
The page template:
========================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://purl.org/kid/ns#"
py:extends="'master.kid'">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"
py:replace="''"/>
<title>Welcome to TurboGears</title>
<script type="text/javascript">
//<![CDATA[
function put_projects(d) {
getElement('project_div').innerHTML = d.hello;
}
function get_projects_error() {
alert("There was an error!");
}
function get_projects() {
d = loadJSONDoc("http://excelsior-b:8088/get_projects");
d.addCallback(put_projects)
}
//]]>
</script>
</head>
<body>
${form(options=options, value=values, attrs=attrs)}
</body>
</html>
==============================
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---