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

Question

Question

Restrict leave date field in a table to pay period, even after clicking to add a row

asked on June 25, 2019

I have a calendar field for Pay Period Begin Date (.periodBegin) and one for Pay Period End Date (.periodEnd). Employees will report their leave dates (vacation, sick...) in a table (Leave_Days_Table) containing calendar field columns for beginning leave period and ending leave period dates (.dateRange). The following code works in my Leave_Days_Table on the first row, but after clicking on the 'Add a row' link, the subsequent rows still show a calendar with all days enabled. I was hoping that the last few rows on this code snippet would restrict the dates in the next row's calendar fields, but it doesn't. 

$(document).ready(function(){

  function updateMinMaxDates () {
    var minimum = $('.periodBegin input').val(); // get Pay Period Start Date
    var maximum = $('.periodEnd input').val();   // get Pay Period End Date
    var minSplit = [];
    minSplit = minimum.split("/");
    var newMin = (minSplit[2]+"-"+minSplit[0]+"-"+minSplit[1]); 
    var maxSplit = [];
    maxSplit = maximum.split("/");
    var newMax = (maxSplit[2]+"-"+maxSplit[0]+"-"+maxSplit[1]);
    $('.dateRange input').attr('min',newMin); // disable dates before period start date
    $('.dateRange input').attr('max',newMax); // disable dates after period end date
  }
  $('.periodBegin input').change(updateMinMaxDates);
  
    // reset enabled date range when a row is added or removed
  $('.Leave_Days_Table').on('click','.cf-table-add-row',function(){
    updateMinMaxDates();
  });
});

 

LF_Forms_PayPeriodDates.PNG
0 0

Answer

SELECTED ANSWER
replied on June 25, 2019 Show version history

I would double-check that you have the class entered correctly for the table. There doesn't appear to be anything wrong with the code.

However, I would structure it a bit differently. Reused functions can be placed outside of the .ready function, and the delegated handler isn't necessary for the add-row button (it could be extra overhead because it is monitoring the entire element and filtering the events instead of just watching the target).

$(document).ready(function(){
  $('.periodBegin input').change(updateMinMaxDates);
  
    // reset enabled date range when a row is added
  $('.Leave_Days_Table .cf-table-add-row').click(function(){
    updateMinMaxDates();
  });
});

function updateMinMaxDates () {
  var minimum = $('.periodBegin input').val(); // get Pay Period Start Date
  var maximum = $('.periodEnd input').val();   // get Pay Period End Date
  var minSplit = [];
  minSplit = minimum.split("/");
  var newMin = (minSplit[2]+"-"+minSplit[0]+"-"+minSplit[1]); 
  var maxSplit = [];
  maxSplit = maximum.split("/");    
  var newMax = (maxSplit[2]+"-"+maxSplit[0]+"-"+maxSplit[1]);

  $('.dateRange input').attr({min : newMin, max : newMax}); // disable dates before period start date and after period end date
}

 

1 0
replied on June 26, 2019

Thank you very much for the advice. I really appreciate it. I've adjusted my code accordingly.

I'm embarrassed to admit that I was using the table field variable name, Leave_Days_Table, to reference it, instead of a CSS class name. I removed the delegated handler (changed to the following), and everything seems to work.

$('.cf-table-add-row').click(function(){
    updateMinMaxDates();
  });
0 0
replied on June 26, 2019 Show version history

No reason to be embarrassed. I can't tell you how many times I end up banging my head against a wall trying to figure out why something isn't working and it ends up being something just like that lol (the easiest issues to recognize are the ones you've already run into before).

1 0

Replies

replied on June 26, 2019

Just a note...  The same thing happens in a collection. The first date works, if you add another collection, that date doesn't work.

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

Sign in to reply to this post.