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

Question

Question

Date Calculations in Forms Table

asked on January 4, 2016 Show version history

Hi,

I have a policy application that has three different date fields and a number (Active #) field in a table. The date fields are "From" (start of policy), "To" (end of policy) and "Add/Delete" (date of any change made to the policy) dates. The number field by default calculates the difference between the "To" and "From" dates. The "Add/Delete" field initially defaults to the "To" date but is editable and as such when there is a change in the policy, this date is edited and as such the number field calculates the difference between the "From" and "Add/Delete". 

To = Add/Del

Active # = Add/Del - From

I managed to do this with the below code. 

 

  //Date Calculation

   $('.cf-table-block').on('change', 'input', sumtotal);

 $('.dateClass').on('change', 'input', sumtotal);

function sumtotal() {

  $('.cf-table-block tbody tr').each(function () {   

    var t1 = 0;

    var t2 = 0;

    var diff = 0;

    var expiry = 0;

  t1 = new Date($(this).find('.startDate input').val());

     //Copying Date

              $('.endDate input').change(function () {

              expiry = new Date($(this).val().toString());

              $('.addDelClass input').val(expiry);

              });

  t2 = new Date($('.addDelClass input').val());

  day1 = (Math.floor((t2-t1)/(1000*60*60*24))) + 1;

  $(this).find('.dateDiff input').val(diff);

  });

}

 

The challenge am getting is that when I add another row, the "Add/Delete" date on all the rows change to the value of "To" date of the second row when only the one on the second row should be affected. What am I not doing right to ensure that the "To" value affects only the :Add/Del" value on it's row. 

Secondly I would appreciate advise on how, when I add the second row, these fields are copied onto the second row then I just change where necessary.

Doc1.docx (47.74 KB)
1 0

Replies

replied on January 4, 2016

Hi Mark,

You can insert the values of the last row into a newly created row by having an on-click event trigger when the "add" button is clicked. For instance: 

$(document).on('click', '.cf-table-add-row', function(){
    
  $('.startDate input').last().val($('.startDate input:eq(-2)').val());
  $('.endDate input').last().val($('.endDate input:eq(-2)').val());
});

Let me know if this works for you!

3 0
replied on May 31, 2023 Show version history

This is miraculous!  Thank you.  I have been trying to make the date picker restriction for the first row, apply to additional rows (when clicking "Add New Row"), with no luck.  Other people have been able to make it work, but I have been challenged!

This worked for me and accomplished what I wanted to accomplish!

I have been working on getting the other way to work for a few hours.  This worked for me the first try.

Thank you!

Christine

Note:  Full Disclosure.......drawback.....Date Picker is no longer limited.  The applicant would have the ability to choose dates prior to July 1st (which I do not want them to be able to do).

Any advice would be great.

0 0
replied on January 4, 2016

Hi Alex,

Thanks a lot for the support. It worked perfectly.

Mark

2 0
replied on January 4, 2016

Hi Alex

It worked well apart from the part of the difference whereby all the rows were getting the difference for the first row only which I rectified by changing

t2 = new Date($('.addDelClass input').val());

to

t2 = new Date($(this).find('.addDelClass input').val());

Otherwise I would still appreciate advise on my second question. I wouldn't want to keep on choosing the dates on each row because most of the time they are the same. I would like new row to copy the values of the previous row and the user to change where necessary. Thanks a lot.

Regards,

Mark

1 0
replied on January 4, 2016 Show version history

Hi Mark,

In the third line after //Copying Date, you use $('.addDelClass input').val(expiry); which will assign the "expiry" date to every ".addDelClass input". 

I would try replacing this line with $(this).closest('tr').find('td.addDelClass input').val(expiry); so that you are targeting one row at a time.

Let me know if this works for you!

You are not allowed to follow up in this post.

Sign in to reply to this post.