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


Python HTML.select方法代码示例

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


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

示例1: select

# 需要导入模块: from webhelpers.html import HTML [as 别名]
# 或者: from webhelpers.html.HTML import select [as 别名]
    def select(self, name, option_tags='', show_errors=True, **options):
        """
        Creates a dropdown selection box

        ``option_tags`` is a string containing the option tags for the select box::

            >>> select("people", "<option>George</option>")
            '<select id="people" name="people"><option>George</option></select>'

        Options:

        * ``multiple`` - If set to true the selection will allow multiple choices.

        """
        o = {'name_': name}
        o.update(options)

        ret = HTML.select(option_tags, **o)
        if show_errors:
            ret += self.get_error(name)
        return ret
开发者ID:NetShepsky,项目名称:Ferrox,代码行数:23,代码来源:formgen.py

示例2: select

# 需要导入模块: from webhelpers.html import HTML [as 别名]
# 或者: from webhelpers.html.HTML import select [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)s'<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)s'<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)s'<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)s'<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", 3, [(1, "Private"), (2, "Semi-public"), (3, "Public")])
        literal(%(u)s'<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)s'<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)
开发者ID:gjhiggins,项目名称:WebHelpers2,代码行数:96,代码来源:tags.py


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