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

Question

Question

Setting Minimum Date when in a Collection

asked on December 10, 2020 Show version history

Having an issue with setting a minimum date on the date picker when the field is in a collection.

I am using the code below to set the minimum date to the current date.  Works great on the first entry in the collection but does not work on any entries added to the collection.

Would anyone be able to tell me how to modify the code so it works for each entry in the collection?

 

Thanks.

 

 

 

$(function() {
  $(document).ready(function () {
    
   var todaysDate = new Date(); // Gets today's date
    
    // Min date attribute is in "YYYY-MM-DD".  Need to format today's date accordingly
    
    var year = todaysDate.getFullYear(); 						// YYYY
    var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);	// MM
    var day = ("0" + todaysDate.getDate()).slice(-2);			// DD

   	var minDate = (year +"-"+ month +"-"+ day); // Results in "YYYY-MM-DD" for today's date 
    
    // Now to set the min date value for the calendar to be today's date
    $('.datePickerMinCurrent input').attr('min',minDate);

  });
});

 

0 0

Replies

replied on December 10, 2020

The only way to get that to work in a dynamic collection would be to re-run it every time the user adds a new row/item to the collection since those items/rows didn't exist when the code first ran.

Basically,

  1. Make a separate function like "setMinimumDate" so you can call it more than once
  2. Call setMinimumDate() on document ready to get the first row/item set
  3. Set up a click event handler for the "add" link, which also calls setMinimumDate()

 

With event handlers you can use bubbling to monitor a collection/table so it can work dynamically, but for updating HTML elements like this the only way is to modify them as they're added.

2 0
replied on December 11, 2020
//this should be your own collection class that you set 

$(".Collection tr").each(function(){      

var todaysDate = new Date(); // Gets today's date

// Min date attribute is in "YYYY-MM-DD".  Need to format today's date accordingly

 var year = todaysDate.getFullYear();                        // YYYY

 var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);  // MM

var day = ("0" + todaysDate.getDate()).slice(-2);           // DD

 var minDate = (year +"-"+ month +"-"+ day); // Results in "YYYY-MM-DD" for today's date 

 // Now to set the min date value for the calendar to be today's date

$(this).find('.datePickerMinCurrent input').attr('min',minDate);

}); 

*****MOST Important : $(this).find('.datePickerMinCurrent input').attr('min',minDate);

$(this) <- will be each row of your collection and you will look for datePickerMinCurrent field within each row.

I would say that if you are populating row from look up ... add this in your onloadlookupfinsihed event and lookupcompleted event. 

.. 

if you prepopulate this from workflow or previous business process activity you might have to consider when these collection rows are being populated

 

if you are manually adding these collection rows then i would consider adding this each function on the trigger for add collection event 

 

 

****Please expose me if im wrong in anything i say:). I can't walk around this earth with the wrong coffeeScroll Knowledge :) 

0 0
replied on December 11, 2020 Show version history

.each is not necessary and actually adds inefficiency. With the .each it would be creating a new date object and everything for each individual row.

The code she already has will work fine because JQuery selectors retrieve all matches meaning it updates the minDate attribute for everything it finds in that one line.

1 0
replied on December 14, 2020 Show version history

oh i see.. thats good to know. As long as my select is for the particular column class name then i should be able to add attr to all of the rows that are either generated by look up or prepopulated? 

 

0 0
replied on December 16, 2020

Yes, jQuery selectors will retrieve all matches which is helpful when you need to update multiple things at the same time.

Sometimes you need .each for more complex actions, but not for stuff like this.

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

Sign in to reply to this post.