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

Question

Question

Advancing date when row is added in table

asked on October 5, 2017 • Show version history

Hey Everyone!

I know there are some really smart people out there than can help me with what I am sure is a simple issue.  I have a very basic table, with the first column being a date that is populated by another field.  What I want to do is when the user adds an additional row, I want the date column on this second row, to read the date on the first row and advance by 1.   Gold star up for grabs for the solution!

 

0 0

Answer

SELECTED ANSWER
replied on October 5, 2017

Hey,

This should do what you need.

function tableDates()
{
  var tbl = $('#q4');
  var dateformat = 'DD/MM/YYYY'; //'MM/DD/YYYY';
  var startdate;
  tbl.find('tr td[data-title="Date"] input[type="text"]').each(function() {
    if(startdate)
    {
      $(this).val(startdate.add(1,'d').format(dateformat));
    }
    else
    {
      startdate = moment($(this).val(), dateformat);
    }
  });
  
  
};


$(document).ready(function() {
  $(document).on('click','.cf-table-add-row',function() { tableDates(); });
  $(document).on('click','.cf-table-delete',function() { tableDates(); });
});

A couple of things you will need to change with it.

1)  var tbl = $('#q4'); change '#q4' to the ID of your table.

2) var dateformat = 'DD/MM/YYYY'; change 'DD/MM/YYYY' to the date format you are using for your field

 

That should be it.

If you have any issues let me know.

2 0
replied on October 9, 2017 • Show version history

Good Morning Aaron!  

Thanks so much for the help!  Worked like a charm and you my good sir get the gold star!

 

Thanks, 


Drew

 

0 0

Replies

replied on October 9, 2017

I guess I have another question along these lines, what if I have multiple tables I need to apply this same scripting too?  I thought it would be as simple as copy and paste and adjust the ID but I thought wrong :-(

0 0
replied on October 9, 2017

Hey,

Glad it worked for you, in theory you are correct by changing the targeting "var tbl = $('#q4');" to the id of the new table.

If that doesn't work post a pic of the new table and ill see if I can figure out what's missing.

0 0
replied on October 9, 2017

What's interesting is when I copy and paste and enter the new ID, the new table will work just fine but the original table stops working. 

0 0
replied on October 9, 2017

show me the code u are using after you paste.

0 0
replied on October 9, 2017

This is currently what I have:

  function tableDates()
{
  var tbl = $('#q215');
  var dateformat = 'MM/DD/YY'; //'MM/DD/YY';
  var startdate;
  tbl.find('tr td[data-title="Date"] input[type="text"]').each(function()
                                                               {
    if(startdate)
    {
      $(this).val(startdate.add(1,'d').format(dateformat));
    }
    else
    {
      startdate = moment($(this).val(), dateformat);
    }
  });
  
  
};


$(document).ready(function() {
  $(document).on('click','.cf-table-add-row',function() { tableDates(); });
  $(document).on('click','.cf-table-delete',function() { tableDates(); });
});

  function tableDates()
{
  var tbl = $('#q344');
  var dateformat = 'MM/DD/YY'; //'MM/DD/YY';
  var startdate;
  tbl.find('tr td[data-title="Date"] input[type="text"]').each(function()
                                                               {
    if(startdate)
    {
      $(this).val(startdate.add(1,'d').format(dateformat));
    }
    else
    {
      startdate = moment($(this).val(), dateformat);
    }
  });
  
  
};


$(document).ready(function() {
  $(document).on('click','.cf-table-add-row',function() { tableDates(); });
  $(document).on('click','.cf-table-delete',function() { tableDates(); });
});

 

 

0 0
replied on October 9, 2017

Yeah that will do it. Ill fix up the code give me 5 min and ill post the proper code :)

0 0
replied on October 9, 2017

Here we go. I haven't tested this but it looks right.

 function tableDates(tbl)
{
  var dateformat = 'MM/DD/YY'; //'MM/DD/YY';
  var startdate;
  tbl.find('tr td[data-title="Date"] input[type="text"]').each(function()
                                                               {
    if(startdate)
    {
      $(this).val(startdate.add(1,'d').format(dateformat));
    }
    else
    {
      startdate = moment($(this).val(), dateformat);
    }
  });
  
  
};


$(document).ready(function() {
  $(document).on('click','#q215 .cf-table-add-row',function() { tableDates($('#q215')); });
  $(document).on('click','#q215 .cf-table-delete',function() { tableDates($('#q215')); });
  
  $(document).on('click','#q344 .cf-table-add-row',function() { tableDates($('#q344')); });
  $(document).on('click','#q344 .cf-table-delete',function() { tableDates($('#q344')); });
});

 

0 0
replied on October 9, 2017

Aaron, you are kind of my most favorite person in the world right now.  Works like a charm. Thank you sooooo much for helping me out with this.  Really appreciate it!!! 

0 0
replied on October 9, 2017

No probs mate, glad it worked :)

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

Sign in to reply to this post.