You are viewing limited content. For full access, please sign in.

Question

Question

sum/count based on variable in table

asked on January 5, 2018

I have the following table...

 

Outside of the table I have fields that total the number of Council Meetings, etc., within the event column.

I have used JavaScript to count the number of "Event Types" for each selection listed.  I have just found out that I need to make the increment value equivalent to the Per Diem (within the same row) selected by the user.  The values are either 0.5 or 1.

Can I increment based on a variable?  If so, how do I reference it within the table?

Here is the code I used to count...

function countCheck(column,valueToCount)
{
  var count = 0;
  $('#q20 tr td[data-title="'+column+'"] input:checked').each(function()
 {
   if($(this).val() == valueToCount)
   {
    count += 1; 
   }
   
 });
  return count;
}

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
     $('.TotCouncil input[type="text"]').val(countCheck('Event Type','Council'))
     $('.TotOther input[type="text"]').val(countCheck('Event Type','Other'))
     $('.TotComm input[type="text"]').val(countCheck('Event Type','Committee'))
   });
  
});

I am not skilled at coding.  This code was a result of the generosity of those on Laserfiche Answers.  I appreciate the help!

smiley

 

 

0 0

Answer

SELECTED ANSWER
replied on January 15, 2018 Show version history

Hi Mary, I believe the error is related to the way we are accessing the Per Diem value. The script was looking at the text of the selection, meaning that it would only work if the selections themselves were the numeric increments (e.g., 0.5 and 1). However, it occurred to me that your form probably has the numbers stored in the drop down's values instead. If this is the case, the script below should work for you.

function countCheck(column,valueToCount)
{
  var count = 0;
  $('#q20 tr td[data-title="'+column+'"] input:checked').each(function()
 {
   if($(this).val() == valueToCount)
   {
     //obtain the value of the dropdown selection
     var name =  $(this).closest('tr').find('.PerDiem :selected').val();
   
     //convert the text into an integer
     var increment = parseFloat(name);
     
     //increment the counter
     count += increment; 
   }
   
 });
  return count;
}

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
    $('.TotCouncil input[type="text"]').val(countCheck('Event Type','Council'))
     $('.TotOther input[type="text"]').val(countCheck('Event Type','Other'))
     $('.TotComm input[type="text"]').val(countCheck('Event Type','Committee'))
   });
  
});

Let me know if you encounter any other issues getting the script to work!

0 0

Replies

replied on January 5, 2018

Hi Mary,

In order to increment the values based on the Per Diem value, you can use a modified version of the script that you are currently using. The example below should achieve the the result you are looking for, and I've included comments to provide a bit of context for the lines I added. To adapt this code for your process, add a PerDiem CSS class to that column of the table, and make sure that the element IDs match those on your form (e.g., #q20 below references the table).

function countCheck(column,valueToCount)
{
  var count = 0;
  $('#q20 tr td[data-title="'+column+'"] input:checked').each(function()
 {
   if($(this).val() == valueToCount)
   {
     //obtain the text of the dropdown selection
     var name =  $(this).closest('tr').find('.PerDiem :selected').text();
   
     //convert the text into an integer
     var increment = parseFloat(name);
     
     //increment the counter
     count += increment; 
   }
   
 });
  return count;
}

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
    $('.TotCouncil input[type="text"]').val(countCheck('Event Type','Council'))
     $('.TotOther input[type="text"]').val(countCheck('Event Type','Other'))
     $('.TotComm input[type="text"]').val(countCheck('Event Type','Committee'))
   });
  
});

Please let me know if you have any further questions or if you have any issues with the script!

0 0
replied on January 8, 2018

When I test this in preview mode, the total fields populate with "NaN"  I have no idea what that's all about.  

I played with the form by changing the numbers in the Per Diem.  It will sometimes show a total, and sometimes it shows the result above.  I can't seem to see a pattern.

0 0
SELECTED ANSWER
replied on January 15, 2018 Show version history

Hi Mary, I believe the error is related to the way we are accessing the Per Diem value. The script was looking at the text of the selection, meaning that it would only work if the selections themselves were the numeric increments (e.g., 0.5 and 1). However, it occurred to me that your form probably has the numbers stored in the drop down's values instead. If this is the case, the script below should work for you.

function countCheck(column,valueToCount)
{
  var count = 0;
  $('#q20 tr td[data-title="'+column+'"] input:checked').each(function()
 {
   if($(this).val() == valueToCount)
   {
     //obtain the value of the dropdown selection
     var name =  $(this).closest('tr').find('.PerDiem :selected').val();
   
     //convert the text into an integer
     var increment = parseFloat(name);
     
     //increment the counter
     count += increment; 
   }
   
 });
  return count;
}

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
    $('.TotCouncil input[type="text"]').val(countCheck('Event Type','Council'))
     $('.TotOther input[type="text"]').val(countCheck('Event Type','Other'))
     $('.TotComm input[type="text"]').val(countCheck('Event Type','Committee'))
   });
  
});

Let me know if you encounter any other issues getting the script to work!

0 0
replied on February 1, 2018

This corrected the issue. 

Thanks.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.