本文整理匯總了Python中webhelpers2.html.HTML.optgroup方法的典型用法代碼示例。如果您正苦於以下問題:Python HTML.optgroup方法的具體用法?Python HTML.optgroup怎麽用?Python HTML.optgroup使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類webhelpers2.html.HTML
的用法示例。
在下文中一共展示了HTML.optgroup方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: select
# 需要導入模塊: from webhelpers2.html import HTML [as 別名]
# 或者: from webhelpers2.html.HTML import optgroup [as 別名]
def select(name, selected_values, options, id=NotGiven, **attrs):
"""Create a dropdown selection box.
* ``name`` -- the name of this control.
* ``selected_values`` -- a string or list of strings or integers giving
the value(s) that should be preselected.
* ``options`` -- an ``Options`` object or iterable of ``(value, label)``
pairs. The label will be shown on the form; the option will be returned
to the application if that option is chosen. If you pass a string or int
instead of a 2-tuple, it will be used for both the value and the label.
If the `value` is a tuple or a list, it will be added as an optgroup,
with `label` as label.
``id`` is the HTML ID attribute, and should be passed as a keyword
argument. By default the ID is the same as the name. filtered through
``_make_safe_id_component()``. Pass None to suppress the
ID attribute entirely.
CAUTION: the old rails helper ``options_for_select`` had the label first.
The order was reversed because most real-life collections have the value
first, including dicts of the form ``{value: label}``. For those dicts
you can simply pass ``D.items()`` as this argument.
HINT: You can sort options alphabetically by label via:
``sorted(my_options, key=lambda x: x[1])``
The following options may only be keyword arguments:
* ``multiple`` -- if true, this control will allow multiple
selections.
* ``prompt`` -- if specified, an extra option will be prepended to the
list: ("", ``prompt``). This is intended for those "Please choose ..."
pseudo-options. Its value is "", equivalent to not making a selection.
Any other keyword args will become HTML attributes for the <select>.
Examples (call, result)::
>>> select("currency", "$", [["$", "Dollar"], ["DKK", "Kroner"]])
literal(u'<select id="currency" name="currency">\\n<option selected="selected" value="$">Dollar</option>\\n<option value="DKK">Kroner</option>\\n</select>')
>>> select("cc", "MasterCard", [ "VISA", "MasterCard" ], id="cc", class_="blue")
literal(u'<select class="blue" id="cc" name="cc">\\n<option value="VISA">VISA</option>\\n<option selected="selected" value="MasterCard">MasterCard</option>\\n</select>')
>>> select("cc", ["VISA", "Discover"], [ "VISA", "MasterCard", "Discover" ])
literal(u'<select id="cc" name="cc">\\n<option selected="selected" value="VISA">VISA</option>\\n<option value="MasterCard">MasterCard</option>\\n<option selected="selected" value="Discover">Discover</option>\\n</select>')
>>> select("currency", None, [["$", "Dollar"], ["DKK", "Kroner"]], prompt="Please choose ...")
literal(u'<select id="currency" name="currency">\\n<option selected="selected" value="">Please choose ...</option>\\n<option value="$">Dollar</option>\\n<option value="DKK">Kroner</option>\\n</select>')
>>> select("privacy", 3L, [(1, "Private"), (2, "Semi-public"), (3, "Public")])
literal(u'<select id="privacy" name="privacy">\\n<option value="1">Private</option>\\n<option value="2">Semi-public</option>\\n<option selected="selected" value="3">Public</option>\\n</select>')
>>> select("recipients", None, [([("u1", "User1"), ("u2", "User2")], "Users"), ([("g1", "Group1"), ("g2", "Group2")], "Groups")])
literal(u'<select id="recipients" name="recipients">\\n<optgroup label="Users">\\n<option value="u1">User1</option>\\n<option value="u2">User2</option>\\n</optgroup>\\n<optgroup label="Groups">\\n<option value="g1">Group1</option>\\n<option value="g2">Group2</option>\\n</optgroup>\\n</select>')
"""
_set_id_attr(attrs, id, name)
attrs["name"] = name
convert_boolean_attrs(attrs, ["multiple"])
# Accept None as selected_values meaning that no option is selected
if selected_values is None:
selected_values = ('',)
# Turn a single string or integer into a list
elif isinstance(selected_values, (basestring, int, long)):
selected_values = (selected_values,)
# Cast integer values to strings
selected_values = map(unicode, selected_values)
# Prepend the prompt
prompt = attrs.pop("prompt", None)
if prompt:
options = [Option("", prompt)] + list(options)
# Canonicalize the options and make the HTML options.
if not isinstance(options, Options):
options = Options(options)
html_options = []
# Create the options structure
def gen_opt(val, label):
if val in selected_values:
return HTML.option(label, value=val, selected="selected")
else:
return HTML.option(label, value=val)
# Loop options and create tree (if optgroups presents)
for opt in options:
if isinstance(opt, OptGroup):
optgroup_options = []
for subopt in opt.options:
optgroup_options.append(gen_opt(subopt.value, subopt.label))
optgroup = HTML.optgroup(NL, NL.join(optgroup_options), NL, label=opt.label)
html_options.append(optgroup)
else:
html_options.append(gen_opt(opt.value, opt.label))
return HTML.select(NL, NL.join(html_options), NL, **attrs)