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

Question

Question

When adding a row to a table, copy values from the previous row in the same table

asked on July 8 Show version history

I'm using the Modern Forms Designer in Laserfiche Forms Professional Version 11.0.2311.50564.

I have a table where the first two columns are Employee Name and Employee ID. When the user enters the values in the first row, and then clicks on the "Add" button to add another row, I want the newly added row to have the same Employee Name and Employee ID. The user is still able to change those values, if necessary. This is just to make is easy for them to add multiple rows for an employee, but be able to add rows for multiple employees.

I have a hidden field (fieldId 16) where I store the number of rows currently in the table. The calculation for that field is below:

=COUNTIF(Table.Employee_Name, "<>") + COUNTIF(Table.Employee_Name, "=")

In the JavaScript code, employee name from the previous row is logged in the console, but it seems that the function runs twice every time a row is added. The screenshot below shows the console when I run it with the 2 setFieldValues() lines (line 14 and 15) commented out. 

In the JavaScript code below, the setFieldValues() lines (14 and 15) cause an infinite loop.

const rowCountFieldId = { fieldId: 16 };

const handleRowChange = async () => {
    const getRowCount = LFForm.getFieldValues(rowCountFieldId); 
    const newRowIndex = getRowCount - 1; //indexes start with 0
    const priorRowIndex = newRowIndex - 1;

    // copy employee name and employee ID from previous row into new row
    let pEmpName = LFForm.getFieldValues({fieldId: 6, index: priorRowIndex});
    let pEmpID = LFForm.getFieldValues({fieldId: 7, index: priorRowIndex});
    console.log("Employee Name = " + pEmpName + 
                ", newRowIndex = " + newRowIndex +
                ", priorRowIndex = " + priorRowIndex);
    await LFForm.setFieldValues({fieldId: 6, index: newRowIndex}, pEmpName);
    await LFForm.setFieldValues({fieldId: 7, index: newRowIndex}, pEmpID);
}
LFForm.onFieldChange(handleRowChange, rowCountFieldId);
0 0

Answer

SELECTED ANSWER
replied on July 11 Show version history

I'm not sure if this will help you or not. 

If I'm trying to keep track of the number of rows in a table/collection I add a hidden number field in the table/collection with a default value of 1. (I tend to name this field Row_Exists)

 

Then in a number field outside of the table I will use a simple sum function on the "Row_Exists" field. something like this: 

=SUM(The_Table.Row_Exists)

 

const rowCountID = 58;

let rowCount = LFForm.getFieldValues({fieldId:rowCountID});


LFForm.onFieldChange(async function(){   
  console.log("A row was added or deleted " ); 

  currRowCount = LFForm.getFieldValues({fieldId:rowCountID});
 
  if(rowCount>currRowCount)
  {
    console.log('Deleted row');
   
  }else{
    console.log('Added row');
  };
  
  rowCount = currRowCount

}, {fieldId: rowCountID});

 

I would think for you when a row is added you would then just need to update the last row field values. So you would need to use the field Id and the index would be one less than the currRowCount (or rowCount after it is updated), because tables/collections are zero based (first row's index is 0). 

 

EDIT:  Saw I left out "currRowCount = LFForm.getFieldValues({fieldId:rowCountID});" in the on change event. So of course I added in as it should have been. :) 

1 0

Replies

replied on July 9

I think your issue lies in the fact that previousRowCount is stored on form load so it isn't dynamic when new rows are added. Instead I would move the variable into the onchange handler and subtract 1.

const rowCountFieldId = { fieldId: 16 };
const handleRowChange = async () => {
    const newRowCount = LFForm.getFieldValues(rowCountFieldId);
    // NEW JS added
    previousRowCount= newRowCount  > 0 ? newRowCount -1 ? newRowCount;
    // if a new row was added
    if (newRowCount > previousRowCount) {
      // copy employee name and employee ID from previous row
      let pEmpName = LFForm.getFieldValues({fieldId: 6, index: previousRowCount});
      let pEmpID = LFForm.getFieldValues({fieldId: 7, index: previousRowCount});
      console.log("Employee Name" + pEmpName);
      await LFForm.setFieldValues({fieldId: 6, index: newRowCount}, pEmpName);
      await LFForm.setFieldValues({fieldId: 7, index: newRowCount}, pEmpID);
    }
    previousRowCount = newRowCount;
}
LFForm.onFieldChange(handleRowChange, rowCountFieldId);

 

0 0
replied on July 10

That JS also results in an infinite loop. I think it's caused by the COUNTIF calculation in my row count field (fieldId: 16), because if there's no calculation and I just change the number manually, handleRowChange does not go into an infinite loop. Sorry, I didn't include that in my original post. And, sorry for changing the code in my original post.

Is there another way to keep a count of the rows in a table or a way to listen for an 'add row' event?

0 0
replied on July 10

Even if I change 

LFForm.onFieldChange(handleRowChange, rowCountFieldId);

to

LFForm.onFieldChange(handleRowChange, {fieldId: 6});

it still goes into an infinite loop after I add a row to the table.

0 0
replied on July 11 Show version history

Edit: nevermind

SELECTED ANSWER
replied on July 11 Show version history

I'm not sure if this will help you or not. 

If I'm trying to keep track of the number of rows in a table/collection I add a hidden number field in the table/collection with a default value of 1. (I tend to name this field Row_Exists)

 

Then in a number field outside of the table I will use a simple sum function on the "Row_Exists" field. something like this: 

=SUM(The_Table.Row_Exists)

 

const rowCountID = 58;

let rowCount = LFForm.getFieldValues({fieldId:rowCountID});


LFForm.onFieldChange(async function(){   
  console.log("A row was added or deleted " ); 

  currRowCount = LFForm.getFieldValues({fieldId:rowCountID});
 
  if(rowCount>currRowCount)
  {
    console.log('Deleted row');
   
  }else{
    console.log('Added row');
  };
  
  rowCount = currRowCount

}, {fieldId: rowCountID});

 

I would think for you when a row is added you would then just need to update the last row field values. So you would need to use the field Id and the index would be one less than the currRowCount (or rowCount after it is updated), because tables/collections are zero based (first row's index is 0). 

 

EDIT:  Saw I left out "currRowCount = LFForm.getFieldValues({fieldId:rowCountID});" in the on change event. So of course I added in as it should have been. :) 

1 0
replied one day ago

All I had to do was add the hidden table field, "Row_Exists", default it to 1, and change my hidden row counter field to =SUM(Table.Row_Exists). Now, I no longer have an infinite loop and my code (referenced below) works!

const rowCountFieldId = { fieldId: 16 };
const handleRowChange = async () => {
    const getRowCount = LFForm.getFieldValues(rowCountFieldId); 
    const newRowIndex = getRowCount - 1; //indexes start with 0
    const priorRowIndex = newRowIndex - 1;

    // copy employee name and employee ID from previous row into new row
    let pEmpName = LFForm.getFieldValues({fieldId: 6, index: priorRowIndex});
    let pEmpID = LFForm.getFieldValues({fieldId: 7, index: priorRowIndex});
    await LFForm.setFieldValues({fieldId: 6, index: newRowIndex}, pEmpName);
    await LFForm.setFieldValues({fieldId: 7, index: newRowIndex}, pEmpID);
}
LFForm.onFieldChange(handleRowChange, rowCountFieldId);

Thank you so much!

2 0
replied 20 hours ago

I am glad you were able to make it work for you :) 

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

Sign in to reply to this post.