简体   繁体   English

如何动态地将选项添加到 Bootstrap 选择器?

[英]How can I add options to a Bootstrap selectpicker dynamically?

I'm trying to create a dropdown that when clicked on the first list item opens a new list of items related to the selected next to the list as shown below:我正在尝试创建一个下拉列表,当单击第一个列表项时,会打开一个与列表旁边的选定项相关的新列表,如下所示:

落下

I tried to use Bootstrap selectpicker for this and on click trying to add another list as:我尝试为此使用Bootstrap selectpicker,然后单击尝试将另一个列表添加为:

<select class="selectpicker" data-live-search="true">
            <optgroup label="Select Group 1">
                <option value="1">Option 1.1</option>
                <option value="2">Option 1.2</option>
                <option value="3">Option 1.3</option>
            </optgroup>
            
        </select>

and in jquery on click trying to append and refresh the selectpicker.并在 jquery 中单击尝试附加和刷新选择器。

       $(event.target)
           .append("<optgroup label="Select Group 2">
                <option value="4">Option 2.1</option>
                <option value="5">Option 2.2</option>
                <option value="6">Option 2.3</option>
            </optgroup>");
       $(dropdowmElem).selectpicker("refresh");

But I'm not sure how to achieve similar kind of layout.但我不确定如何实现类似的布局。 I'm not looking for similar CSS styles but to render another list next to existing list, any help/ sources to solve this?我不是在寻找类似的 CSS 样式,而是在现有列表旁边呈现另一个列表,有什么帮助/资源来解决这个问题吗?

I've created an example of creating a dynamic drop-down menu with grouped options by taking the data from an array.我创建了一个示例,该示例通过从数组中获取数据来创建具有分组选项的动态下拉菜单。 I hope it helps我希望它有帮助

 // Example data Array list namespace_list = [ ['test1-c1', 'test2-c1', 'test3-c1', 'test4-c1', 'test5-c1', 'test6-c1'], ['test1-c2', 'test2-c2', 'test3-c2', 'test4-c2', 'test5-c2', 'test6-c2'] ] $('#pod_dropdown').selectpicker(); // Selecting selectpicker dropdown select = document.getElementById('pod_dropdown'); for (let namespace of namespace_list) { // Generating group name namespace_name = namespace[0].slice(6, 8)+'-title' // Creating the optiongroup var optgroup = document.createElement('optgroup'); optgroup.id = namespace_name; optgroup.label = namespace_name // Appending optiongroup to the dropdown select.appendChild(optgroup); // Selecting the optiongroup optiongroup = document.getElementById(namespace_name); for (let pod of namespace) { // Creating the options of the optiongroup var opt = document.createElement('option'); opt.value = pod; opt.innerHTML = pod; // Appending the option to the optiongroup optiongroup.appendChild(opt); } } // Refresh the dropdwon menu $('#pod_dropdown').selectpicker("refresh"); // Getting the value after selecting it in the UI and unselect the dropdown function filterpod() { let pod = $('#pod_dropdown').val().toString(); console.log(pod) }
 <!-- jquery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <!-- bootstrap --> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <!-- multi select dropdown --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script> <!-- Creatting the dropdown --> <select id="pod_dropdown" class="selectpicker" data-style="btn btn-primary btn-sm" multiple data-live-search="true" data-width="auto"></select> <!-- Calling the function filterpod when hide dropdown after select option --> <script type="text/javascript"> $('#pod_dropdown').on('hide.bs.select', function(e) {filterpod();}); </script>

A reason why this might have not worked is because you can use double quotes in double quotes or double quotes for more than one line.这可能不起作用的一个原因是,您可以在双引号中使用双引号或在多行中使用双引号。 Try using this instead:尝试改用这个:

$(event.target)
    .append(`<optgroup label="Select Group 2">
         <option value="4">Option 2.1</option>
         <option value="5">Option 2.2</option>
         <option value="6">Option 2.3</option>
     </optgroup>`);
$(dropdowmElem).selectpicker("refresh");

You also can't detect mouseover on an <option> .您也无法在<option>上检测到鼠标悬停。 You can detect :hover though in case if you wanted to somehow link the two together that way.如果您想以某种方式将两者链接在一起,您可以检测到:hover You could also create your own type of menu if this functionality doesn't work the way that you would like.如果此功能无法按您希望的方式工作,您也可以创建自己的菜单类型。

Have resolved by following steps:已通过以下步骤解决:

$("#dropdownid").append('<option value="1" title="1">1</option>');
$("#dropdownid").selectpicker("refresh");
       

why not implement this in a multilevel dropdown为什么不在多级下拉菜单中实现它

 $(document).ready(function(){ $('.dropdown-submenu a.test').on("click", function(e){ $(this).next('ul').toggle(); e.stopPropagation(); e.preventDefault(); }); });
 .dropdown-submenu { position: relative; } .dropdown-submenu .dropdown-menu { top: 0; left: 100%; margin-top: -1px; }
 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> </style> </head> <body> <div class="container"> <h2>Multi-Level Dropdowns</h2> <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials <span class="caret"></span></button> <ul class="dropdown-menu"> <li><a tabindex="-1" href="#">HTML</a></li> <li><a tabindex="-1" href="#">CSS</a></li> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="#">2nd level dropdown</a></li> <li><a tabindex="-1" href="#">2nd level dropdown</a></li> <li class="dropdown-submenu"> <a class="test" href="#">Another dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">3rd level dropdown</a></li> <li><a href="#">3rd level dropdown</a></li> </ul> </li> </ul> </li> </ul> </div> </div> <script> </script> </body> </html>

Replace the menu items with the optgroupoptgroup替换菜单项

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

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