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

Question

Question

trigger code to restrict date for second row in table

asked on January 22, 2020

I have a table. The applicant enters a start date (From:). I have restricted the end date (To:) show only dates after the start date.

$(document).on ('.myTable change', function getdate () {
   $('.firstdate input').on('change', function() { 
    var datearray = $('.firstdate input').val();
    var minDate = datearray
    $('.seconddate input').attr('min',minDate); 
  });
});

This works well for the first row. I cannot get it to work for the second row, despite having combed over multiple entries in Laserfiche Answers. I guess that will come with experience.

In the snip above, the dates are restricted, but they are relevant to the entry in the first row, not the second row. How can I adapt my code to reference the first cell in each additional row?

 

0 0

Answer

SELECTED ANSWER
replied on January 22, 2020 Show version history

The issue you're seeing is that every time your code runs it is referencing the same "firstDate" match because your code doesn't "target" a specific row or relative object.

If you want it to work the way you describe, then it needs to have a more dynamic way of finding the "associated" field(s) rather than just the first match.

To do this, you could either extract the index of the "current" field that triggered the change, or use parent functions to find the "relative" field, the latter being the preferable approach.

Second, the way you have the code written, it looks like it is going to reassign those handlers every time the table changes, which I don't think is at all desirable. Ideally, you should only need to assign the handlers once when the form first loads (document ready event).

For example,

$(document).ready(function(){
  // detect any change to the table, filtered by the firstDate field
  $('.myTable table').on('change','.firstDate input',function(){
    // get the start date
    var minDate = $(this).val();
    
    // set the min end date on the "secondDate" field within the same row as the one that changed
    $(this).parents('tr').find('.secondDate input').attr('min',minDate);
  });
});

DISCLAIMER: This code will only work if you keep the date format as you have it (yyyy-MM-dd). If you want to use a different format, you would need to extract the value and convert it to yyyy-MM-dd before you assign it to the minDate attribute otherwise it will cause errors.

Fortunately, Forms imports the moment.js library, which makes it easier to format date objects.

For example, if you wanted to display 01/31/2020 to the user instead of 2020-01-30, you could use moment.js to convert that date into a valid format by changing line 5

// get the start date and format it for min date attribute
var minDate = moment($(this).val(),'MM/DD/YYYY').format('YYYY-MM-DD');

First you have moment($(this).val(),'MM/DD/YYYY')

Which is getting the value of the trigger field and saying it is in 01/31/2020 format.

Next, .format('YYYY-MM-DD') is converting 01/31/2020 to 2020-01-31, which is what the min date attribute requires.

All you need to do is match the "input" format to whatever you display on the field and you won't the user view to match exactly what is expected by the attribute.

https://momentjs.com/docs/#/displaying/

0 0

Replies

replied on January 22, 2020

Thanks for the coding tips. Most of the time, I am just winging it. I seem to be improving on reading scripts, but when it comes to writing, I have a hard time wrapping my head around it.

I used the code you provided, it worked great. I did have to change the variables to match mine, but that I can manage.

You have helped me more than once over the last few days. I really appreciate your help.

0 0
replied on January 22, 2020

No problem, it all comes with time and practice. That's why I try to include detailed comments and the extra explanation rather than just dropping in sample code with no frame of reference or context smiley

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

Sign in to reply to this post.