Vous devez construire votre table comme ceci:
<table class="summonizer">
<thead>
<tr>
<th></th>
<th>c1</th>
<th>c2</th>
<th>s</th>
</tr>
</thead>
<tbody>
<tr>
<th>r1</th>
<td><input type="text" value="5"/></td>
<td><input type="text" value="6"/></td>
<td></td>
</tr>
<tr>
<th>r2</th>
<td><input type="text" value="7"/></td>
<td><input type="text" value="3"/></td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
<th>s</th>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
Et votre javascript devrait ressembler à ceci:
function summonize_table($table) {
var col_sums = [];
var row_sums = [];
var all_sum = 0;
$table.find('tbody tr').each(function(row){
row_sums[row] = 0;
$(this).find('td input[type=text]').each(function(column){
value = parseInt(this.value);
row_sums[row] += value;
if (!col_sums[column]) col_sums[column] = 0;
col_sums[column] += value;
all_sum += value;
});
});
$.each(row_sums,function(index,value){
$table.find('tbody tr:eq('+index+') td:last').html(value);
});
$.each(col_sums,function(index,value){
$table.find('tfoot tr td:eq('+index+')').html(value);
});
$table.find('tfoot tr td:last').html(all_sum);
}
$(function(){
$('table.summonizer input[type=text]').live('change',function(){
summonize_table($(this).closest('table'));
});
$('table.summonizer input[type=text]:first').trigger('change');
});
Vous pouvez voir un exemple de travail ici: http://jsfiddle.net/my9DF/
une un peu au-delà de ma compréhension de jquery mais travaillant. merci – edib
qu'en particulier ne comprenez-vous pas? Peut-être que je peux vous aider à comprendre. – jigfox
Je suis nouveau dans jquery mais je suis dans un projet pour produire un projet avec jquery. Ma question est la suivante: que les dernières colonnes et lignes soient la somme, quel sera le code? je veux dire 3 * 3 colonnes textfield et les dernières sont des sommes – edib