简体   繁体   English

有没有办法使用 select2 为下拉列表中的每个选项添加描述?

[英]Is there a way to add a description to each option in a dropdown using select2?

I have a Django project in which I have a select dropdown like this:我有一个 Django 项目,其中有一个像这样的选择下拉列表:

在此处输入图片说明

But I want something like this:但我想要这样的东西:

在此处输入图片说明

My <select> form looks like so: (after inspecting in developer tools)我的<select>表单看起来像这样:(在开发人员工具中检查后)

<select class="form-control" data-parsley-required="true" id="id_org_role" name="org_role" required="">
    <option value="A">Administrator</option>
    <option value="M" selected="selected">Member</option>
</select>

If not select2, is there any other jquery library that can help me accomplish this?如果不是 select2,是否还有其他 jquery 库可以帮助我完成此操作?

I have tried following the Select2's documentation, and believe that if this way (for templates), we can span an image beside the option's text, then, there must be a way to span a "text description" too under each option.我曾尝试选择二的文档以下,并认为,如果这种方式(对于模板),我们可以跨越该选项的文本旁边的图像,那么,必须有跨越“文字说明”太下,每个选项的方式。 But I dont know how this can be achieved.但我不知道这是如何实现的。 All I have tried to do untill now is this:直到现在我一直试图做的是:

var org_role = function(roles){
      if (!roles.id) {
      return roles.text;
    }
    return $(
    '<strong>' + roles.text + '</strong>' + '<br/>'
    '<span>' + "some description" + '</span>'
  );
};

$("#id_org_role").select2({
  templateResult: org_role,
});

But this is not working.但这是行不通的。 Also, even if it does, it would display the same description for all the options.此外,即使是这样,它也会为所有选项显示相同的描述。 But it has to be different obviously.但它必须明显不同。

My Django form inside the template looks like this:我在模板中的 Django 表单如下所示:

<form method="POST" action="" class="org-member-edit" id='organization_permissions' data-parsley-validate>

{% csrf_token %}

<div class="form-group member-role{% if org_role_form.org_role.errors %} has-error{% endif %}">

    <label class="control-label" for="{{ org_role_form.org_role.id_for_label }}">{% trans "Role" %}</label>

{% render_field org_role_form.org_role class+="form-control" data-parsley-required="true" %}

    <div class="error-block">{{ org_role_form.org_role.errors }}</div>

  </div>

</form>

in the 5th line above, ie the render_field org_role_form.org_role in the template tag, the org_role takes up values from the following form: (note : render_field because I am using Django-tweaks-wideget)在上面,即5日线render_field org_role_form.org_role在模板标签中, org_role从下面的表格占用值:(注: render_field因为我使用的Django的调整,wideget)

class EditOrganizationMemberForm(SuperUserCheck, forms.Form):

org_role = forms.ChoiceField(choices=ADMIN_CHOICES)

which takes choice fields from another file choices.py like so:它从另一个文件 options.py 中获取选择字段,如下所示:

ADMIN_CHOICES = (
         ('A', _('Administrator')),
         ('M', _('Member'))
        )

How can I achieve this?我怎样才能做到这一点? (different descriptions for different options) Please help. (不同选项的不同描述)请帮忙。 I have been stuck at this since hours.几个小时以来,我一直坚持这个。

What about putting the description in an attribute eg title?将描述放在属性(例如标题)中怎么样?

 $(function(){ $("select").select2({ templateResult: formatOption }); function formatOption (option) { var $option = $( '<div><strong>' + option.text + '</strong></div><div>' + option.title + '</div>' ); return $option; }; });
 <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.full.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" /> <select style="width:200px"> <option value="optionA" title="Description for A">option A</option> <option value="optionB" title="Description for B">option B</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM