# DataTables select 合计选中行的数量

> 需求：如何在datatables中，我们通过select选中的行rows来合计单价、重量？ 例子： var weightSun = 0; stockTable.on( 'select', function ( e, dt, type, in…

- 原文：https://www.l2qq.com/article/81
- 发布：2019-10-12T05:57:14Z
- 更新：2020-06-12T09:51:01Z
- 作者：l2qq

---

需求：如何在datatables中，我们通过select选中的行rows来合计单价、重量？

例子：

```
var weightSun = 0;
stockTable.on( 'select', function ( e, dt, type, indexes ) {
var rowData = stockTable.rows( indexes ).data().toArray();
weightSun = weightSun + rowData[0].zhongliang;
console.log("rowData.zhongliang: " + rowData[0].zhongliang);
console.log("weightSun: " + weightSun);
} ).on( 'deselect', function ( e, dt, type, indexes ) {
var rowData = stockTable.rows( indexes ).data().toArray();
weightSun = weightSun - rowData[0].zhongliang;
console.log("rowData.zhongliang: " + rowData[0].zhongliang);
console.log("weightSun: " + weightSun);
} );
```

![QQ截图20191012135621.png](/assets/media/l2qq/1/share/0/1_20191012135647.png)

参考API地址：
https://datatables.net/extensions/select/examples/api/events.html
```
$(document).ready(function() {
var events = $('#events');
var table = $('#example').DataTable( {
select: true
} );
table
.on( 'select', function ( e, dt, type, indexes ) {
var rowData = table.rows( indexes ).data().toArray();
events.prepend( '<div><b>'+type+' selection</b> - '+JSON.stringify( rowData )+'</div>' );
} )
.on( 'deselect', function ( e, dt, type, indexes ) {
var rowData = table.rows( indexes ).data().toArray();
events.prepend( '<div><b>'+type+' <i>de</i>selection</b> - '+JSON.stringify( rowData )+'</div>' );
} );
} );
```
