2011-12-23 39 views
0

我目前的表中的数据:jQuery的组通过与同等级的TD

<table>  
<tr class="Violão"> 
    <td>Violão</td> 
    <td class="td2 8">8</td> 
</tr> 
<tr class="Violão"> 
    <td>Violão</td> 
    <td class="td2 23">23</td> 
</tr> 
<tr class="Guitarra"> 
    <td>Guitarra</td> 
    <td class="td2 16">16</td> 
</tr> 
</table> 

我想要做的就是GROUPBY的达阵哪都一样,总结第二TD的值来获得总。考虑到这一点从来就放在了产品的名称是在TR类(如果需要的话就不知道了)

,我已经编码的当前的javascript:

$(".groupWrapper").each(function() { 
       var total = 0; 
       $(this).find(".td2").each(function() { 

       total += parseInt($(this).text()); 
       }); 
       $(this).append($("<td></td>").text('Total: ' + total)); 
      }); 

顺便说一下,目前的Java脚本不groupby。

现在我迷路了,我不知道我还能做什么,或者是否有一个插件可以做我想做的事。

+0

其中'.groupWrapper'类在你的上面的html代码中? – Ahsan 2011-12-23 13:30:25

回答

2
  1. </tr class="Violão">这没有意义。您只需关闭标签:</tr>。我假设你知道,因为你的代码的其余部分是正确的(除了你的类名,检查this问题)。
  2. 如果要将每个<td>的值与td2的类相加,请参见下文。

试试这个jQuery的:

var sum = 0; 
$(".td2").each(function(){ 
    sum = sum + $(this).text(); 
}); 

这应该在td S中添加的每个数可变sum

2
<table>  
<tr class="Violão"> 
    <td>Violão</td> 
    <td class="td2 8">8</td> 
</tr> 
<tr class="Violão"> 
    <td>Violão</td> 
    <td class="td2 23">23</td> 
</tr class="Violão"> 
<tr class="Guitarra"> 
    <td>Guitarra</td> 
    <td class="td2 16">16</td> 
</tr> 
</table> 

var dictionary = {}; 

$("td").each(function(){ 
    if(!dictionary[$(this).attr("class")) 
     dictionary[$(this).attr("class")] = 0; 
    dictionary[$(this).attr("class")] += parseInt($(this).html()); 
}); 
1

假设tr只有一个给定的类!

var sums = []; 
$('.td2').each(function(){ 
    var val = $(this).text(); 
    var parentClass = $(this).parent().attr('class'); 
    if(sums[parentClass] != undefined) sums[parentClass] +=parseFloat(val); 
    else sums[parentClass] = parseFloat(val); 
}); 
for(var key in sums){ 
    $('<tr><td>Total ('+key+')</td><td>'+sums[key]+'</td></tr>').appendTo($('table')); 
} 

我会给表中的一些ID,并切换到appendTo($('#<thID>'))

2
// declare an array to hold unique class names 
var dictionary = []; 

// Cycle through the table rows 
$("table tr").each(function() { 
    var thisName = $(this).attr("class"); 
    // Add them to the array if they aren't in it. 
    if ($.inArray(thisName, dictionary) == -1) { 
     dictionary.push(thisName); 
    } 
}); 

// Cycle through the array 
for(var obj in dictionary) { 
    var className = dictionary[obj]; 
    var total = 0; 
    // Cycle through all tr's with the current class, get the amount from each, add them to the total 
    $("table tr." + className).each(function() { 
     total += parseInt($(this).children(".td2").text()); 
    }); 
    // Append a td with the total. 
    $("table tr." + className).append("<td>Total: " + total + "</td>"); 
} 

提琴手(在屋顶上):http://jsfiddle.net/ABRsj/

+0

这更好,因为它匹配表上的任何类名,尽管我认为我们可以改进JS,但它肯定是更完整的例子。 – Phunky 2011-12-23 13:42:07

0

第一棒和ID在桌面上选择第一个与jQuery相匹配的ID始终是最有效的。

然后,你需要做的就是匹配类,将字符串解析为一个数字并添加它们。我创建了一个简单的例子,下面为您

http://jsfiddle.net/Phunky/Vng7F/

但你没有明确是你如何期望得到的TD类的价值,如果这是动态的,可以改变你可以使它更多才多艺,但希望这会让你对从这里走到哪里有一些了解。