[英]Jquery Sum Values based on dropdown list filter
i have a dropdown filter that works and filter the data on my jquery datatable, but i was wondering how can i make that each time someone select a value from my dropdown list and this gets filter i need to sum all values from the filtered datatable??? 我有一个下拉过滤器,可以工作并过滤我的jquery数据表中的数据,但是我想知道每次有人从我的下拉列表中选择一个值时,我如何才能使它得到过滤器,我需要对过滤后的数据表中的所有值求和? ?? how can i do this? 我怎样才能做到这一点?
My table is created using Jquery datatables I need to sum all values from my 'td' 'Total' based on the value of the user selects how can i do this in javascript or jquery? 我的表是使用Jquery数据表创建的,我需要根据用户的值将“ td”“总计”中的所有值相加,然后选择如何在javascript或jquery中执行此操作?
This is my Dropdown list : 这是我的下拉列表:
<select id="selectorpdv">
<option value="">Todos</option>
<option value="01/02/2016">@item</option> // USER SELECTS THIS VALUE
<option value="02/02/2016">@item</option>
</select>
This is my filter code: 这是我的过滤器代码:
$('#selectorpdv').on('change', function () {
dataTable.columns(2).search(this.value).draw();
});
This is my table code: 这是我的表代码:
<table class='table datatable-column-search-inputs table-hover table-striped table-bordered'>
<thead>
<tr>
<td>Con</td>
<td class='thead_search'>Platillo</td>
<td class='thead_select'>Pdv</td>
<td class='thead_select'>Rid</td>
<td >PV</td>
<td>0</td>
<td >Total</td>
<td class='sum'>Venta</td>
<td class='fechas'>Fecha</td>
</tr>
</thead>
<tfoot>
<tr>
<th colspan="4" style="text-align:left">Total:</th>
<th></th>
</tr>
</tfoot>
</table>
There is an api for the same provided below : 下面提供了相同的api:
dataTable.column( i ).data().sum();
If above not work in your current version of datatables library please use code below : 如果以上方法在当前版本的数据表库中不起作用,请使用以下代码:
function sumColumn() {
var dataArray = dataTable._('td:nth-child(4)', {"filter": "applied"});
var sum = 0;
for (var i=0, len=dataArray.length; i < len; i++) {
sum += +dataArray[i];
}
alert(sum);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.