You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At present the choices list should contain tuples: [ ("option-value", "display-text"), (..., ...), ... ]
Can it be expanded to optionally allow a tool-tip as 3rd tuple element which is then rendered as a title attribute in the element: ("option-value", "display-text", "tool-tip") --> <option value="optional-value" title="tool-tip">display-text</option>
The text was updated successfully, but these errors were encountered:
fromwtforms.fieldsimportSelectFieldfromwtforms.widgetsimportSelect, html_params, HTMLStringclassSelectWithOptions(Select):
""" Renders a select field that supports options including additional html params. The field must provide an `iter_choices()` method which the widget will call on rendering; this method must yield tuples of `(value, label, selected, html_attribs)`. """def__call__(self, field, **kwargs):
kwargs.setdefault('id', field.id)
ifself.multiple:
kwargs['multiple'] =Truehtml= ['<select %s>'%html_params(name=field.name, **kwargs)]
forval, label, selected, html_attribsinfield.iter_choices():
html.append(self.render_option(val, label, selected, **html_attribs))
html.append('</select>')
returnHTMLString(''.join(html))
classSelectWithOptionsField(SelectField):
widget=SelectWithOptions()
@propertydefcurrent_choice(self):
ifself.data:
returnnext(choiceforchoiceinself.choicesifint(choice[0]) ==int(self.data))
else:
returnself.choices[0]
defiter_choices(self):
forvalue, label, render_argsinself.choices:
yield (value, label, self.coerce(value) ==self.data, render_args)
defpre_validate(self, form):
ifself.choices:
forv, _, _inself.choices:
ifself.data==v:
breakelse:
raiseValueError(self.gettext('Is Not a valid choice'))
Usage:
Country = SelectWithOptionsField('Country', choices=[(c.id, c.name, {'title': c.name}) for c in Country.query.all()])
Though I agree, a render_option param should be available in the SelectField attributes.
At present the choices list should contain tuples:
[ ("option-value", "display-text"), (..., ...), ... ]
Can it be expanded to optionally allow a tool-tip as 3rd tuple element which is then rendered as a
title
attribute in the element:("option-value", "display-text", "tool-tip")
--><option value="optional-value" title="tool-tip">display-text</option>
The text was updated successfully, but these errors were encountered: