英文:
Only optgroup multiple (bootstrap-select)
问题
我正在使用 https://github.com/snapappointments/bootstrap-select/ v1.13.18,并且我需要提供一个<select>对象,其中只有optgroup选项可以是多选的。因此,如果我们选择非optgroup选项,所有其他选项都会取消选择(包括optgroup选项),如果我们选择optgroup选项,则所有非optgroup选项都会取消选择,但可以选择其他optgroup选项。是否有内置的方法可以实现这一点,还是必须从头开始编写?
英文:
I am using https://github.com/snapappointments/bootstrap-select/ v1.13.18 and I need to provide <select> object, where only optgroup options can be multiple. So if we select non optgroup option, all other options are being deselect (including optgroup options) and if we select optgroup option all non optgroup options are being deseleted, but it is possible to select other optgroup options. Is there any built-in way to do this or it has to be written from scratch?
<select class="selectpicker" data-container="body" id="levels">
<option value="1">My tasks</option>
<optgroup id="departments" label="Departments tasks:" multiple>
<option value="8">Service</option>
<option value="1">Production</option>
</optgroup>
<option value="3" selected="">All tasks</option>
</select>
答案1
得分: 0
以下是您要翻译的代码部分:
const levels = document.getElementById("levels");
var levels_selected_previous = Array.from(levels.selectedOptions);
levels.addEventListener("change", (e) => {
e.preventDefault();
let levels_selected_current = Array.from(levels.selectedOptions);
let last_selected_array = levels_selected_current.filter(el => !levels_selected_previous.includes(el));
if(last_selected_array.length > 0){
let last_selected = last_selected_array[0].parentElement.nodeName;
levels_selected_current.forEach(function(el) {
if(last_selected === "OPTGROUP"){
if (el.parentElement.nodeName !== "OPTGROUP") {
el.selected = false;
}
} else {
if (el.parentElement.nodeName === "OPTGROUP") {
el.selected = false;
} else if(el.parentElement.nodeName !== "OPTGROUP") {
if (el !== last_selected_array[0]) {
el.selected = false;
}
}
}
});
}
$(levels).selectpicker('refresh');
levels_selected_previous = levels_selected_current;
});
如果您需要进一步的翻译或有其他问题,请告诉我。
英文:
Finally I made something like this:
const levels = document.getElementById("levels");
var levels_selected_previous = Array.from(levels.selectedOptions);
levels.addEventListener("change", (e) => {
e.preventDefault();
let levels_selected_current = Array.from(levels.selectedOptions);
let last_selected_array = levels_selected_current.filter(el => !levels_selected_previous.includes(el));
if(last_selected_array.length>0){
let last_selected = last_selected_array[0].parentElement.nodeName;
levels_selected_current.forEach(function(el) {
if(last_selected==="OPTGROUP"){
if (el.parentElement.nodeName !== "OPTGROUP") {
el.selected = false;
}
} else {
if (el.parentElement.nodeName === "OPTGROUP") {
el.selected = false;
} else if(el.parentElement.nodeName !== "OPTGROUP") {
if (el !== last_selected_array[0]) {
el.selected = false;
}
}
}
});
}
$(levels).selectpicker('refresh');
levels_selected_previous = levels_selected_current;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论