gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:51:27 24 4
gpt4 key购买 nike

我正在尝试创建一个下拉列表,当单击第一个列表项时,会打开一个与列表旁边的选定项相关的新列表,如下所示:
Dropdown
我尝试为此使用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>
并在 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");
但我不确定如何实现类似的布局。我不是在寻找类似的 CSS 样式,而是在现有列表旁边呈现另一个列表,有什么帮助/资源来解决这个问题吗?

最佳答案

我创建了一个示例,该示例通过从数组中获取数据来创建具有分组选项的动态下拉菜单。我希望它有帮助

// 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>

关于javascript - 如何动态地将选项添加到 Bootstrap 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64722016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com