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

Question

Question

automatically add a specific number of rows to a table based on a number field in New Forms

asked on July 8

I have a field that calculates the number of rows needed in a table, but I can't figure out how to programmatically specify that number of rows in the table in New forms.

 

Use case:

Company picnic.  How many people are coming with you to the picnic? (3)

I want those three person's names in a table with their relationship and if a child their age.  I have the table, but I want it to automatically have 3 required rows in the table and no more, no less.

0 0

Replies

replied on July 8 Show version history

I'm doing something similar using the below Javascript:

// Allocate Blocked Tickets Section Populate Table Rows
const updateTable1Rows = async (tableFieldId, rowCount) => {
  const currentTable1FieldValues = LFForm.getFieldValues(tableFieldId);
  const curRowCount1 = currentTable1FieldValues.length;

  if (curRowCount1 < rowCount) {
    await LFForm.addRow(tableFieldId, rowCount - curRowCount1);
  } else if (curRowCount1 > rowCount) {
    await LFForm.deleteRow(
      tableFieldId,
      ...Array.from(Array(curRowCount1 - rowCount)).map(
        (_, i) => curRowCount1 - i - 1
      )
    );
  }
};

let lastFieldValue1 = null;

LFForm.onFieldChange(
  async () => {
    const fieldValues1 = LFForm.getFieldValues({ fieldId: 56 });

    // Check if the field value has changed since the last time a row was added
    if (fieldValues1[0] !== lastFieldValue1) {
      // Update the table rows with the new row count
      await updateTable1Rows({ variableName: 'FT_Allocate_Passengers' }, fieldValues1);
      lastFieldValue1 = fieldValues1; // Update the last field value
    }
  },
  { fieldId: 56 }
);

I bolded the variables/id's you'll have to update - Hope this helps!

 

Editing to add:

This CSS will hide the add/remove row buttons:

.table-row-add, .table-row-remove {display: none;}
0 0
replied on July 8

Thank you.  I'll give that a shot.

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

Sign in to reply to this post.