我从一个API中获取数据,api包含用户的信息,包括他们的名字,用户ID和所有这些信息。现在我从API获取数据,但我只想获取特定用户的数据。最初我想显示姓名和一个按钮来查看有关此人的更多信息。但是当按钮被点击时,我想用user_id获取关于用户的所有信息
let table;
const show = (data) => {
table.innerHTML = data
.map(({id,name,email, gender,status}) => `<tr>
<td colspan="3">${name} </td>
<td><button class="more" data-id="${id}" data-name="${name}" data-email="${email}" data-gender="${gender}" data-status="${status}"><span class="material-symbols-outlined">visibility</span></button></td>
<tr hidden><td>${email}</td>
<td>${gender}</td>
<td>${status}</td>
</tr>`)
.join("");
};
const load = () => {
fetch("url")
.then(result => result.json())
.then(show);
};
window.addEventListener("DOMContentLoaded", () => {
table = document.getElementById('table');
table.addEventListener("click", (e) => {
const tgt = e.target.closest("button");
if (!tgt) return;
const id = tgt.dataset.id;
const name = tgt.dataset.name;
const email = tgt.dataset.email;
const gender = tgt.dataset.gender;
const status = tgt.dataset.status;
document.getElementById("modal-name").textContent = name;
document.getElementById("modal-email").textContent = email;
document.getElementById("modal-gender").textContent = gender;
document.getElementById("modal-status").textContent = status;
$('#myModal').modal('show');
})
load();
});
1条答案
按热度按时间czq61nw11#
备注
超文本标记语言
JS