我正在尝试使用dropdown
和checkbox
来过滤javascript
上的jQuery
菜单中的选项,但似乎不起作用。它会隐藏即使更改时过滤器也会应用的所有内容。
使用相同的javascript本身通过数组填充下拉列表。
function fillJobs(jobStatus){
var select = document.getElementById("candidateAssociatedJobs");
select.innerHTML = '';
let companies = {
"A":[
{"jobid":"1","company":"A","role":"A Role 1","status":"Closed"},
{"jobid":"2","company":"A","role":"A Role 2","status":"Active"},
{"jobid":"3","company":"A","role":"A Role 3","status":"Active"}
],
"B":[
{"jobid":"4","company":"B","role":"B Role 1","status":"Closed"},
{"jobid":"5","company":"B","role":"B Role 2","status":"Closed"},
{"jobid":"6","company":"B","role":"B Role 3","status":"Active"}
]}
Object.keys(companies).forEach(company => {
let optGroup = document.createElement("optgroup");
optGroup.label = company;
companies[company].forEach(job => {
if(jobStatus === "Active"){
if(job.status === "Active"){
let opt = job;
let el = document.createElement("option");
el.textContent = opt.role;
el.setAttribute('data-tokens', company);
el.value = opt.jobid;
optGroup.appendChild(el);
}
}
else
{
let opt = job;
let el = document.createElement("option");
el.textContent = opt.role;
el.setAttribute('data-tokens', company);
el.value = opt.jobid;
optGroup.appendChild(el);
}
})
select.append(optGroup);
console.log(select);
})
}
let jobStatusCheck = "Active";
fillJobs(jobStatusCheck);
$('#activeJobsCheck').change(e =>{
e.preventDefault();
if(jobStatusCheck === "Active"){
jobStatusCheck = "Closed"
fillJobs(jobStatusCheck);
}
else{
jobStatusCheck = "Active"
fillJobs(jobStatusCheck);
}
})
在控制台上记录我的select
的选项确实可以正确显示,但是DOM没有得到更新。
答案 0 :(得分:3)
修改了基础select
后,调用引导程序选择refresh
使其反映方法末尾的更改:
function fillJobs(jobStatus){
var select = document.getElementById("candidateAssociatedJobs");
select.innerHTML = '';
let companies = {
"A": [
{"jobid":"1","company":"A","role":"A Role 1","status":"Closed"},
{"jobid":"2","company":"A","role":"A Role 2","status":"Active"},
{"jobid":"3","company":"A","role":"A Role 3","status":"Active"}
],
"B": [
{"jobid":"4","company":"B","role":"B Role 1","status":"Closed"},
{"jobid":"5","company":"B","role":"B Role 2","status":"Closed"},
{"jobid":"6","company":"B","role":"B Role 3","status":"Active"}
]
};
Object.keys(companies).forEach(company => {
let optGroup = document.createElement("optgroup");
optGroup.label = company;
companies[company].forEach(job => {
if(jobStatus === "Active"){
if(job.status === "Active"){
let opt = job;
let el = document.createElement("option");
el.textContent = opt.role;
el.setAttribute('data-tokens', company);
el.value = opt.jobid;
optGroup.appendChild(el);
}
}
else
{
let opt = job;
let el = document.createElement("option");
el.textContent = opt.role;
el.setAttribute('data-tokens', company);
el.value = opt.jobid;
optGroup.appendChild(el);
}
})
select.append(optGroup);
$(select).selectpicker('refresh');
})
}
let jobStatusCheck = "Active";
fillJobs(jobStatusCheck);
$('#activeJobsCheck').change(e =>{
e.preventDefault();
if(jobStatusCheck === "Active"){
jobStatusCheck = "Closed"
fillJobs(jobStatusCheck);
}
else{
jobStatusCheck = "Active"
fillJobs(jobStatusCheck);
}
});
$(function() {
$('#candidateAssociatedJobs').selectpicker();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/css/bootstrap-select.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/js/bootstrap-select.min.js"></script>
<select id="candidateAssociatedJobs"></select>
<label>
<input type="checkbox" id="activeJobsCheck">
Show all jobs
</label>