Sums of columns

Sums of columns

antoniocibantoniocib Posts: 277Questions: 62Answers: 1

Hi guys,
I must to do a video the sums of 2 columns how to do?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    This example shows how to sum one column. Just repeat the code for each column.
    https://datatables.net/examples/advanced_init/footer_callback.html

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    Kevin i cant use footerCallback because i just use the searchpanes on the footer

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    You can use the same code in the drawCallback.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    i want to see the sums of columns in this footer with red rectangle i can?

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921
    Answer ✓

    Use the FooterCallback example I linked to originally. First get one column working then repeat the code for the other two columns.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    i add the code for one column but return me an error
    https://damoratraffico.netsons.org/nord/scrivania1.html

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    The error DataTables warning: table id={id} - Cannot reinitialise DataTable. provides a troubleshooting link:
    https://datatables.net/manual/tech-notes/3

    Did you follow the steps in the link? You will want the "Single initialisation" solution.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    okay, i resolved the proble,the code for the 3 sums is this?

        footerCallback: function ( row, data, start, end, display ) {
                var api = this.api(), data;
    
                var intVal = function ( i ) {
                        return typeof i === 'string' ?
                                i.replace(/[\$,]/g, '')*1 :
                                typeof i === 'number' ?
                                        i : 0;
                };
                pageTotal = api
                        .column( 7, { page: 'current'} )
                        .data()
                        .reduce( function (a, b) {
                                return intVal(a) + intVal(b);
                        }, 0 );
                pageTotal = api
                        .column( 8, { page: 'current'} )
                        .data()
                        .reduce( function (a, b) {
                                return intVal(a) + intVal(b);
                        }, 0 );
                pageTotal = api
                        .column( 9, { page: 'current'} )
                        .data()
                        .reduce( function (a, b) {
                                return intVal(a) + intVal(b);
                        }, 0 );
    
                // Update footer
                $( api.column( 7 ).footer() ).html(
                        pageTotal
                );
                $( api.column( 8 ).footer() ).html(
                        pageTotal
                );
                $( api.column( 9 ).footer() ).html(
                        pageTotal
                );
        },
    
  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    Looks good.

    Kevin

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    Nope return all 0

  • antoniocibantoniocib Posts: 277Questions: 62Answers: 1

    ehm, @kthorngren now works boh idk what r happened maybe my fault..

This discussion has been closed.