简体   繁体   English

使用自定义选项标签选择/下拉

[英]Select/dropdown with customized option labels

Just want to ask here, although from all the searches that i did, the only answer i ever found was customizing select's option is just not possible. 只想在这里问一下,尽管从我进行的所有搜索中,我找不到的唯一答案就是自定义select的选项。

However i stumbled upon a page with customized option on it 但是我偶然发现了一个带有自定义选项的页面

在此处输入图片说明

I was thinking the code of the above might look something like this 我以为上面的代码可能看起来像这样

<select>
    <option>
        <span style="float:left;">Option 1</span>
        <span style="float:right;">Test</span>
    </option>
</select>

But as i mentioned, customizing such option is not possible at all. 但是正如我提到的,完全不可能自定义这样的选项。

So i just want to know how to achieve such thing? 所以我只想知道如何实现这样的目标? Is there such javascript/jquery select/dropdown plugins out there that can provide such option look? 有没有这样的javascript / jquery select / dropdown插件可以提供这种外观?

There is a bootstrap selectpicker plugin . 有一个bootstrap selectpicker插件 Which allows you to achieve this. 这可以让您实现这一目标。

In bootstrap selectpicker there is an option to add subtext like as below. 在bootstrap selectpicker中,有一个添加子文本的选项,如下所示。

<select class="selectpicker" data-size="5">
  <option data-subtext="Heinz">Ketchup</option>
</select>

You can also add button like add new as an option in selectpicker as <option data-content="<span class='btn btn-link'><i class='fa fa-plus text-primary'></i> Add new</span>"></option> 您还可以在selectpicker中添加像add new这样的选项作为<option data-content="<span class='btn btn-link'><i class='fa fa-plus text-primary'></i> Add new</span>"></option>

Example: 例:

<select class="selectpicker" data-size="5">
  <option data-subtext="Heinz">Ketchup</option>
  <option data-content="<span class='btn btn-link'><i class='fa fa-plus text-primary'></i> Add new</span>"></option>
</select>

After including your html code initialise your selectpicker with jquery as below. 包含html代码后,使用jquery初始化selectpicker,如下所示。

$('.selectpicker').selectpicker({
  size: 4
});

There are lots options with this selectpicker plugin. 这个selectpicker插件有很多选项。 Please Refer this link for more details. 请参阅此链接以获取更多详细信息。

If you want to display the Subtext as First text on the dropdown then you can use the below example. 如果要在下拉列表中将“子文本”显示为“第一文本 ”,则可以使用以下示例。

<select class="selectpicker" data-size="5">
     <option  data-subtext="<span class='leftMen pull-left'>Sub Text</span>">Main Text </option>
     <option data-content="<span class='btn btn-link'><i class='fa fa-plus text-primary'></i> Add new</span>"></option>
</select>

The leftMen class is defined as below. leftMen类的定义如下。

.leftMen{
    padding-top: 3px;
    padding-right:5px;
 }

Sample Screen is here ( note that this is not the output of the code that i have written here. This is just to show the behaviour of selectpicker plugin ): 示例屏幕在这里( 请注意,这不是我在此处编写的代码的输出。这只是为了展示selectpicker插件的行为 ): 在此处输入图片说明

Demo: 演示:

 $('.selectpicker').selectpicker(); //Initialize selectpicker $(".muted.text-muted").addClass("pull-right"); //after the initialization we add pull-right class to the class 'muted text-muted' so that the sub-text will be displayed on right. 
 .bootstrap-select.btn-group .dropdown-menu li a span.text { display: inline-block; width: 100% !important; //This is also necessary to change the default class by adding this line of code to set the width of the option to 100% then only the sub-text will be pulled to right } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <select class="selectpicker" data-size="5"> <option data-content="<span class='btn btn-link'><i class='fa fa-plus text-primary'></i> Add new</span>"></option> <option data-subtext="Sub Text1">Main Text1 </option> <option data-subtext="Sub Text2">Main Text2 </option> <option data-subtext="Sub Text3">Main Text3 </option> <option data-subtext="Sub Text4">Main Text4 </option> </select> 

Please try select2 for this https://select2.org/ 请尝试为此选择https://select2.org/

But yes, you can't just customize the select option. 但是,是的,您不能只是自定义选择选项。 Select2 will create a placeholder for you. Select2将为您创建一个占位符。

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

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