Javascript dataTable使用页脚回调搜索个人,以合计列货币
我试图添加到带有页脚回调的表search individual以求和货币,但无法编写正确的代码 htmlJavascript dataTable使用页脚回调搜索个人,以合计列货币,javascript,search,datatables,sum,Javascript,Search,Datatables,Sum,我试图添加到带有页脚回调的表search individual以求和货币,但无法编写正确的代码 html 渲染引擎 浏览器 月台 引擎版本 CSS等级 价格 渲染引擎 浏览器 月台 引擎版本 CSS等级 价格 总数: 三叉戟 Internet Explorer 4.0 赢得95分+ 4. X R$1.000,00 三叉戟 Internet Explorer 5.0 赢得95分+ 5. C R$2.000,00 三叉戟 Internet Explorer 5.5 赢得95分+ 5.5 A. R$
渲染引擎
浏览器
月台
引擎版本
CSS等级
价格
渲染引擎
浏览器
月台
引擎版本
CSS等级
价格
总数:
三叉戟
Internet Explorer 4.0
赢得95分+
4.
X
R$1.000,00
三叉戟
Internet Explorer 5.0
赢得95分+
5.
C
R$2.000,00
三叉戟
Internet Explorer 5.5
赢得95分+
5.5
A.
R$500,00
JS
/*为表尾添加输入文本*/
$('#示例tfoot.footer th')。每个(函数(){
var title=$(this.text();
$(this.html(“”);
});
/*添加货币总数*/
变量表=$('#示例')。数据表({
//footerCallback的代码在这里,但它不起作用
“footerCallback”:函数(行、数据、开始、结束、显示){
var api=this.api(),数据;
var intVal=函数(i){
返回类型i==='string'?
i、 替换(/[\R$,]/g'')*1:
i的类型=='编号'?
i:0;
};
总计=空气污染指数
.第(5)栏
.data()
.减少(功能a、b){
返回intVal(a)+intVal(b);
}, 0 );
pageTotal=api
.column(5,{page:'current'})
.data()
.减少(功能a、b){
返回intVal(a)+intVal(b);
}, 0 );
$(api.column(5.custo()).html(
“$”+pageTotal+”($”+total+“total”)
);
}
});
/*当键盘设置更改时,进行个性化搜索*/
table.columns().every(函数(){
var=这个;
$('input',this.footer()).on('keyup change',函数(){
如果(that.search()!==this.value){
那个
.search(this.value)
.draw();
}
});
});
下面是不带总和货币的示例:
我想在该链接上使用sum currency的示例:
谢谢请执行以下示例:
const Table = $('#foo').DataTable({
. . . . . .,
. . . . . .,
drawCallback: function(){
Table.columns(5, {
page: 'current'
}).every(function() {
var sum = this
.data()
.reduce(function(a, b) {
var x = parseFloat(a) || 0;
var y = parseFloat(b) || 0;
return x + y;
}, 0);
console.log(sum);
$(this.footer()).html(sum);
});
}
});
在本例中,该列是第5列,您有:
$(api.column(5).custo())
什么是custo()
?Hi@develincontrante编辑custo的价格类,感谢您向我展示。但仍然不起作用您是否尝试过在reduce函数中使用console.log()来查看添加了什么?您的部分问题就在这里。请看这里的控制台日志:@develEnhantate我首先要做什么才能更改“.”to“,”以给出正确的结果
/* add input text for table footer */
$('#example tfoot.footer th').each( function () {
var title = $(this).text();
$(this).html( '<input type=\"text\"/>' );
});
/* add the sum of currency */
var table = $('#example').DataTable({
// code for footerCallback goes here but its not working
'footerCallback': function ( row, data, start, end, display ) {
var api = this.api(), data;
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\R$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
total = api
.column( 5 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
pageTotal = api
.column( 5, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
$( api.column( 5 ).custo() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
});
/* make the search indvidual when keyup change. */
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
});
});
const Table = $('#foo').DataTable({
. . . . . .,
. . . . . .,
drawCallback: function(){
Table.columns(5, {
page: 'current'
}).every(function() {
var sum = this
.data()
.reduce(function(a, b) {
var x = parseFloat(a) || 0;
var y = parseFloat(b) || 0;
return x + y;
}, 0);
console.log(sum);
$(this.footer()).html(sum);
});
}
});