How to sum render data

How to sum render data

qqwwqqww Posts: 3Questions: 1Answers: 0

i need to sum data from render: "[].ves" now its look like 90,60 i need in this case 150. Sorry for english.

my code:

->join(
    Mjoin::inst( 'chast' )
        ->link( 'razdelka.id', 'chast.razdelka' )
        ->fields(
           Field::inst( 'ves' )
        )
)

client:

columns: [
{ data: "chast", render: "[].ves" }
]

Help me! i weaste all day.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,836Questions: 1Answers: 10,518 Site admin

    You need to use columns.render as a function - for example:

    {
      data: 'chast',
      render: function ( d ) {
        var sum = 0;
        for ( var i=0 ; i<d.length ; i++ ) {
          sum += d[i].ves;
        }
        return sum;
      }
    }
    

    Allan

  • qqwwqqww Posts: 3Questions: 1Answers: 0

    Thanks for reply!

    i try to use you example, and they return me 09060 (before i put 90 and 60), not sum

  • allanallan Posts: 63,836Questions: 1Answers: 10,518 Site admin
    Answer ✓

    That suggests that ves is a string. You'd need to cast it as a number. You can do that simply by using: d[i].ves*1.

    Allan

  • qqwwqqww Posts: 3Questions: 1Answers: 0

    thanks Allan!

This discussion has been closed.