gpt4 book ai didi

jquery - 如何使用页脚回调对数据表中的某些行求和

转载 作者:行者123 更新时间:2023-12-03 22:38:23 25 4
gpt4 key购买 nike

我正在使用数据表。我想对一些列进行求和,并希望显示在报告的底部。我搜索很多东西。然后我在数据表中找到了新的页脚回调函数。我用过那个。但我的输出还没有准备好..

我的代码如下,

function Databind(Pdestroy) {debugger;
var destroy = false;
if (Pdestroy == "1")
destroy = true;

var oTable = $('.datatable').dataTable({
"bJQueryUI": true,
'bServerSide': true,
"bDestroy": destroy,
"iDisplayLength": 10,
"sPaginationType": "full_numbers",
'sAjaxSource': '<%= Url.Action("listcount", "Home") %>',
"bFilter": true,
"aoColumnDefs": [{ 'bSortable': false, 'aTargets': [0, 1, 2, 7, 8, 9, 10, 11]}],

"fnRowCallback": function (nRow, aData, iDisplayIndex) {
var oSettings = oTable.fnSettings();
$("td:first", nRow).html(oSettings._iDisplayStart + iDisplayIndex + 1);
return nRow;
},
"footerCallback": function (row, aData, start, end, iDisplayIndex) {
var api = this.api(),
data;

// Remove the formatting to get integer data for summation
var intVal = function (i) {
return typeof i === 'string' ? i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0;
};

// Total over all pages
total = api.column(4)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
});

// Total over this page
pageTotal = api.column(4, {
page: 'current'
})
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);

// Update footer
$(api.column(5).footer()).html(
'$' + pageTotal + ' ( $' + total + ' total)');
}
});
}

这里Row显示未定义。并且也没有显示错误。但输出没有显示。我已附上输出的屏幕截图..

enter image description here

要获取值,我应该进一步处理什么?

最佳答案

使用sum plugin而不是重新发明轮子:)

jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
return this.flatten().reduce( function ( a, b ) {
if ( typeof a === 'string' ) {
a = a.replace(/[^\d.-]/g, '') * 1;
}
if ( typeof b === 'string' ) {
b = b.replace(/[^\d.-]/g, '') * 1;
}
return a + b;
}, 0 );
} );

由于您只想在每次重绘表格时更新某个页脚列,因此您只需要一个简单的 drawCallback :

drawCallback: function() {
var api = this.api();

// Total over all pages
var total = api.column(4).data().sum();

// Total over this page
var pageTotal = api.column(4, {page:'current'}).data().sum();

$(api.column(5).footer()).html('$' + pageTotal + ' ( $' + total + ' total)');
}

关于jquery - 如何使用页脚回调对数据表中的某些行求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32962506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com