Check if table in not empty inside footerCallback

Check if table in not empty inside footerCallback

itajackassitajackass Posts: 162Questions: 48Answers: 3
edited February 2018 in Free community support

I've now this working footerCallback:

                                           $('#table').DataTable( {
                                           footerCallback: function(row, data, start, end, display) {
                                          var api = this.api();
                                          var footer = $(this).append('<tfoot><tr></tr></tfoot>');
                                          this.api().columns().every(function () {
                                              var column = this;
                                              if ($(column.header()).hasClass('sum')) {
                                                    var sum = column
                                                      .data()
                                                      .reduce(function(a, b) {
                                                        var x = parseFloat(a) || 0;
                                                        var y = parseFloat(b) || 0;
                                                        return x + y;
                                                      }, 0);
                                              } else {
                                                  var sum = "";
                                              }
                                              $(footer).append('<th class="text-left">'+sum+'</th>');
                                          });
                                  }
                                             });

When the table (built from ajax) is not empty, all is ok. But if it is empty result, the footer break!
How can i check if table is not empty INSIDE the footerCallback? I've tried if ( !this.data().any() ) without success
I'd like to prevent footer creation if table is empty

Answers

This discussion has been closed.