Find().Each() calculation in table cell

Viewed 39

Not really sure what I'm missing. I need to run some calculations in a table cell. I have several tables on the page. Each table has its own ID. So I only need to run the calculation for one specific table at the time (per the ID).

let calculateDiscountFromEffective = function(id){
            console.log(' BUTTON PRESSED ' + id);
    
            $(".tableTab2").find("tr").each(function(){
                var tableID = $(this).closest('tr').attr('id');
                
                 if(tableID == id) {
                     let targetDisc = calculateRequiredDiscountRate($(this));
                   $(this).closest("tr").find(".discountRatePercent").val((targetDisc * 100.0).toFixed(2));
                     calculateDiscount($(this));                    
                 }
            });
        }

        let calculateRequiredDiscountRate = function($tr){
            let daysOutstanding = $tr.find(".daysOutstanding").asNumber();
            let maturityBuffer = $tr.find(".maturityBuffer").asNumber();
            let effectiveRate = $("input[id$='txtEffectiveRate']").asNumber();
            let targetDisc = (effectiveRate * daysOutstanding / 100.0)/((daysOutstanding + maturityBuffer) * (1.0 - effectiveRate * daysOutstanding / 360.0 / 100.0));
            
            return targetDisc;
        }

Problem: The first "IF", where I compare tableID and id, returns only the first row and stops. When I remove the "IF", it will be processing the calculation for all rows in all tables on the page. How do I make it work for a specific table?

Here is a portion of my table:

<table class="table tableTab2" id="{!oppWrapper.oppSchoolName}">
                    <thead>
                        <tr id="{!oppWrapper.oppSchoolName}">
                            <th class="col1">{!$Label.FE_P2_Receivable_Name}</th>
                            <th class="col2">{!$Label.FE_P2_Purchase_Date}</th>

Here is my button:

<button class="btn btn-tertiary" id="{!oppWrapper.oppSchoolName}" onClick="calculateDiscountFromEffective(id);return false;">Calc. rates</button>

Thanks in advance

1 Answers

The id has to added into the as well as the . With that said here is the modified table:

<table class="table tableTab2" id="{!oppWrapper.oppSchoolName}">
   <thead>
       <tr id="{!oppWrapper.oppSchoolName}">
          <th class="col1">{!$Label.FE_P2_Receivable_Name}</th>
          <th class="col2">{!$Label.FE_P2_Purchase_Date}</th>
          ...                  
       </tr>
   </thead>
   <tbody>
       <tr id="{!oppWrapper.oppSchoolName}">
          <td style="line-height: 30px !important"> ...</td>
          <td style="line-height: 30px !important"> ...</td>
          ...
Related