Jump to content

using JS to conditionally sum fields in a details report data page


Recommended Posts

What I am trying to accomplish in my details report data page is to Sum the weight of each product.

s1 = product ID for the first item in the sale

s1w = product weight or units for the first item in the sale

I can not just sum all my "sw" fields because each one could possibly be tied to a different product. I have come up with the following JS solution, which is intended to run when an HTML button is clicked. Here is what I have:

In an HTML block at the top of the page:

<input type="button" value="product 1 weight" onclick="prod1w()" />

In the footer of the page:

<script>
function prod1w()
{
var v_s1 = parseFloat(document.getElementById("InsertRecords1").value);
var v_s2 = parseFloat(document.getElementById("InsertRecords2").value);
var v_s3 = parseFloat(document.getElementById("InsertRecords3").value);
var v_s4 = parseFloat(document.getElementById("InsertRecords4").value);
var v_s5 = parseFloat(document.getElementById("InsertRecords5").value);
var v_s6 = parseFloat(document.getElementById("InsertRecords6").value);
var v_s7 = parseFloat(document.getElementById("InsertRecords7").value);
var v_s8 = parseFloat(document.getElementById("InsertRecords8").value);
var v_s9 = parseFloat(document.getElementById("InsertRecords9").value);
var v_s10 = parseFloat(document.getElementById("InsertRecords10").value);

if (v_s1 == 1)

var v_prod1s1w = parseFloat(document.getElementById("InsertRecords1w").value);
}
else

var v_prod1s1w = '0'; 
}
if (v_s2 == 1)
{
var v_prod1s2w = parseFloat(document.getElementById("InsertRecords2w").value);
}
else
{
var v_prod1s2w = '0';
}
if (v_s3 == 1)
{
var v_prod1s3w = parseFloat(document.getElementById("InsertRecords3w").value);
}
else
{
var v_prod1s3w = '0';
}
if (v_s4 == 1)
{
var v_prod1s4w = parseFloat(document.getElementById("InsertRecords4w").value);
}
else
{
var v_prod1s4w = '0';
}
if (v_s5 == 1)
{
var v_prod1s5w = parseFloat(document.getElementById("InsertRecords5w").value);
}
else
{
var v_prod1s5w = '0';
}
if (v_s6 == 1)
{
var v_prod1s6w = parseFloat(document.getElementById("InsertRecords6w").value);
}
else
{
var v_prod1s6w = '0';
}
if (v_s7 == 1)
{
var v_prod1s7w = parseFloat(document.getElementById("InsertRecords7w").value);
}
else
{
var v_prod1s7w = '0';
}
if (v_s8 == 1)
{
var v_prod1s8w = parseFloat(document.getElementById("InsertRecords8w").value);
}
else
{
var v_prod1s8w = '0';
}
if (v_s9 == 1)
{
var v_prod1s9w = parseFloat(document.getElementById("InsertRecords9w").value);
}
else
{
var v_prod1s9w = '0';
}
if (v_s10 == 1)
{
var v_prod1s10w = parseFloat(document.getElementById("InsertRecords10w").value);
}
else
{
var v_prod1s10w = '0';
}

var v_prod1w = v_prod1s1w + v_prod1s2w + v_prod1s3w + v_prod1s4w + v_prod1s5w + v_prod1s6w + v_prod1s7w + v_prod1s8w + v_prod1s9w + v_prod1s10w;

document.getElementById("InsertRecordprod1w").value = (v_prod1w);
}

</script>

Update:

The problem was that the Element ID in this data page is EditRecord, I changed that out but am now running  into a different issue.

I am going to have about 20 different product ID's. I started to expand on this and added a second product ID function code segment.

My buttons are working and values are coming into my prod1w and prod2w fields.

However, something is going wrong in the calculation. The attachment shows whats happening. Based on what was entered here prod1w and prod2w should be 11.5

To add to the confusion, when products are entered in order (so s1,s2 and s3 = 1) the code works and you get the correct answer. However that is not how users will be submitting the information. I tried adjusting the else values from '0'; to just 0; but that broke it. I tried changing this line to:

document.getElementById("InsertRecordprod1w").value = math.round(v_prod1w);

Which lead to the same results.

Any ideas? 

Any and all help is greatly appreciated.

Untitled.png

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...