Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load values from data on element change #405

Closed
craftzneko opened this issue May 8, 2024 · 1 comment
Closed

Load values from data on element change #405

craftzneko opened this issue May 8, 2024 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@craftzneko
Copy link

craftzneko commented May 8, 2024

Prehaps this is not the best approach, but hopefully if not someone can suggest a better one. My form is simple in that it just creates a JSON object and Adds and deletes "components" to the JSON. I am trying to add a modify option. When modify is selected and the component ID from the JSON object is chosen, i need to load the data into each form element. I thought i could do this through on form change using this

const componentElement = context.panel.elements.find((element) => element.id === "action");
// Handle the 'Modify' action
if (componentElement.value === 'Modify') {
  // Check if window.formData exists and is an object
  if (typeof window.formData === 'object' && window.formData !== null) {
    // Check if there is a 'Components' array in the object
    if (Array.isArray(window.formData.Components)) {
      // Update the form elements with the corresponding values from window.formData
      onChange(context.panel.elements.map((element) => {
        // Find the component with the same ID as the element (without the "installer" prefix)
        const component = window.formData.Components.find(component => 'installer' + component.ID === element.id);
        // If the component was found, update the element value with the component value
        if (component) {
          return { ...element, value: component.value };
        } else {
          // If the component was not found, return the element unchanged
          return element;
        }
      }));
    }
  }
}

I would expect description to be filled in

image

Json object created

image

I get the error onchange is not defined

@vitPinchuk vitPinchuk self-assigned this May 13, 2024
@vitPinchuk
Copy link
Contributor

Hi, @craftzneko
use context.panel.onChangeElements instead onChange in 'Element Value Changed' code editor

@mikhail-vl mikhail-vl added the question Further information is requested label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

3 participants