Error average NaN€

Error average NaN€

silenssilens Posts: 79Questions: 32Answers: 0

I have a field whose value is a number and the sign €, when I do the summation if I do it well but when I do the average it gives me the error.

"footerCallback": function (row, data, start, end, display) {//Aqui meto los subtatales y totales por pagina en el pie de la tabla
                var api = this.api(),
                data;
                var intVal = function (i) {
                    return typeof i === 'string' ? i.replace(/[\€,]|Kg/g, '') * 1 : typeof i === 'number' ? i : 0;
                };


jQuery.fn.dataTable.Api.register( 'average()', function () {
                    var data = this.flatten();
                    var sum = data.reduce( function ( a, b ) {
                    return  intVal(a*1) +  intVal(b*1); // cast values in-case they are strings
                    }, 0 );
  
                return (sum) / data.length;
                } );    

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 48,053Questions: 1Answers: 6,898 Site admin

    Can you link to a page showing the issue please? Is intVal actually defined in your average() function - its hard to tell from the above.

    Allan

  • silenssilens Posts: 79Questions: 32Answers: 0

    I'm sorry, right now I can not make a real example. I have lacked some information in the previous example that I put now.

    Column 6 from where I do the summation, is a field type: 1.56 €. My problem is that with the € symbol I get an error. IF I remove the symbol, it does it well.

    "footerCallback": function (row, data, start, end, display) {//Aqui meto los subtatales y totales por pagina en el pie de la tabla
                    var api = this.api(),
                    data;
                    var intVal = function (i) {
                        return typeof i === 'string' ? i.replace(/[\€,]|Kg/g, '') * 1 : typeof i === 'number' ? i : 0;
                    };
    
    jQuery.fn.dataTable.Api.register( 'average()', function () {
                        var data = this.flatten();
                        var sum = data.reduce( function ( a, b ) {
                        return  (a*1) +  (b*1); 
                        }, 0 );
      
                    return (sum) / data.length;
                    } );    
                        
                     media=tblLinALb.column( 6 ).data().average().toFixed(2);
                     $(api.column(6).footer()).html('<font color="white">Media: '+media+ '€</font>');
    
  • allanallan Posts: 48,053Questions: 1Answers: 6,898 Site admin
    Answer ✓

    Sounds like you need to remove the Euro symbol. The intVal function has a regex which will do that, but I'm not clear if that is actually being used or not. It appears not in your second post above.

    Allan

  • silenssilens Posts: 79Questions: 32Answers: 0
    edited June 14

    De esta forma lo he solucionado. Gracias

    jQuery.fn.dataTable.Api.register( 'count()', function () {
        var data = this.flatten();
        return  data.length;
    } );    
    
    precio = api.column(6, { 
        page: 'current'
        }).data().reduce(function (a, b) {
        return intVal(a) + intVal(b);
        }, 0);
    
    media=precio/tblLinALb.column( 6 ,{page:'current'}).data().count();
    
    
Sign In or Register to comment.