here is my table
<table id="first" class="merge">
<tr>
<td>Mick Jagger</td>
<td>30</td>
<td>50</td>
<td>10</td>
</tr>
<tr>
<td>David Bowie</td>
<td>21</td>
<td>45</td>
<td>21</td>
</tr>
</table>
<table id="second" class="merge">
<tr>
<td>Ronnie Wood</td>
<td>45</td>
<td>78</td>
<td>42</td>
</tr>
<tr>
<td>Mick Jagger</td>
<td>20</td>
<td>50</td>
<td>10</td>
</tr>
<tr>
<td>Lenny Kravitz</td>
<td>45</td>
<td>78</td>
<td>42</td>
</tr>
</table>
<table class="result">
</table>
What I like to do is:
1) For tables with class .merge get text of each first tr td elements (Mick Jagger, David Bowie, Ronnie Wood, Mick Jagger, Lenny Kravitz)
2) If any text matches text in second table (in our example Mick Jagger, but in practice matched elements could be hundrets). Get whole tr and append it into .result tabble. First td element remains the same - mick jagger and sum each next td elemnt 30 + 20 next td going to be 50 + 50, next 50 + 50.
3) If text do not matches second table, append this tr as it is into .result table. So new row going to be David Bowie 21 45 21. Next one Ronnie Wood 45 78 42 and so on.
Here is function what I created but I fell that there is several errors in it. I cant create better function.
$('.merge tr').each(function(){
var tableRow = $(this).find("td:first-of-type").text();
if ( $(this).find("td:first-of-type").text() === tableRow ) {
$(this) + $(this);
}
if ( $(this).find("td:first-of-type").text() !== tableRow ) {
$(this);
}
$("result").append(this);
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…