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

Question

Question

Auto Insert Rows in a Table

asked on August 9 • Show version history

In Laserfiche 12 modern Forms, is there a way to automatically insert a row based off a single field number?  So, Row Count field has a number of 5. Then i would like to have a table automatically have 5 rows in a table. If the number changes to 3 than i would to have that same table readjust to only 3 rows and so on.

Thanks in Advance!!!

0 0

Replies

replied on August 9

Yes.  Use the JS below and replace yourfieldID with the Field ID for the single line field you are using to determine the number of rows.  Replace yourtablevariablename with the variable of your table.  

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: yourfieldID});
    // 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: 'Yourtablevariablename' }, fieldValues);
      lastFieldValue = fieldValues; // Update the last field value
    }
  },
  { fieldId: yourfieldID }
);

 

3 0
replied on August 9

Thank youuuuuuu sooooo muchhhhhh Angelaaaaa!!!! It works like a CHAMP!!!!

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

Sign in to reply to this post.