我正在从一个 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();
});