需求:如何在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);
} );
参考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>' );
} );
} );
最新评论 0