PDA

View Full Version : How to add values with renderer function



ludhiana
14 May 2008, 5:25 AM
Hi,
I am trying to add column values.Substration, Multiplication, division works fine but when I put '+', instead of adding it concats the string. if p1 is 2 and p2 is 4 then the sum should be 6 but I get 24. Please help.



allcols.push({header: 'sum',renderer: function(v, params, record){
return (record.data.p1 + record.data.p2);

mjlecomte
14 May 2008, 6:26 AM
How about:

allcols.push({header: 'sum',renderer: function(v, params, record){
return (record.data.p1 + record.data.p2 - 0);
If not that exactly, some form of subtracting zero from the individual components so it force converts the data from strings to values.
You could also try:

allcols.push({header: 'sum',renderer: function(v, params, record){
return ((+record.data.p1) + (+record.data.p2));
Allegedly it's faster to use a unary like that to force convert the data.

ludhiana
14 May 2008, 8:31 AM
it is working now.

mjlecomte
14 May 2008, 9:06 AM
Just curious, do both ways work, I wasn't sure if the first way did (if the last operation trumps the others and forces it to use values or not).