Jump to content

Count The Quantities of Each Selected Value


Recommended Posts

I have 12 fields in total - IG1 - IG6, which are dropdown fields and IG1_Qty - IG6_Qty - number input fields, they coincide with each other. 

The IG fields have 3 possible values - 1, 2, or 3. The Qty field is a number field to state how many of those IGs they want. In Example, the user would select '2' for IG1 and put in 4 for IG1_Qty, showing that they want 4 of the type 2 IGs. Now, the sum field for type 2 IGs would show 4. If we went on to give IG2's value to 2 as well, with a quantity of 1, then the sum field for type 2 IGs would show 5 (4 + 1). 

Essentially, I want to sum up the selections for the 3 possible IG dropdown values with the quantities selected for each. 

I was able to create an array with the 6 IG dropdown fields and count how many of each possible values were selected. However, adding in the Quantities for each IG into that number is where I ran into the problem, so I had to change the script entirely. This is my latest try: 

var ig1v = document.querySelector('select[id^="InsertRecordIG_Sheet_Table_IG1_"]'); //IG1 field - possible values are 1, 2, or 3
var vf1 = document.getElementById("cbParamVirtual22"); //This is where the sum value for IG Type 1 will go
var vf2 = document.getElementById("cbParamVirtual23"); //Type 2
var vf3 = document.getElementById("cbParamVirtual21"); //Type 3
var ig1q = document.getElementById("InsertRecordIG_Sheet_Table_IG1_Qty"); //How many of the selected IG type
var addNew1 = true;

vf1.value = 0; //Setting the value to 0 so that the other calculated fields in the form don't load with an error.

//This first eventListener/function is to automatically give 'Qty' a value if the IG dropdown isn't null anymore. This is just to automatically set Qty to 1 instead of blank when the user selects an IG value for the first time - UX
document.querySelector('select[id^="InsertRecordIG_Sheet_Table_IG1_"]').addEventListener("change", function () {
  if (addNew1){
    ig1q.value = 1;
    addNew1 = false;
  }
  ig1q.dispatchEvent(new Event('change'));
});

//This function is what I am using to count the number of each IG value. If IG = 1, then add the IG1_Qty to the type 1 sum field (vf1), etc. 
document.getElementById('InsertRecordIG_Sheet_Table_IG1_Qty').addEventListener("change", function calcGroups1(){
  if (ig1v.value == 1){
    vf1.value = +vf1.value + +ig1q.value;
   } else if (ig1v.value == 2){
    vf2.value = +vf2.value + +ig1q.value;
   } else if (ig1v.value == 3){
    vf3.value = +vf3.value + +ig1q.value;
   }
});

I have 6 of both of those functions for each IG field. Please forgive the ugly JS, I am sure there is a much better way to write this, but I have no clue how. :D

The problems I am now having is that the Sum fields (vf1-vf3) are adding every change. See how the first function in the script sets Qty to 1 when the corresponding IG field is given a value - this is correctly being added to the vf1 totals field. But, if I change the quantity from 1 to another number, it adds it instead of just changing the value. So, when the Qty field is automatically set to 1, and then you change it to 4, the vf1 totals field would show 5 instead of 4. If you changed the 5 to a 4, it would show 9.

Also, if the IG type is changed, the totals values are not updated for the previously selected value. Say I have IG1 = 1, Qty = 4. Now VF1 (sums of 1) will show 4. But, if I change IG1 = 2 with the same Qty, now VF2 will show 4, but VF1 will also still show 4. I'm not sure how to go about deleting the value from the previous sum field when the IG type is changed, or when the quantity is changed. 

I apologize for this being so confusing, but it is making my head turn into mush and I could use any of the help I can get. 

Link to comment
Share on other sites

Trying to think of other possible options... I've found a couple posts on SO where you can group an object array by one value and sum up the other values. However, I'm not too sure how to get the 'results' properties into my 3 totals fields.

This script groups the array by Type and then sums up the Qtys of each. It produces an object ... ex {type: 1, qty: 14}. Now I need to be able to put those values into my virtual fields...

var igT = [
  {type: ig1v.value, qty: ig1q.value},
  {type: ig2v.value, qty: ig2q.value},
  {type: ig3v.value, qty: ig3q.value},
  {type: ig4v.value, qty: ig4q.value},
  {type: ig5v.value, qty: ig5q.value},
  {type: ig6v.value, qty: ig6q.value}
  ];

var result = [];
igT.reduce(function(res, value) {
  if (!res[value.type]) {
    res[value.type] = { type: value.type, qty: 0 };
    result.push(res[value.type])
  }
  res[value.type].qty += +value.qty;
  return res;
}, {});
console.log(result)
});

 

Link to comment
Share on other sites

3 hours ago, kpcollier said:

I have 12 fields in total - IG1 - IG6, which are dropdown fields and IG1_Qty - IG6_Qty - number input fields, they coincide with each other. 

The IG fields have 3 possible values - 1, 2, or 3. The Qty field is a number field to state how many of those IGs they want. In Example, the user would select '2' for IG1 and put in 4 for IG1_Qty, showing that they want 4 of the type 2 IGs. Now, the sum field for type 2 IGs would show 4. If we went on to give IG2's value to 2 as well, with a quantity of 1, then the sum field for type 2 IGs would show 5 (4 + 1). 

Essentially, I want to sum up the selections for the 3 possible IG dropdown values with the quantities selected for each. 

I was able to create an array with the 6 IG dropdown fields and count how many of each possible values were selected. However, adding in the Quantities for each IG into that number is where I ran into the problem, so I had to change the script entirely. This is my latest try: 

var ig1v = document.querySelector('select[id^="InsertRecordIG_Sheet_Table_IG1_"]'); //IG1 field - possible values are 1, 2, or 3
var vf1 = document.getElementById("cbParamVirtual22"); //This is where the sum value for IG Type 1 will go
var vf2 = document.getElementById("cbParamVirtual23"); //Type 2
var vf3 = document.getElementById("cbParamVirtual21"); //Type 3
var ig1q = document.getElementById("InsertRecordIG_Sheet_Table_IG1_Qty"); //How many of the selected IG type
var addNew1 = true;

vf1.value = 0; //Setting the value to 0 so that the other calculated fields in the form don't load with an error.

//This first eventListener/function is to automatically give 'Qty' a value if the IG dropdown isn't null anymore. This is just to automatically set Qty to 1 instead of blank when the user selects an IG value for the first time - UX
document.querySelector('select[id^="InsertRecordIG_Sheet_Table_IG1_"]').addEventListener("change", function () {
  if (addNew1){
    ig1q.value = 1;
    addNew1 = false;
  }
  ig1q.dispatchEvent(new Event('change'));
});

//This function is what I am using to count the number of each IG value. If IG = 1, then add the IG1_Qty to the type 1 sum field (vf1), etc. 
document.getElementById('InsertRecordIG_Sheet_Table_IG1_Qty').addEventListener("change", function calcGroups1(){
  if (ig1v.value == 1){
    vf1.value = +vf1.value + +ig1q.value;
   } else if (ig1v.value == 2){
    vf2.value = +vf2.value + +ig1q.value;
   } else if (ig1v.value == 3){
    vf3.value = +vf3.value + +ig1q.value;
   }
});

I have 6 of both of those functions for each IG field. Please forgive the ugly JS, I am sure there is a much better way to write this, but I have no clue how. :D

The problems I am now having is that the Sum fields (vf1-vf3) are adding every change. See how the first function in the script sets Qty to 1 when the corresponding IG field is given a value - this is correctly being added to the vf1 totals field. But, if I change the quantity from 1 to another number, it adds it instead of just changing the value. So, when the Qty field is automatically set to 1, and then you change it to 4, the vf1 totals field would show 5 instead of 4. If you changed the 5 to a 4, it would show 9.

Also, if the IG type is changed, the totals values are not updated for the previously selected value. Say I have IG1 = 1, Qty = 4. Now VF1 (sums of 1) will show 4. But, if I change IG1 = 2 with the same Qty, now VF2 will show 4, but VF1 will also still show 4. I'm not sure how to go about deleting the value from the previous sum field when the IG type is changed, or when the quantity is changed. 

I apologize for this being so confusing, but it is making my head turn into mush and I could use any of the help I can get. 

Really difficult to understand the workflow as is, but, what I understood is you're just having difficulty with the ig1q value because it adds instead of replace. I believe you should reiterate or re-initialize the values AFTER change and not just add to the variables that's already set

Link to comment
Share on other sites

Thanks for the reply, @TellMeWhy.

Maybe this screenshot will help with the understanding. Alum, Vinyl, and Wood are the three possible values for the IG fields. The dropdown is showing the display field, but the value is either 1, 2, or 3. Then the user puts a value of quantity next to it. If IG #1 is set to Wood, and the Qty is 3, then we know we have 3 wood windows. 

igcount.PNG.77f4cb6c25db3dffcdf55bccb34c1999.PNG

I'm trying to determine how many of each type of IG we have. So, if IG1 and IG2 were both 'Alum' with Qty of 6, I would have 12 Alum IGs. 

 

I will give your approach a try. 

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

×
×
  • Create New...