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

Question

Question

Reset a Currency Field with JS on Laserfiche 12

asked on July 28 • Show version history

Hi, I have a table collection. I want to reset the currency field to blank or 0.00 when a radio button is clicked. So, i'm thinking an onChange action can be used. I hope.

Steps:

1. Click on Personal and add amount

2. Click on Center and add amount. And the same time i want the amount       that is set on the Personal to reset to empty or 0

3. Basically every time i click on a radio button i want to reset/clear any             amount for that record only.

Rest Amount.png
Rest Amount 2.png
Rest Amount 3.png
Rest Amount 4.png
0 0

Replies

replied on July 28

yes It works nicely. Great Job Mr. Luis!!! Thanks a Million!!!!

2 0
replied on July 28 • Show version history

Hi Carlos, the example below was done in the Modern Designer. You don't speciffy witch LF Form you're using, so I will asumming is Modern Design.

In this example:

  • Paid by radio button: Field ID "3" (match with your field IDs)
  • Amount currency field: Field ID "4" (match with your field IDs)

 

In the JavaScript, change these IDs to yours:

- const PAID_BY_ID = 3; (replace with your paid ID radio button)
- const AMOUNT_ID = 4; (replace with your amount ID field)

The following JavaScript detects which row’s Paid by selection changed and clears the Amount field only in that same row:
 

const PAID_BY_ID = 3;
const AMOUNT_ID = 4;

// Always process the field values as an array of rows.
function asArray(value) {
  return Array.isArray(value) ? value : [value];
}

// Return the selected value from a radio button.
function getRadioValue(item) {
  if (item && typeof item === "object") {
    return item.value;
  }

  return item;
}

// Store the initial Paid by value for each row.
let previousPaidBy = asArray(
  LFForm.getFieldValues({ fieldId: PAID_BY_ID })
).map(getRadioValue);

// Run whenever a Paid by radio button changes.
LFForm.onFieldChange(async function () {
  const currentPaidBy = asArray(
    LFForm.getFieldValues({ fieldId: PAID_BY_ID })
  ).map(getRadioValue);

  for (let index = 0; index < currentPaidBy.length; index++) {
    // Clear Amount only in the row where Paid by changed.
    if (currentPaidBy[index] !== previousPaidBy[index]) {
      await LFForm.setFieldValues(
        {
          fieldId: AMOUNT_ID,
          index: index
        },
        null
      );
    }
  }

  // Save the current selections for the next change.
  previousPaidBy = [...currentPaidBy];

}, { fieldId: PAID_BY_ID });

How to implement it

1 - In your form, go to the JavaScript tab.
2 - Paste the code into the JavaScript editor.
3. Verify that the field IDs match your form:

   - "Paid by" should be Field ID '3'. Put your IDs
   - "Amount" should be Field ID '4'. Put your IDs
4. Save the form and open its preview.
5. Add an amount, then change Paid by from Personal to Center, or from Center to Personal.
6. The amount should immediately be cleared only in the affected row. Amounts entered in the other rows will remain unchanged.

The index property is important because it identifies the specific row in the table. Without the index, the script could clear the Amount field in every row.

If 'null' does not clear the currency field in a particular Forms build, replace it with an empty string:

I tested this approach in Laserfiche 11 and 12 Modern Designer, and it successfully resets the currency field when the radio button changes while leaving the other rows unchanged.

Let me know if it works for you.

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

Sign in to reply to this post.