formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

Custom default code is not fired when the control is redrawn i.e. if we set one control to redraw on change of another control , then its custom default value should be fired every time it is redrawn but this is not happening [BUG]

Open pardeep224 opened this issue 7 years ago • 5 comments

Environment

Please provide as many details as you can:

  • Hosting type
    • [ ] Local deployment
      • Version: 4.2.6
  • Formio.js version:4.2.6
  • Frontend framework: angular-formio
  • Browser: Google Chrome
  • Browser version: 77.0.3865.90 (Official Build) (64-bit)

Steps to Reproduce

  1. Take a select component on form as "Country" and bind some values hardcoded from data tab.
  2. Take another Text Field component on form. Set Redraw On as Country. Add some code in "Custom Default Value" section.
  3. Save the form.
  4. Run the from and change value from country component.

Expected behavior: Code written in "Custom Default Value" section should be run on every selection change of Country component.

Observed behavior: Code written in "Custom Default Value" section is not being run on every selection change of Country component.

Example

NA

pardeep224 avatar Oct 03 '19 14:10 pardeep224

The Custom Default value does not re-evaluate when the component is "redrawn" and this is by design. You may be asking for "Calculated Value" with the "Allow Override" checked. That probably will give you what you want out of this.

travist avatar Oct 03 '19 14:10 travist

We want to call a API with value of the select component as a query string parameter. If we do that in calculated Value the API will keep on calling multiple times. Is there a way to call the API only when component is redrawn.

sanjaykrgaur avatar Oct 14 '19 05:10 sanjaykrgaur

The custom default value is only called when setting the default value (i.e. at component initialization). If you want to run custom code when a component is redrawn, you will probably need to listen to the "render" event on the form that fires every time a component is rendered. There is also an "attach" event when the rendered dom element has its events attached and might be more in line with what you are looking for.

randallknutson avatar Oct 14 '19 14:10 randallknutson

Hello , i am also looking for this kind of functionality but not able to perform, I tried with "render" event but not able to listen the event , while i am changing the value from dropdown render method is not calling , please provide any suggestion how we can achieve this Thanks

ThakkarHardik avatar Oct 06 '20 10:10 ThakkarHardik

Rendering or redrawing does not indicate that the data should be defaulted again. One thing you could do is add a listener to the instance within the default value and do whatever it is you want to do.

For example:

instance.on('change', function(event) {
  // do something based on a change.
})
}

You can add this within the custom default value since it will only run once when the component is initialized.

randallknutson avatar Oct 06 '20 14:10 randallknutson