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

Question

Question

JQuery Help- JQuery calculations not updating changes to prior rows in table

asked on March 14, 2018

I am working on jquery code for a table in Forms that looks at each row to make sure   a number (amount requested ) is not greater than another number (budgeted amount- this field is populated by a lookup and could be different on every row.) If the amount requested  is over budget I hide the submit button and the ability to create new rows. The code works great on the initial row and any additional rows added,  but the problem occurs whenever I go back to a prior row and make a change- the code (although it triggers) does not seem to be working with the line selected. Any help would be much appreciated!  THANKS in advance!!!

 

$(document).ready(function(){
  $('.table').on('change onloadlookupfinished', check);
							});
function check(){
  $('.table tbody tr').each(function(){
    if (1*$(this).find('.requestedamount input').val() > 1*$(this).find('.budgetamount input').val()){
        $(".action-btn.Submit").hide();
        $(".cf-table-add-row").hide();
                                     }
				else {
        $(".action-btn.Submit").show();                
        $(".cf-table-add-row").show();
                    }   
                                           });
                                            };

 

0 0

Answer

SELECTED ANSWER
replied on March 16, 2018

You can try it like this:

$(document).ready(function(){
  $('.table').on('change onloadlookupfinished', check);
});
function check(){
  var hideBtn = false;
  $('.table tbody tr').each(function(){
    if (1*$(this).find('.requestedamount input').val() > 1*$(this).find('.budgetamount input').val()){
        hideBtn = true;
    } 
  });
  if (hideBtn) {
    $(".action-btn.Submit").hide();
    $(".cf-table-add-row").hide();
  } 
  else {
    $(".action-btn.Submit").show();                
    $(".cf-table-add-row").show();
  }
};

 

1 0

Replies

replied on March 14, 2018

Hi Terri,

In the check function, if the first row does not meet the request it would hide the buttons, but then it would process the second row which meet the request and show the buttons up.

You can use additional variable for keeping row status and show/hide buttons at once after processing all rows; or stop processing other rows when any of the row does not meet the condition.

0 0
replied on March 15, 2018

Thank you very much Rui- Do you have an example of code where the coder keeps the row status by chance?  I think that is the best alternative.

0 0
SELECTED ANSWER
replied on March 16, 2018

You can try it like this:

$(document).ready(function(){
  $('.table').on('change onloadlookupfinished', check);
});
function check(){
  var hideBtn = false;
  $('.table tbody tr').each(function(){
    if (1*$(this).find('.requestedamount input').val() > 1*$(this).find('.budgetamount input').val()){
        hideBtn = true;
    } 
  });
  if (hideBtn) {
    $(".action-btn.Submit").hide();
    $(".cf-table-add-row").hide();
  } 
  else {
    $(".action-btn.Submit").show();                
    $(".cf-table-add-row").show();
  }
};

 

1 0
replied on March 16, 2018

Thank you so much Rui!!!!!  If you have time can you tell me the logic behind the difference so I'll know for next time.  I can't really tell where the logic difference lies except that it looks like you are relying on the status of the submit visibility.

0 0
replied on March 18, 2018

The variable hideBtn does not get the status of the submit visibility. When check() function is triggered, variable hideBtn is set to false by default which means it should show the buttons.

Then inside $('.table tbody tr').each() function, it will check each row in the table and when a row meets the condition, it sets hideBtn to true, which means it should hide the buttons. There is no logic to set it back to false inside the each function, so it would not flip the status back, and this is the part that is different from your original code.

1 0
replied on March 20, 2018

Thank you Rui- That makes sense.  I appreciate the information!!!

 

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

Sign in to reply to this post.