Mon script pour mettre à jour une table : qu'en pensez vous ?
Bonjour,
Je dois mettre à jour une table toutes les secondes
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
var $table;
function createLine(jsonLine){
return '<tr>' +
'<td>' + jsonLine.x + '</td>' +
'<td>' + jsonLine.y + '</td>' +
'<td>' + jsonLine.z + '</td>' +
'</tr>';
}
function createBody(jsonTab){
var tbody = '<tbody>';
var i;
for(i=0; i<jsonTab.length; i++){
tbody += createLine(jsonTab[i]);
}
return tbody += '</tbody>';
}
function createTable(jsonTab){
var tab =
'<table class="table table-striped table-bordered table-hover">' +
'<thead>' +
'<tr>' +
'<th>X</th>' +
'<th>Y</th>' +
'<th>Z</th>' +
'</tr>' +
'</thead>';
return tab += createBody(jsonTab) + '</table>';
}
function successMain(jsonVar){
var tableName = "tutu";
if(!$table){
$("#" + tableName).html(createTable(jsonVar.table));
$table = $("#" + table + " table");
} else {
$table.find("tbody").replaceWith(createBody(jsonVar.table))
}
} |
Est-ce que vous auriez fait comme ça, où il y a des méthodes mieux pensées ?
Merci d'avance,