数据表根据其他页脚值计算页脚

时间:2018-09-19 14:59:43

标签: jquery datatables

我有一个页脚回调来汇总列。我想根据两个页脚列总数的值再计算一个页脚列。

在页脚回调中可能吗? enter image description here

"footerCallback": function(row, data, start, end, display) {
                          var api = this.api();

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

                          api.columns('.sum', {
                            page: 'current'
                          }).every(function() {
                            var sum = this
                              .data()
                              .reduce(function(a, b) {
                                    var x = String(a);
                                        x = parseFloat( x.replace(/,/g,'') );
                                        x = Math.round(x * 100) / 100;


                                    var y = String(b);  //turn object into string
                                        y = parseFloat( y.replace(/,/g,'') ); //remove all commas
                                        y = Math.round(y * 100) / 100; //round to 2 decimal places

                                    theSum = (x + y).toFixed(2); //sum the two operands & set to 2 decimal places

                                return theSum.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                              }, 0);
                            //console.log(sum); //alert(sum);
                            $(this.footer()).html("$ " + sum);
                          });

//this part creates a average but it's based on all the rows, I need it to be based on the totals of two of the other columns
                          api.columns('.avg', {
                            page: 'current'
                          }).every(function() {

                            var numerator = this
                                    .data()
                                    .reduce( function(a, b) {
                                        return (intVal(a) + intVal(b));
                                    }, 0);

                            var denominator = this.data().length;

                            avg = numerator / denominator;
                            avg = avg.toFixed(2)

                            $(this.footer()).html("" + avg + "%");
                          });

0 个答案:

没有答案