[英]how to sum datatables in codeigniter
the result just show $NaN ( $NaN total)
结果只显示
$NaN ( $NaN total)
I just copying code from datatables and I just change the column where I wanna SUM it.我刚刚从复制的代码数据表,我只是更改列在这里我想总结一下。 here my code before
这是我之前的代码
<script>
$(document).ready(function() {
$('#tableoperasional').DataTable( {
"footerCallback": function ( row, data, start, end, display ) {
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( 2 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 2, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
// Update footer
$( api.column( 2 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
});
});
</script>
I also updated the js lib, and still nothing happened.我也更新了js lib,仍然没有任何反应。 I hope your help.
我希望你的帮助。
Main problem主要问题
I think the problem lies within your intVal
function.我认为问题出在您的
intVal
函数中。 When a String
is passed into it, all $
and ,
chars are removed and the result is multiplied by 1
(which converts it into a Number
).当一个
String
传入它时,所有$
和,
字符都被删除,结果乘以1
(将其转换为Number
)。 However, if String.prototype.replace
does not remove all non-numeric characters, that conversion will fail and NaN
is returned.但是,如果
String.prototype.replace
没有删除所有非数字字符,则转换将失败并返回NaN
。
For example:例如:
var valueInColumn = '12,45%';
var nanResult = valueInColumn.replace(/[\$,]/g, '') * 1;
console.log(nanResult);
Instead of using implicit conversion ( * 1
), use explicit conversion via parseFloat
/ parseInt
.不使用隐式转换 (
* 1
),而是通过parseFloat
/ parseInt
使用显式转换。 It's clearer about the intention and also a bit safer than implicit conversion:它的意图更清晰,也比隐式转换更安全:
var intVal = function (i) {
return typeof i === 'string'
? parseFloat(i.replace(/[\$,]/g, '')) // <-- here
: typeof i === 'number'
? i
: 0;
}
In case this does not solve the problem, intVal
can be made safer/more strict.如果这不能解决问题,可以使
intVal
更安全/更严格。 Here's how:就是这样:
var safeIntVal = i => {
var rval = i;
if (typeof rval === 'string') {
rval = parseFloat(rval.replace(/[^\d\.]/g, '')); // <-- remove all non-numeric characters except dots and convert into a number
}
if (typeof rval !== 'number' || Number.isNaN(rval)) { // <-- if the result of the conversion isn't a number or is NaN, set it to zero
rval = 0;
}
return rval; // <-- return
}
The difference in both implementations becomes visible in the following snippet:在以下代码段中可以看出两种实现的差异:
var intVal = function ( i ) { return typeof i === 'string' ? i.replace(/[\\$,]/g, '') * 1 : typeof i === 'number' ? i : 0; }; var safeIntVal = function (i) { var rval = i; if (typeof rval === 'string') { rval = parseFloat(rval.replace(/[^\\d\\.]/g, '')); } if (typeof rval !== 'number' || Number.isNaN(rval)) { rval = 0; } return rval; } var testString = 'a23,3.65$b'; console.log('Original intVal result:', intVal(testString)); console.log('Modified safeIntVal result:', safeIntVal(testString));
Side notes旁注
You declare a data
variable inside the footerCallback
function that shadows the equally named argument that's passed to the callback.您在
footerCallback
函数中声明了一个data
变量,该变量footerCallback
了传递给回调的同名参数。 Also, the total
and pageTotal
variables look like implicit global variables (they are declared without the var
keyword).此外,
total
和pageTotal
变量看起来像隐式全局变量(它们声明时没有使用var
关键字)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.