Jump to content

Can't get JScript Column Total to work


Recommended Posts

Ok, I'm very new to adding javascript to caspio data forms. I've tried to insert the javascript outlined in the topic found here:

viewtopic.php?f=13&t=12180&p=13937&hilit=sum+column#p13937

But when I try to run it, it does nothing. I've changed the columns to the correct columns in my table. If I uncheck the "disable script debugging" in the options on my browser, I get the error,

Line 39:

Error: '10.innerHTML' is null or not an object

Column 10 is the column I'm trying to sum and it is not null. I've tried to change it to different columns to be sure I have the correct column, but it gives me the same result no matter what. I've tried this on IE7 and IE8. If somone could provide me a solution to this I would greatly appreciate it. Totalling columns is something I will need to do a lot of.

Thanks.

Link to comment
Share on other sites

I changed the 3 on line 39 to a 10 because that's the row I need to total. So the line says,

v_rev = cells[10].innerHTML;

Here is the whole jscript segment. I'm still at a loss on this so if someone can help me figure out what is wrong I would appreciate it.

Thanks.

function NumberFormatted(amount,decimal)

{

if(isNaN(amount))

i = 0.00;

else

{

var v_number = parseFloat(amount);

var v_minus = '';

if(v_number < 0)

v_minus = '-';

v_number = Math.abs(v_number);

v_number = Math.round(v_number*Math.pow(10,decimal));

v_number = v_number/Math.pow(10,decimal);

v_numStr = new String(v_number);

v_decStr = new String(Math.pow(10,decimal));

if(v_numStr.indexOf(".") < 0)

v_numStr = v_numStr + "." + v_decStr.substr(1,v_decStr.length);

else

v_numStr = v_numStr + v_decStr.substr(1,v_decStr.length);

return (v_minus + v_numStr.substr(0,v_numStr.indexOf(".") + decimal + 1));

}

}

function f_calTotal()

{

var v_totalRev = 0;

var v_rev = 0;

var cas_form = document.getElementById("cb_resultTotal");

if (cas_form.getElementsByTagName("table").length > 0)

{

var cas_rows = cas_form.getElementsByTagName("table")[1].getElementsByTagName("tr");

for(var rowIndex=1; rowIndex < cas_rows.length; rowIndex++)

{

var cells = cas_rows[rowIndex].getElementsByTagName("td");

/* 1 – change value inside brackets to choose column to calculate sum */

v_rev = cells[10].innerHTML;

if ( v_rev != " " && !isNaN(v_rev.substr(1)))

v_totalRev = v_totalRev + parseFloat(v_rev);

}

var v_nrow = cas_rows.length;

cas_form.getElementsByTagName("table")[1].insertRow(v_nrow);

var o_lastRow = cas_form.getElementsByTagName("table")[1].rows[v_nrow];

o_lastRow.style.background = "#385C7E";

o_lastRow.insertCell(0);

o_lastRow.insertCell(1);

o_lastRow.insertCell(2);

o_lastRow.insertCell(3);

o_lastRow.insertCell(4);

o_lastRow.insertCell(5);

o_lastRow.insertCell(6);

o_lastRow.insertCell(7);

o_lastRow.insertCell(8);

/* 2 – Display the “Total†label (2 lines below) */

var v_colText = o_lastRow.insertCell(9);

v_colText.innerHTML = "

Total
";

var v_colValue = o_lastRow.insertCell(10);

/* 3 – Display the result of the calculation (2 lines below) */

v_colValue.innerHTML = "

$" + NumberFormatted(v_totalRev,2) + "
";

o_lastRow.insertCell(11);

o_lastRow.insertCell(12);

}

}

f_calTotal();

Link to comment
Share on other sites

Actually there are 13 columns. Column #10 is the one I'm trying to total. Unfortunately, I can't post the link to where the datapage is because it cotains data I don't want to broadcast to whoever wants to see it. I can answer any questions though, if someone has some suggestions on why I'm getting this error.

Thanks.

Link to comment
Share on other sites

hi,

if you want to calculate the results in column #10, then you shoud refer to it as index 9 (remember, the first column is 0, the second is 1, and so on... then the tenth column is 9)

so your code should be:

var v_colValue = o_lastRow.insertCell(9);

also make sure all other indexes.

finally, make sure the field values are of type "number" or "text" (and have numeric values)

Link to comment
Share on other sites

hello,

Thanks for the response. It is the correct column. It is column 10 starting with 0 as the first column. The column is of format number and there are no nulls in the column. What am I missing here? This can't be such a complex problem. There has to be something small that I'm missing if this code is supposed to work at all.

Thanks.

Link to comment
Share on other sites

The error I posted was from a delpoyed version in IE. But now that I try it again within Caspio bridge, I realized it's a different error there. The error I get when running in Caspio is:

Line: 605

Error: 'getElementsByTagName(...).1' is null or not an object

Here is a screenshot of the error and a sample of what the report the javascript is supposed to be summing from looks like.

size = 300http://ReportError.gif[/img]

It's supposed to be summing column 10 (Plan Comm), but as you can see it's not making it past the above error. It's not even getting to the point of adding the label "total".

Thanks for the continued help.

Link to comment
Share on other sites

My advise is: start all over. Copy and paste the JS code as it is in the forums. Then start making small changes to it and immediatelly checking in inside Caspio (not in a deployed web page). Once you get it to work inside Caspio, deploy it and check.

My best guess it that you have more than one table in the webpage you have the Datapage deployed. So the table indexes in the code should be updated (lines 34, 46,47) accordingly.

Now, since you mention you are getting errors inside Caspio as well, I advise you to start over and take smaller steps to see if you can pinpoint where the error is appearing.

Best,

sbelini.

Link to comment
Share on other sites

  • 5 years later...
  • 3 weeks later...
  • 3 months later...

This thread is still relevant because we still can't aggregate calculated fields based on subqueries (which was added and then retracted as a feature to Bridge 9).

 

The OP was referring to the Caspio provided JS from the forum

http://forums.caspio.com/index.php/topic/3171-js-calculate-column-sum-in-the-result-page/

 

The script can be fixed to work if you change the table array index from [1] to [2] in the three places where it is used in the script, as in

var o_lastRow = cas_form.getElementsByTagName("table")[1].rows[v_nrow];

 

However,

  • Grouping breaks the script
  • it's not clear how to use this for multiple columns
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...