当前位置: 首页> 科技> 能源 > 上海设计院_免费制作app的手机软件_线上广告平台_网络媒体广告代理

上海设计院_免费制作app的手机软件_线上广告平台_网络媒体广告代理

时间:2025/7/10 3:25:53来源:https://blog.csdn.net/weixin_46001736/article/details/144033907 浏览次数:0次
上海设计院_免费制作app的手机软件_线上广告平台_网络媒体广告代理

一、监听行内文本框,进行行内数据修改

效果

修改数量、单价会自动计算金额(金额=数量*单价)

实现

1、增加行的class

2、数据监听、修改数值

"initComplete": function() {// 监听数量和单价输入框的变化$(document).on('input', '.quantity, .price', function() {var row = $(this).closest('tr');var quantity = parseFloat(row.find('.quantity').val()) || 0;var price = parseFloat(row.find('.price').val()) || 0;var lineAmount = quantity * price;// 更新金额输入框row.find('.line_amount').val(lineAmount.toFixed(2));});}

解决新增行或删除行,数据丢失

"initComplete": function() {$(document).on('input', '.quantity, .price', function() {var row = $(this).closest('tr');var quantity = parseFloat(row.find('.quantity').val()) || 0;var price = parseFloat(row.find('.price').val()) || 0;var lineAmount = quantity * price;// 更新金额输入框row.find('.line_amount').val(lineAmount.toFixed(2));// 更新 allData 数组中的 line_amountvar rowIndex = row.index();let allData = table.rows().data().toArray();allData[rowIndex].line_amount = lineAmount.toFixed(2);});
}

 

二、计算行总和

效果

方法一:遍历求和

 遍历全部金额特有的选择器(这里使用的是class="line_amount"),进行累加

"initComplete": function() {// 获取总金额function getallcount() {var total = 0;//遍历所有class="line_amount"的值,进行累加$('.line_amount').each(function() {var amount = parseFloat($(this).val());if (!isNaN(amount)) {total += amount;}});console.log(total)}
}

在需要的地方进行引用

例如

"initComplete": function() {$(document).on('input', '.quantity, .price', function() {var row = $(this).closest('tr');var quantity = parseFloat(row.find('.quantity').val()) || 0;var price = parseFloat(row.find('.price').val()) || 0;var lineAmount = quantity * price;// 更新金额输入框row.find('.line_amount').val(lineAmount.toFixed(2));// 更新 allData 数组中的 line_amountvar rowIndex = row.index();let allData = table.rows().data().toArray();allData[rowIndex].line_amount = lineAmount.toFixed(2);// 更新总和getallcount();});
}

方法二:使用DataTable的draw 事件

var table = $('#mytable').DataTable({// ... 其他配置 ..."initComplete": function(settings, json) {// ... 初始化代码 ...// 绑定 draw 事件table.on('draw', function() {var api = this.api();//例如获取第列的总和var total = api.column(7).data().reduce(function(a, b) {return a + (b ? parseFloat(b) : 0);}, 0);console.log(total.toFixed(2));});}
});

关键字:上海设计院_免费制作app的手机软件_线上广告平台_网络媒体广告代理

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: