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

Question

Question

Adding a time stamp for field entry in Forms table

asked on April 6, 2017

My customer has a table in Forms in which you can add rows.  There is currently only one column but they would like to add a second column that captures the date/time of entry change for each field in the corresponding row.  I found this answer that looks like it would work for field outside of a table (thank you Xiuhong!)  https://answers.laserfiche.com/questions/67971/Inserting-local-machine-Time-into-Forms-Field#68207 I need to modify this code to be applicable to a table.  Is this possible?  

 

THANKS!!!

0 0

Answer

SELECTED ANSWER
replied on April 12, 2017

I believe this should do the trick.

 

Tato

var column1 = 'Field12';
var column2 = 'Field13';

$('form.cf-form').on('click','[id^='+column1+']', function() {
  var chng = $(this).attr('id');
  var chngindex = chng.substr(chng.search(/\(/),chng.search(/\)/));
  var cid = "[name=" + '"' + column2 +chngindex+'"]';
  var currentdate=new Date();
  var datetimenow=pad((currentdate.getMonth()+1),2)+ "/"+pad(currentdate.getDate(),2)
    + "/" + currentdate.getFullYear() + " " 
    + pad(currentdate.getHours(),2) + ":" 
    + pad(currentdate.getMinutes(),2) + ":" + pad(currentdate.getSeconds(),2);
  $(cid).val(datetimenow)
});

function pad(str, max) {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}

 

1 0

Replies

replied on April 7, 2017

This may help

var column1 = 'Field12';
var column2 = 'Field13';

$('input[id^='+column1+']').on('change', function(){
  var chng = $(this).attr('id');
  var chngindex = chng.substr(chng.search(/\(/),chng.search(/\)/));
  var cid = "[name=" + '"' + column2 +chngindex+'"]';
  var currentdate=new Date();
  var datetimenow=(currentdate.getMonth()+1)+ "/"+currentdate.getDate()
    + "/" + currentdate.getFullYear() + " " 
    + currentdate.getHours() + ":" 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds();
  $(cid).val(datetimenow)
});

"column1" one would be the one where the change happens for which the time needs to be recorded on "column2". I just modified the code you referenced before.

 

Tato

1 0
replied on April 7, 2017

please see this. it shows it working. I have added the modified (added the document ready section)

$(document).ready(function(){
var column1 = 'Field12';
var column2 = 'Field13';

$('input[id^='+column1+']').on('focus', function(){
  var chng = $(this).attr('id');
  var chngindex = chng.substr(chng.search(/\(/),chng.search(/\)/));
  var cid = "[name=" + '"' + column2 +chngindex+'"]';
  var currentdate=new Date();
  var datetimenow=(currentdate.getMonth()+1)+ "/"+currentdate.getDate()
    + "/" + currentdate.getFullYear() + " " 
    + currentdate.getHours() + ":" 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds();
  $(cid).val(datetimenow)
});

});

 code that works on my form.

Tato

1 0
replied on April 7, 2017

Hello Gerardo- Thank you so much for helping!

I input your code and I cannot get it to trigger.  Am I implementing it correctly?  Thank you again for your help!

$(document).ready(function(){
var column1 = 'Field1';
var column2 = 'Field2';

$('input[id^='+column1+']').on('change', function(){
  alert'triggered';
  var chng = $(this).attr('id');
  var chngindex = chng.substr(chng.search(/\(/),chng.search(/\)/));
  var cid = "[name=" + '"' + column2 +chngindex+'"]';
  var currentdate=new Date();
  var datetimenow=(currentdate.getMonth()+1)+ "/"+currentdate.getDate()
    + "/" + currentdate.getFullYear() + " " 
    + currentdate.getHours() + ":" 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds();
  $(cid).val(datetimenow)
});
});

0 0
replied on April 7, 2017 Show version history

The code is meant to trigger on change so a value must change on the first column to change the value on the second. If you want the value to change on entry, just change the 'change' on your code to 'focus'

0 0
replied on April 7, 2017

After looking at your change to the code, it looks like the problem is on your alert statement. it appears to be missing the parenthesis:

your code:   alert'triggered';

should be:   alert('triggered');

0 0
replied on April 7, 2017

Thanks Gerardo!  I removed the alert that I was using to troubleshoot and input your code again and it worked but it only worked for the first row- Is yours working for subsequent rows?

0 0
replied on April 9, 2017

Yes, it is supposed to read the index based on the row that you are selecting. you could put an alert like this and it would show what row it is going to affect:

 

right under the line that starts: var chngindex =

type in a new line alert(chngindex);

 

All this script is doing is identifying the row value of the modified cell in the specified column and modifying the same row value cell on the desired column. the code you showed originally extracted the proper format for the time and that is the value entered there.

0 0
replied on April 10, 2017 Show version history

Fixed the code. Had to do delegated events so that new rows added would calculate as well. here is the changed code. Sorry for any confusion.

$(document).ready(function () {
var column1 = 'Field12';
var column2 = 'Field13';
$('form.cf-form').on('change','[id^='+column1+']', function() { //delegated event handler for specific column
  var chng = $(this).attr('id'); // identify specific field modified with index
  var chngindex = chng.substr(chng.search(/\(/),chng.search(/\)/)); //extract index
  var cid = "[name=" + '"' + column2 +chngindex+'"]'; // build field id for column to be changed with specified index
// These lines will calculate specific date format for now time
  var currentdate=new Date();
  var datetimenow=(currentdate.getMonth()+1)+ "/"+currentdate.getDate()
    + "/" + currentdate.getFullYear() + " " 
    + currentdate.getHours() + ":" 
    + currentdate.getMinutes() + ":" + currentdate.getSeconds();
  $(cid).val(datetimenow); // change value
});
});

 

1 0
replied on April 10, 2017

Thank you so much for taking time to help!!!! The code works on all rows now.

 

Thanks again,

Terri

 

0 0
replied on April 12, 2017

Hello again Geraldo :)

Is there an easy way to mask the date time to add the leading or trailing 0's so the result is in true date time format MM/dd/yyyy hh:mm:ss   I am getting 4/12/2017 8:3:13.  I appreciate all of your help!

 

0 0
replied on April 12, 2017

It did- thank you so much!!!  I went ahead and changed the latest code to the answer.  

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

Sign in to reply to this post.