form: store jsonp display value earlier, and use it to init the field (#2755)
This commit is contained in:
parent
e6ce4895ec
commit
6cbc19ab78
|
@ -1432,10 +1432,18 @@ class JsonpSingleSelectWidget(Widget):
|
|||
else:
|
||||
value = htmlescape(self.value)
|
||||
r = TemplateIO(html=True)
|
||||
|
||||
attrs = {'id': 'form_' + self.name}
|
||||
r += htmltag('input', xml_end=True, type="hidden", name=self.name, value=value, **attrs)
|
||||
|
||||
attrs = {'id': 'form_' + self.name + '_display'}
|
||||
if value and get_session().jsonp_display_values:
|
||||
key = '%s_%s' % (self.url, value)
|
||||
if key in get_session().jsonp_display_values:
|
||||
attrs['value'] = get_session().jsonp_display_values.get(key)
|
||||
r += htmltag('input', xml_end=True, type="hidden", name=self.name + '_display', **attrs)
|
||||
initial_display_value = attrs.get('value')
|
||||
|
||||
get_response().add_javascript(['jquery.js', 'select2/select2.min.js'])
|
||||
get_response().add_css_include('../js/select2/select2.css')
|
||||
|
||||
|
@ -1522,12 +1530,28 @@ $("#form_%(id)s").change(function() {
|
|||
$('#form_%(id)s_display').val(text);
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
""" % {'id': self.name})
|
||||
|
||||
if initial_display_value:
|
||||
r += htmltext("""
|
||||
$("#form_%(id)s").select2("data", {id: "%(value)s", text: "%(text)s"});
|
||||
""" % {'id': self.name, 'value': self.value, 'text': initial_display_value})
|
||||
|
||||
r += htmltext("""});</script>""")
|
||||
|
||||
return r.getvalue()
|
||||
|
||||
def parse(self, request=None):
|
||||
if request and request.form.get(self.name) and request.form.get(self.name + '_display'):
|
||||
# store text value associated to the jsonp value
|
||||
if not get_session().jsonp_display_values:
|
||||
get_session().jsonp_display_values = {}
|
||||
value = request.form.get(self.name)
|
||||
display_value = request.form.get(self.name + '_display')
|
||||
get_session().jsonp_display_values['%s_%s' % (self.url, value)] = display_value
|
||||
|
||||
return Widget.parse(self, request=request)
|
||||
|
||||
class AutocompleteStringWidget(WcsExtraStringWidget):
|
||||
url = None
|
||||
|
||||
|
|
Loading…
Reference in New Issue