Datatable row grouping subtotal based on value inside td span tag

Datatable row grouping subtotal based on value inside td span tag

crazyx7crazyx7 Posts: 1Questions: 1Answers: 0
edited June 2018 in Free community support

I have tried this, but it only work when the value displayed inside td without any tag.

rowGroup: {
                 startRender:function(rows,group){
                     return group +' ( '+rows.count()+' )';
                 },
                 endRender: function ( rows, group ) {

                     var intVal = function ( i ) {
                         return typeof i === 'string' ?
                             i.replace(/[\$,]/g, '')*1 :
                             typeof i === 'number' ?
                                 i : 0;
                     };
                     var sub = rows
                         .data()
                         .pluck(1)
                         .reduce( function (a, b) {
                             return intVal(a) + intVal(b);
                         }, 0);


                     return $('<tr/>')
                         .append( '<td>Sub-total for '+group+'</td>' )
                         .append( '<td>'+sub+'</td>' )          
                         .append('<tr></tr>')  
                 },
                 dataSrc: 0
             },

How can i sum up the value of the column's row when the value is inside the span tag?

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @crazyx7 ,

    You just need to get inside the element, so change

    return intVal(a) + intVal(b);
    

    to be

    return intVal(a) + intVal($(b).text());
    

    as in this example here,

    Cheers,

    Colin

This discussion has been closed.