A table that gets updated with new values as soon as they arrive. Each value is in its own row:
An array of objects representing each a quote (ticker and price)
let array_of_quotes = [
{ ticker: "AAPL", price: 999 },
{ ticker: "AMZN", price: 957 },
{ ticker: "GOOG", price: 959 },
{ ticker: "YHOO", price: 225 },
{ ticker: "MSFT", price: 514 },
{ ticker: "AAPL", price: 218 },
{ ticker: "AMZN", price: 256 },
{ ticker: "AAPL", price: 526 },
{ ticker: "AMZN", price: 288 },
{ ticker: "MSFT", price: 514 },
{ ticker: "AMZN", price: 909 },
{ ticker: "YHOO", price: 225 },
];
A counter that keeps track of the current row to be updated
let counter = 0;
A function that updates the row, it gets the body by its id, then adds a row with the current count as its id. In said row, 4 table datas <td>
are added with the ticker and current count as id so that, the function only has to get that <td>
element and injects the price in its inner text leaving the other <td>
s untouched
let table_s_body = document.getElementById("quote_table_body");
function table_row_updater(ticker_price) {
table_s_body.innerHTML += `<tr id=row-${counter}></tr>`;
let current_row = document.getElementById(`row-${counter}`);
current_row.innerHTML += `<td id=AAPL-${counter}></td>`;
current_row.innerHTML += `<td id=MSFT-${counter}></td>`;
current_row.innerHTML += `<td id=AMZN-${counter}></td>`;
current_row.innerHTML += `<td id=GOOG-${counter}></td>`;
current_row.innerHTML += `<td id=YHOO-${counter}></td>`;
let td_to_be_updated = document.getElementById(
`${ticker_price.ticker}-${counter}`
);
td_to_be_updated.innerText = ticker_price.price;
counter += 1;
}
of course, the counter gets incremented so that the next iteration starts a new.
For this demo purpose, i paced the "injection" of prices into their relative row's so it is less confusing.
array_of_quotes.forEach((quote, index) => {
setTimeout(() => {
table_row_updater(quote);
}, (index + 1) * 1000);
});
What I would like to take out of your answers:
- Is this solution clean? I feel it's a bit dirty to rely on a counter.
- Couldn't the counter be achieved with a closure? If so, would anyone show me how?
- How would you go about updating a single out of multiple ones in a table with an ever growing number of rows.
let array_of_quotes = [
{ ticker: "AAPL", price: 999 },
{ ticker: "AMZN", price: 957 },
{ ticker: "GOOG", price: 959 },
{ ticker: "YHOO", price: 225 },
{ ticker: "MSFT", price: 514 },
{ ticker: "AAPL", price: 218 },
{ ticker: "AMZN", price: 256 },
{ ticker: "AAPL", price: 526 },
{ ticker: "AMZN", price: 288 },
{ ticker: "MSFT", price: 514 },
{ ticker: "AMZN", price: 909 },
{ ticker: "YHOO", price: 225 },
];
let counter = 0;
let table_s_body = document.getElementById("quote_table_body");
function table_row_updater(ticker_price) {
table_s_body.innerHTML += `<tr id=row-${counter}></tr>`;
let current_row = document.getElementById(`row-${counter}`);
current_row.innerHTML += `<td id=AAPL-${counter}></td>`;
current_row.innerHTML += `<td id=MSFT-${counter}></td>`;
current_row.innerHTML += `<td id=AMZN-${counter}></td>`;
current_row.innerHTML += `<td id=GOOG-${counter}></td>`;
current_row.innerHTML += `<td id=YHOO-${counter}></td>`;
let td_to_be_updated = document.getElementById(
`${ticker_price.ticker}-${counter}`
);
td_to_be_updated.innerText = ticker_price.price;
counter += 1;
}
array_of_quotes.forEach((quote, index) => {
setTimeout(() => {
table_row_updater(quote);
}, (index + 1) * 1000);
});
<table>
<thead>
<tr>
<th>AAPL</th>
<th>MSFT</th>
<th>AMZN</th>
<th>GOOG</th>
<th>YHOO</th>
</tr>
</thead>
<tbody id="quote_table_body"></tbody>
</table>