I want to generate a huge table generated by a Java servlet and sent to client as ajax response. My table cells are
<td style="background-color:blue;border-color:yellow;" onClick=tableClick(this)>value1</td>
<td style="background-color:red;border-color:cyan" onClick=tableClick(this)>value2</td>
suppose we have a table with 30 rows and 40 cols makes totally ca. 1000 cels. When I send this string from java to browser, i will be using to much network resources and the clients have poor connection capabilitiy, so have to minimize the content sent from java. First i cache the color names with c1:blue, c2:yellow, c3:red, c4:cyan
On the receiving side, the devices are mobile or ipads or tablets, so cpu workload is also important. What is the best way for performance to generate the table dynamically? How should be actions registered? How should be colors set?
Option1: use a json-notation such as
{ {'c1','c2','value1'},{'c3','c4','value2'}..
Populate a big string containing all cells and set to the table only once with append.
// iterate in the json array and fetch the corresponding background color
var bgcolor = extractFromJson(row,col);
tablestring += "<td onclick=tableClick(this) style='background-color:' + bgcolor + ';border-color:' + brcolor + '>cellvalue</td>";
// once we have all the cells, then set to the table
$('#mytable').append(tablestring);
Option:2 Send a empty template to be filled on the receiver
Java: String tableString; bg is attribute for background color, br is attribute for the border color
tableString += '<td bg=c1 br=c4>cellvalue</td>';
on the browser side, set this table string to the table container
$('#mytable').append(tablestring);
// register actions and set colors
$('#mytable').find('td').each(funtction() {
$(this).on('click', function () { tableClick($(this)[0]); });
$(this).style('background-color', getColor($(this).attr("bg")));
$(this).style('border-color', getColor($(this).attr("br")));