我目前有一个基本的CRUD网站,最近我开始工作,列出了许多产品和相关的价格。我的技能不是最好的,但我正在努力打好仗,希望对我要达到的目标有所帮助/指导。
我将数据存储在Firestore数据库中,并且将其拉出并显示在列表中。为了显示它,我将子集合中的值添加到数组中,然后使用for循环遍历并提取每个值,并使用html模板字符串显示它。我决定,因为我已经在创建和显示方面做到了这一点,所以我想添加一个按钮(在我的情况下为图标),使我可以编辑返回的每个值。
const shelf = doc.data();
doc.ref.collection("products").get().then((Snapshot) => {
const products = Snapshot.docs.map(doc => doc.data());
// start of for loop
for(let product of products){
// start of template string inside backticks
const li = `
<p><div>${product.name} <div class="right"><font class=pink-text>Price:</font> $${product.price} <a href="#" class="grey-text modal-trigger" data-target="modal-priceedit"><i class="material-icons">create</i></a></div></div></p>
`;
// end of template string inside backticks
html += li;
// start of edit price section
let price = product.price;
const priceeditForm = document.querySelector('#priceedit-form');
priceeditForm.addEventListener('submit', (e) => {
e.preventDefault();
console.log("Document with ID: ", doc.id);
doc.ref.collection("vendors").doc(price)get().then((Snapshot) => {
const priceedit = Snapshot.docs.map(doc => doc.data());
price: priceeditForm['price'].value
}).then(() => {
// close the signup modal & reset form
const modal = document.querySelector('#modal-priceedit');
M.Modal.getInstance(modal).close();
priceeditForm.reset();
}).catch(err => {
console.log(err.message);
});
});
//end of edit price section
}
// end of for loop
productList.innerHTML = html;
});
我希望单击该图标后,会弹出一个模型,其中包含一个价格输入字段,我可以输入新价格并提交价格,从而更新数据库中的值。
我将非常感谢能为我提供帮助的任何人。预先谢谢你。
感谢所有为此提供协助的人。我决定使用DOM元素从头开始重做代码到appendChild。完成后,一切将变得非常容易。决定对其进行更改,以使自己在代码方面更具灵活性。