当前位置: 首页>>代码示例>>Python>>正文


Python HTML.option方法代码示例

本文整理汇总了Python中webhelpers.html.HTML.option方法的典型用法代码示例。如果您正苦于以下问题:Python HTML.option方法的具体用法?Python HTML.option怎么用?Python HTML.option使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在webhelpers.html.HTML的用法示例。


在下文中一共展示了HTML.option方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: select

# 需要导入模块: from webhelpers.html import HTML [as 别名]
# 或者: from webhelpers.html.HTML import option [as 别名]
def select(name, selected_values, options, id=None, **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.

    ``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 the empty string ("") 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>')
    """
    _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 = []
    for opt in options:
       if opt.value in selected_values:
           opt = HTML.option(opt.label, value=opt.value, selected="selected")
       else:
           opt = HTML.option(opt.label, value=opt.value)
       html_options.append(opt)
    return HTML.select(NL, NL.join(html_options), NL, **attrs)
开发者ID:adrianpike,项目名称:wwscc,代码行数:79,代码来源:tags.py

示例2: gen_opt

# 需要导入模块: from webhelpers.html import HTML [as 别名]
# 或者: from webhelpers.html.HTML import option [as 别名]
 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)
开发者ID:gjhiggins,项目名称:WebHelpers2,代码行数:7,代码来源:tags.py


注:本文中的webhelpers.html.HTML.option方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。