I am creating a project that simulates Stock Exchange Market. I am exposing stock data to the user on a big html <table>
where each <tr>
row has two <td>
cells. One of them is the symbol of the company (i.e. AAPL) and the other one is the current market value (couldn't be simpler).
What I do now is that I use a javascript function like this:
<script type="text/javascript">
$(function(){
setInterval(loadTable, 10000)
});
function loadTable(){
$("#se_table").load("/load/table/?ajax&user={{user.username}}");
}
</script>
Is this the best way? Should I load the whole table when only one value is changed?
Firstly, this will probably result in a bad user experience if someone is busy doing something with the table, such as highlighting text, when it all suddenly re-renders. If you must reload all data, at least only update the rows that changed. You can achieve this with javascript built-in table manipulations, or any number of jQuery plugins.
Secondly, if it is computationally plausible, it would be much more efficient in terms of data transfer and user experience if you could calculate which rows changed, and only send those rows. Assign a globally unique id
to each row in your table, so you can easily update only that row with jQuery. With this technique, it's also easy to add some visual cue that the specific row has updated, such as a slight colour change, which is often nice (where applicable).
My favorite way of doing this sort of thing (without Comet) is the following:
trigger
method send another ajax request, this time expecting a list of rows that have changed since that datetime. Update
Based on your comments, I'll just add a few extra notes.
text
function, or one of the jQuery table plugins, to update the rows.This is quite a mouthful and a lot of googling if you're new to this, but it's a good, clean way of doing it.
Make extensive use of of data attributes. During the initial GET
page request, have your columns that contain values look something like:
<td data-for-symbol="GOOG">+256.00</td>
Have /load/table/?ajax&user={{user.username}}
return a JSON response with objects that contain updates since the last time the user fetched the table, then apply them individually:
$('td[data-for-symbol=' + obj.symbol + ']').text(obj.value);
Can't go cleaner than that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With