Datatables вычисляют нижний колонтитул на основе других значений нижнего колонтитула - PullRequest
0 голосов
/ 19 сентября 2018

У меня есть обратный вызов нижнего колонтитула, работающий для суммирования столбцов.Я хотел бы рассчитать еще один столбец нижнего колонтитула на основе значений двух итогов столбца нижнего колонтитула.

Возможно ли это в обратном вызове нижнего колонтитула?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 + "%");
                          });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...