我需要一些建议。我有firebase实时数据库,我想对数据值进行条件逻辑处理,我想显示或隐藏div元素,取决于列表中的值。有什么办法可以做到吗?如何在Firebase列表中为特定值使用if / else语句?
function renderRingNumbers(doc){
let li = document.createElement('li');
let ringSelect = document.createElement('span');
let cross = document.createElement('div');
li.setAttribute('data-id', doc.id);
ringSelect.textContent = doc.data().ringSelect;
cross.textContent = 'x';
li.appendChild(ringSelect);
li.appendChild(cross);
ringnumList.appendChild(li);
//deleting data
cross.addEventListener('click', (e) => {
e.stopPropagation();
let id = e.target.parentElement.getAttribute('data-id');
db.collection('ringnum').doc(id).delete();
})
}
//getting data
db.collection('ringnum').get().then((snapshot) => {
snapshot.docs.forEach(doc => {
renderRingNumbers(doc);
})
})
//saving data
form39.addEventListener('submit', (e) => {
e.preventDefault();
db.collection('ringnum').add({
ringSelect: form39.ringSelect.value
});
})
//realtime listener
db.collection('ringnum').onSnapshot(snapshot => {
let changes = snapshot.docChanges();
changes.forEach( change => {
if(change.type == 'added'){
renderRingNumbers(change.doc)
} else if (change.type == 'removed'){
let li = ringnumList.querySelector('[data-id=' + change.doc.id +
']');
ringnumList.removeChild(li);
}
})
})
这是我的JS代码,用于保存和读取数据,我只想不出如何编写if / else语句来显示和隐藏div元素。