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

Question

Question

Laserfiche Modern Forms - Append table rows form date range selection

asked on February 21

I am looking for help with some JavaScript in the Forms Modern Designer. It is supposed to append table rows form a date range selection. I am new to JavaScript but I don't see a mention to the date range selection in the JavaScript provided. 

 

JavaScript:

const updateTableRows = async (tableFieldId, rowCount) => {
  const currentTableFieldValues = LFForm.getFieldValues(tableFieldId);
  const curRowCount = currentTableFieldValues.length;
	if (curRowCount < rowCount) {
    await LFForm.addRow(tableFieldId, rowCount - curRowCount);
  } else if (curRowCount > rowCount) {
    await LFForm.deleteRow(
      tableFieldId,
      ...Array.from(Array(curRowCount - rowCount)).map(
        (_, i) => curRowCount - i - 1
      )
    );
  }
};
	let lastFieldValue = null;
	LFForm.onFieldChange(
  async () => {
    const fieldValues = LFForm.getFieldValues({ fieldId: 31});
	// Check if the field value has changed since the last time a row was added
    if (fieldValues !== lastFieldValue) {
      // Update the table rows with the new row count
      await updateTableRows({ variableName: 'Date_Time' }, fieldValues);
      lastFieldValue = fieldValues; // Update the last field value
    }
  },
  { fieldId: 31}
);

 

Screenshot:

 

 

Form:

0 0

Answer

SELECTED ANSWER
replied on February 21

The JS doesn't reference the date range from the date fields, it references the number of days from the date range.  Here are the instructions on setting this up:

Add two date fields to be the start and end dates needed for the table.  
Add a single line field to calculate the number of days from Start to End.
Use the formula =DATEDIF(Start_Date, End_Date, "D") + 1.  *Replace "Start_Date" and "End_Date" with your date variables.

Add the field ID that contains the calculation number to the Javascript and replace "yourTableVariable" with the variable name of your table.
Add formula =Start_Date+(ROW()-1) to the date field within your table. *Replace "Start_Date" with your date variables.
*Add and change the start and end data fields to see the table append based on the number of days needed.

Happy to see you are playing with the codes from the Advent calendar.  If you have questions, feel free to email me angela@schooloffiche.com

0 0
replied on February 24

Hello Angela,

Thank you for your help, I really like learning. I have tried to implement your solution, but I must be missing something or doing something wrong, because it isn't working. Here are some screenshots of how I implemented your instructions. 

 

Start Date: Added formula

 

End Date: Done nothing.

 

Days between Start And End Date: Added formula

0 0
replied on February 24

The Start Date, End Date and Additional Single Line (Days Between Start and End Date) field should all be outside of the table.  These fields are only there to calculate the number of rows you need for your table.  

You only need to add the calculation to your Single Line field (Days Between Start and End Date).  This gives you the number of days between start and end.  This field will then use JS to give you the number of rows in your table.  The second formula is for the date field within a table.  This is a different date field that you might use for a time card or expense report to provide specific dates to the end user.  

There is a video on here: How to Automatically Populate Table Rows Based on a Date Range

1 0
replied on February 26

That information was great, I do have it working on its own form. I think some of the other JS from the advent calendar was interacting with it. Thank you! 

0 0
replied on February 26

Awesome!  Yes, you may have to make adjustments to the JS if you are putting several together in one form.  I was trying to keep it as generic as possible for simplicity.  Glad you got it working and feel free to reach out if you run into other issues.

1 0
replied on April 7

Thank You @████████, I used this solution today for a travel form.

0 0

Replies

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

Sign in to reply to this post.