add table

This commit is contained in:
zongor 2024-11-05 12:50:05 -05:00
parent dc456207b7
commit ce65d04cdc
1 changed files with 31 additions and 22 deletions

View File

@ -76,29 +76,38 @@
let stateContainer = document.getElementById('state-container'); let stateContainer = document.getElementById('state-container');
stateContainer.innerHTML = ''; stateContainer.innerHTML = '';
stateVotes.forEach((row) => { stateVotes.forEach((row) => {
const card = document.createElement('div'); const table = document.createElement('table');
const thead = document.createElement('thead');
const stateInfo = document.createElement('div'); const headerRow = document.createElement('tr');
stateInfo.innerHTML = ` const headers = [`${row.state}`, 'Party', `Votes Left: ${row.expected - row.current}`];
<h2>${row.state} (Votes Left: ${row.expected - row.current})</h2> headers.forEach(header => {
`; const th = document.createElement('th');
card.appendChild(stateInfo); th.textContent = header;
headerRow.appendChild(th);
const candidateInfo = document.createElement('div');
candidateInfo.innerHTML = '<h3>Candidates:</h3>';
const candidatesList = document.createElement('ul');
row.candidates.sort((a, b) => b.votes - a.votes).forEach(candidate => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<span>${candidate.name[0]}</span> (${candidate.name[1]}) - ${candidate.votes} votes
`;
candidatesList.appendChild(listItem);
}); });
thead.appendChild(headerRow);
table.appendChild(thead);
candidateInfo.appendChild(candidatesList); const tbody = document.createElement('tbody');
card.appendChild(candidateInfo); row.candidates.sort((a, b) => b.votes - a.votes).forEach((candidate, index) => {
stateContainer.appendChild(card); const rowElement = document.createElement('tr');
const nameCell = document.createElement('td');
nameCell.textContent = candidate.name[0];
rowElement.appendChild(nameCell);
const partyCell = document.createElement('td');
partyCell.textContent = candidate.name[1];
rowElement.appendChild(partyCell);
const votesCell = document.createElement('td');
votesCell.textContent = candidate.votes;
rowElement.appendChild(votesCell);
tbody.appendChild(rowElement);
});
table.appendChild(tbody);
stateContainer.appendChild(table);
}); });
} }
} }