Percent calculation in the footer

Hi all,

I’m struggeling with some code.
I have 3 columns
Fisrt column is “amount to pay”
Second column is amount “already paid”
Thirth column is “percentage paid”
The percentace paid is taken out the XML file so there are no calculations in the grid.attachHeader.

In the footer I used #stat_total to calculate the first two colums. So now that I have a total of “Amount to pay” and total of “Already paid” I want the percentage op those 2.

This is what I got now

function percentage(){
getFooterLabel1=parseFloat(myGrid.getFooterLabel(1));
getFooterLabel2=parseFloat(myGrid.getFooterLabel(2));
setFooterLabel =(getFooterLabel2/getFooterLabel1)*100;
setFooterToString = setFooterLabel.toString();
myGrid.setFooterLabel(3,setFooterToString);
}

As result in the last footer column I get “NaN”.
It seems that it will not parse to int.

the number in the first column is 26.379.000,00
the number in the second column is 22.798.807,40

Can it be that the problem is with the decimal point or the comma?

Anyway thx for your time to read this.

Update
I found out that if I want to see the label it retuns an empty string. If I set something manual in the footer then I see the label string.

So I think using the #stat_total does not set the footer label.

Update: Forgot to post my solution.

What I do here is manual calculate the sum of the column, then do the percentage calculation of 2 columns, then format the result with decimal point and add a “%” sing to it. Then return it back to the footer label.

the reason why I manual calculate the sum is that I didn’t found a way to get the footer label to a vaiable. The footerlabel is set by #stat_total. If the footerlabel is set manualy then it’s possible to get that label and set it in a var for later use.

I hope I can be of some help to someone.
If you have some tips about this code to improve it, plz let me know as I’m still in a learning phase.

greetz

function procentInFooter(){
var col13 = sumColumn(13);
var col14 = sumColumn(14);
var col15 = ToProcent(col14,col13);
myGrid.setFooterLabel(3,col15);

					var col16 = sumColumn(16);
					var col14b = sumColumn(14);
					var col17 = ToProcent(col16,col14b);
					myGrid.setFooterLabel(5,col17);
					
					var col16b = sumColumn(16);
					var col18 = sumColumn(18);
					var col19 = ToProcent(col18,col16b);
					myGrid.setFooterLabel(7,col19);
					
					var col20 = sumColumn(20);
					var col21 = sumColumn(21);
					var col22 = ToProcent(col21,col20);
					myGrid.setFooterLabel(10,col22);						
				}
				
				
				function sumColumn(columnIndex){
					var teller=myGrid.getRowsNum();
					var som = 0;					
						for(var i=0; i<teller; i++){
							var findCell = myGrid.cellByIndex(i,columnIndex);
							var getVal = findCell.getValue();
							som = som + parseFloat(getVal);								
						}
					return som;						
				}
				
				function ToProcent(col1,col2){
					
					var floatNumber = 0;
					var floatNumberFixed = 0;
					if(col2==0){
						floatNumberFixed = "Error. div by zero";
						return floatNumberFixed
					}
					else{
						floatNumber =((parseFloat(col1)/parseFloat(col2)) * 100).toFixed(2);
						floatNumberFixed = floatNumber.replace('.',',') + "%";
					return floatNumberFixed;
					}
				}

Hi,

Your origin code actually is a correct one, please check the next snippet
snippet.dhtmlx.com/f41093210

There are two points which can cause an issue

  • if you are loading data from a server side, be sure to use the loading callback to recalculate values in the footer.
  • if you are using number formatting, you need to use a custom function instead of parseFloat

Ah oke that was the problem I use number formatting.
Well it time to make some work on learning how to make some custom functions to replace the parseFloat.

Anyway thx alot for your time and info.