简体   繁体   English

如何查找动态生成的多个表列的总和

[英]How to find the sum of dynamically generated multiple table columns

I have table with some data in an array to be displayed as below. 我有一些数组中的数据表,如下所示。 In this example, I display 2 table datas but it can be more than 2. I have to calculate the column population, collection 1 and collection 2 of each table and show then at the end of each table. 在此示例中,我显示2个表数据,但可以多于2个。我必须计算每个表的列填充,集合1和集合2,然后在每个表的末尾显示。

So far I have tried following code to calculate where I first calcuate the number of tables available by counting array. 到目前为止,我已经尝试了以下代码来计算我首先通过对数组进行计数来计算可用表的数量。

<script type="text/javascript">
var count = <?php echo json_encode(count($lists)); ?>;
var total = 0;
for(var i = 1; i <= count; i++)
{
    var available = $(".used-community .plant_counter_"+i);
    $.each(available, function(key, value)
    {
        total+=parseInt(available[key].innerText);
    });
    console.log(total);//edit variable name  - totala changed to total
}
</script>

 <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="47" class="rowList middle"> <td>Wangkha</td> <td>Soi Wangkha 3</td> <td class="plant_counter_1">100000</td> <td>31</td> <td>11315</td> </tr> <tr id="43" class="rowList middle"> <td>one</td> <td>one address</td> <td class="plant_counter_1">35000</td> <td>7</td> <td>2555</td> </tr> <tr id="46" class="rowList middle"> <td>Bang</td> <td>Bang khuwang Rd</td> <td class="plant_counter_1">6000</td> <td>2</td> <td>730</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="45" class="rowList middle"> <td>sap</td> <td>sap thawi</td> <td class="plant_counter_2">80000</td> <td>15</td> <td>5475</td> </tr> <tr id="44" class="rowList middle"> <td>two-nonthaburi</td> <td>nonthaburi</td> <td class="plant_counter_2">69000</td> <td>11</td> <td>4015</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> 

My code is calculating the row with class "plant_counter_1" correctly but for next table it continue adding the sum of table 1 as well. 我的代码正在正确计算类“ plant_counter_1”的行,但对于下一个表,它也继续添加表1的总和。 Can anybody teach me where I did mistake and I how can I find out the sum of other two columns 谁能教我我在哪里做错了,我如何找出其他两列的总和?

Thank You 谢谢

Just a small modification to your code and it will work. 只需对您的代码进行少量修改,它就会起作用。 Since you need multiple results, your total needs to be an array. 由于需要多个结果,因此总计必须是一个数组。 Please run attached code snippet for clarity. 为了清晰起见,请运行附带的代码段。

 var count =2;//change back to your php <?php echo json_encode(count($lists)); ?>; var total = new Array(); total.push(0); for(var i = 1; i <= count; i++) { total.push(0); var available = $(".used-community .plant_counter_"+i); $.each(available, function(key, value) { total[i]+=parseInt(available[key].innerText); }); total.push(0); console.log(total[i]); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="47" class="rowList middle"> <td>Wangkha</td> <td>Soi Wangkha 3</td> <td class="plant_counter_1">100000</td> <td>31</td> <td>11315</td> </tr> <tr id="43" class="rowList middle"> <td>one</td> <td>one address</td> <td class="plant_counter_1">35000</td> <td>7</td> <td>2555</td> </tr> <tr id="46" class="rowList middle"> <td>Bang</td> <td>Bang khuwang Rd</td> <td class="plant_counter_1">6000</td> <td>2</td> <td>730</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="45" class="rowList middle"> <td>sap</td> <td>sap thawi</td> <td class="plant_counter_2">80000</td> <td>15</td> <td>5475</td> </tr> <tr id="44" class="rowList middle"> <td>two-nonthaburi</td> <td>nonthaburi</td> <td class="plant_counter_2">69000</td> <td>11</td> <td>4015</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> 

Loop over each table and work within that table instance. 遍历每个表并在该表实例中工作。 The total data count is not really needed since it doesn't break down the number of tables. 总数据数并不是真正需要的,因为它不会分解表的数量。

Since you can easily isolate table instances I would suggest you use common class names across all tables and don't use incremental ones 由于您可以轻松隔离表实例,因此建议您在所有表中使用通用的类名,而不要使用增量类名

 $('table').each(function(){ var $table =$(this), $rows = $table.find('tbody tr'), $lastRowCells = $rows.last().children(), collectionTotals = $rows.not(':last').map(function(){ var $cells = $(this).children() return [[+$cells[3].textContent, +$cells[4].textContent]] }).get() .reduce(function(a,c){ $.each(c,function(i, val){ a[i] += val; }); return a; },[0,0]); $lastRowCells.eq(3).text(collectionTotals[0]); $lastRowCells.eq(4).text(collectionTotals[1]); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="47" class="rowList middle"> <td>Wangkha</td> <td>Soi Wangkha 3</td> <td class="plant_counter">100000</td> <td>31</td> <td>11315</td> </tr> <tr id="43" class="rowList middle"> <td>one</td> <td>one address</td> <td class="plant_counter">35000</td> <td>7</td> <td>2555</td> </tr> <tr id="46" class="rowList middle"> <td>Bang</td> <td>Bang khuwang Rd</td> <td class="plant_counter">6000</td> <td>2</td> <td>730</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="45" class="rowList middle"> <td>sap</td> <td>sap thawi</td> <td class="plant_counter">80000</td> <td>15</td> <td>5475</td> </tr> <tr id="44" class="rowList middle"> <td>two-nonthaburi</td> <td>nonthaburi</td> <td class="plant_counter">69000</td> <td>11</td> <td>4015</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> 

to make things a little bit of fun (I assumed the total rows have two extra misplaced columns) 使事情变得有趣(我假设总行有两个额外的错位列)

 ;$(function() { var tables = $('table > tbody'); var totals = new Array(tables.length); tables.each(function(nt,t) { var rows = $('tr',t); var totalElement = rows.splice(-1); rows = rows.map(function(nr, r) { return [$('td',$(r)).slice(-3) .map( function(nc,c) { return parseInt($(c).text()) || 0; }).toArray()]; }).toArray(); totals[nt] = rows.reduce( function(a, b) { return a.map(function(v,n) { return v + b[n]; }); }, [0,0,0] ); $('td',totalElement[0]).splice(-3).forEach(function(c,n) { $(c).text(totals[nt][n]); }); }); console.log(totals); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="47" class="rowList middle"> <td>Wangkha</td> <td>Soi Wangkha 3</td> <td class="plant_counter_1">100000</td> <td>31</td> <td>11315</td> </tr> <tr id="43" class="rowList middle"> <td>one</td> <td>one address</td> <td class="plant_counter_1">35000</td> <td>7</td> <td>2555</td> </tr> <tr id="46" class="rowList middle"> <td>Bang</td> <td>Bang khuwang Rd</td> <td class="plant_counter_1">6000</td> <td>2</td> <td>730</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> </tr> </tbody> </table> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="45" class="rowList middle"> <td>sap</td> <td>sap thawi</td> <td class="plant_counter_2">80000</td> <td>15</td> <td>5475</td> </tr> <tr id="44" class="rowList middle"> <td>two-nonthaburi</td> <td>nonthaburi</td> <td class="plant_counter_2">69000</td> <td>11</td> <td>4015</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> </tr> </tbody> </table> 

Well if I understand you correctly. 好吧,如果我理解正确的话。 You can store the total sum of each table into array. 您可以将每个表的总和存储到数组中。 I think the code explain alone... But I declare an array named 'total' outside of the for statement that contain the total of each table. 我认为代码可以单独解释...但是我在for语句之外声明了一个名为“ total”的数组,该数组包含每个表的总数。 When the foreach finish to sum all the values in tempTotal, then do a push to total array and save the operation of the first loop. 当foreach完成对tempTotal中的所有值求和时,请执行一次推入total数组并保存第一个循环的操作。 and the second... third... etc 第二个...第三个...等等

 var total = []; for(var i = 1; i <= 2; i++) { var tempTotal = 0; var available = $(".used-community .plant_counter_"+i); $.each(available, function(key, value) { tempTotal = tempTotal + parseInt(available[key].innerText); }); total.push(tempTotal); } console.log(total); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="47" class="rowList middle"> <td>Wangkha</td> <td>Soi Wangkha 3</td> <td class="plant_counter_1">100000</td> <td>31</td> <td>11315</td> </tr> <tr id="43" class="rowList middle"> <td>one</td> <td>one address</td> <td class="plant_counter_1">35000</td> <td>7</td> <td>2555</td> </tr> <tr id="46" class="rowList middle"> <td>Bang</td> <td>Bang khuwang Rd</td> <td class="plant_counter_1">6000</td> <td>2</td> <td>730</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> <table class="table" border="1"> <thead> <tr class="titlerow"> <th>Name</th> <th>Address</th> <th>Population</th> <th>Collection 1</th> <th>collection 2</th> </tr> </thead> <tbody class="used-community"> <tr id="45" class="rowList middle"> <td>sap</td> <td>sap thawi</td> <td class="plant_counter_2">80000</td> <td>15</td> <td>5475</td> </tr> <tr id="44" class="rowList middle"> <td>two-nonthaburi</td> <td>nonthaburi</td> <td class="plant_counter_2">69000</td> <td>11</td> <td>4015</td> </tr> <tr bgcolor="#66CCFF"> <td></td> <td></td> <td></td> <td class="col"></td> <td class="cap"></td> <td></td> <td></td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM