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

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