So for example, is something like this possible, perhaps using ajax calls, so that the latter field populates properly?
question: |
Where do you live?
fields:
- Country: country
input type: combobox
code: countries_list()
- State: state
input type: combobox
code: states_list(country)
Docassemble customization is based around server-side Python, not client-side JavaScript, so there is no built-in system for creating screens like this. The standard way to do this in Docassemble would be to have two question
s, one for the country and one for the state, which would be on separate screens. The code for that would be simple.
There is an example in the documentation that uses check in
along with background_response('refresh')
to regenerate the screen when the value of an input element changes. This works ok if there are just two fields on the screen; otherwise you need to adjust for the fact that refreshing the screen will wipe out the user's answers to the other fields.
Refreshing the screen is overkill just to update the values in a <select>
. Another way you could accomplish this is to write a jQuery event handler that listens for a change
on the country field and uses action_call()
to retrieve a list of states from Python. (It would be best to avoid using the "combobox" and use <select>
instead, because the "combobox" comes from an add-on package and it doesn't act the same way as a native input element.) The action in Python would use json_response()
to return the list of states as a JSON object, and your JavaScript callback function would use that to substitute a new set of <option>
elements inside the <select>
.