formio / formio.js

JavaScript powered Forms with JSON Form Builder
https://formio.github.io/formio.js
MIT License
1.88k stars 1.06k forks source link

How to update edit form in a builder? #3625

Closed Samanoova closed 8 months ago

Samanoova commented 3 years ago

hi I overwrite the components dialog with this function

let dialog={...};
for (var key in Formio.Components.components) {
        Formio.Components.components[key].editForm = function () {
          return dialog;
        };
      }

then I have a dropdown menu & add button in the Display tab Capture now when pressing the add button I edited the dialog and add a value to the drop-down menu so I want to update it and show the new values that I added to the dropdown menu so I tried reinitiating the builder but I have to close the dialog and open it again to see my new value in the drop-down then I tried to call the redraw function but don't work and redraw function initiate the following error "builder.redraw is not a function"

how can I reload the drop-down menu to show the new values?

Samanoova commented 3 years ago
builder.instance.editComponent_Original = this.builder.instance.editComponent;
  builder.instance.editComponent = function (
        component,
        parent,
        isNew,
        isJsonEdit,
        original
      ) {
        .builder.instance.editComponent_Original(
          component,
          parent,
          isNew,
          isJsonEdit,
          original
        );
        //then store the params component , parent ,isNew,isJsonEdit,original to use them later 
      };
    },

now you have the parameters you can call the method that create the dialog you can close the dialog and call the method builder.instance.editComponen(the parameters you stored) then the dialog will show again and it will be updated

Samanoova commented 3 years ago

but I hope if there any way to update the dropdown menu only without the need to rerender the dialog @travist

travist commented 3 years ago

The editForm is a form instance that can be found within the form builder and can be redraw as follows.

builder.editForm.redraw();

This should redraw the form within the modal and not actually refresh the modal (which should appear seamless).

Samanoova commented 3 years ago

if anybody watching this issue and builder.editForm.redraw(); don't work and builder.editForm return undefined then you can find the editForm property in builder.instance so you have to edit the components in the editForm property then call builder.instance.redraw(); that's worked for me

gladkiy commented 3 years ago

@travist @Samanoova how can I find "builder" instance. What should I import? I'm using this https://help.form.io/developers/custom-components into "editForm" method I do ajax query to get custom options from server but can't update them on modal dialog.

Samanoova commented 3 years ago

@gladkiy did you overwrite the editform function? It should return json object that's include your options and You can overwrite it for each component separately

gladkiy commented 3 years ago

@Samanoova I do something like that:

 static editForm() {

    let components = editForm().components;

    let tabs = Utils.getComponent(components, "tabs", true);

    let dataTab = Utils.getComponent(components, "data", true);

    dataTab.components = [];

    $.ajax({
        url: "...",
        type: "GET",
        success: function (response) {
          for(let masterType of response.data) {
             dataTab.components.push({
                type: 'checkbox',
                label: masterType.name,
                key: masterType.name.replace(" ", "").toLowerCase(),
             });
          }
          console.log(2);
          return {
            components: [tabs]
          };
        }
      });

    console.log(1);
    return {
      components: [tabs]
    };
  }

I see that console.logs are executed but no changes on UI (modal dialog). Is there error? Or I need to update/refresh modal dialog or builder somehow?

Samanoova commented 3 years ago

@gladkiy do you execute this function before the dialog shows up or after?

gladkiy commented 3 years ago

@Samanoova I'm not sure when, how can I investigate that?

Samanoova commented 3 years ago

@gladkiy are you on discord?

gladkiy commented 3 years ago

@Samanoova I'm going through this tutorial https://help.form.io/developers/custom-components

export class MyComponent extends Field { 
   ...
   static editForm() {
      //you can see body of this method into comment above
   }
}
gladkiy commented 3 years ago

@Samanoova I think console.log(1) is executed before modal dialog is showing and console.log(2) after modal dialog is showing.

Samanoova commented 3 years ago

i am using different way than i can't be sure if you are doing it right !

gladkiy commented 3 years ago

@Samanoova Can you share your way, please? Or maybe a link, will be appreciate any help.

Samanoova commented 3 years ago

@gladkiy if you have an account on discord i can call you to see your screen and try to help you

gladkiy commented 3 years ago

@Samanoova I attached my file, change extension to ".ts" container-chooser.txt

Samanoova commented 3 years ago

here is an example import TextFieldEditForm from "your path to script file/TextFieldEditForm.ts"; Formio.Components.components["textfield"].editForm = function () { return TextFieldEditForm; }; Formio.builder(document.getElementById("formio"), {}, {}); TextFieldEditForm.zip then you can edit the textfieldeditform file or add on it

gladkiy commented 3 years ago

@Samanoova I need to get options from server, I can't simply edit TextFieldEditForm file. And I have error Property 'builder' does not exist on type 'typeof Formio'.

Samanoova commented 3 years ago

import { Formio } from "formiojs";

gladkiy commented 3 years ago

@Samanoova sure, I have that import but still see this error. I'm using formiojs v4.13.5 if it helps

Samanoova commented 3 years ago

are you sure that you are using these {} ? this import will work : import { Formio } from "formiojs";

this import will not work: import Formio from "formiojs";

gladkiy commented 3 years ago

Yes, I have

 import { Utils, Formio } from "formiojs";
Samanoova commented 3 years ago

is your file .ts or .js ?

gladkiy commented 3 years ago

My file is ".ts"

Samanoova commented 3 years ago

convert it to .js

gladkiy commented 3 years ago

issue was disappeared, thanks. But can you send me full file how can I get options from server side and put them into modal dialog please?

Samanoova commented 3 years ago

can you explain what are you trying to do?

gladkiy commented 3 years ago

Did you download my file container-chooser.txt above?

Samanoova commented 3 years ago

yes editForm should be = function(){return your object}

gladkiy commented 3 years ago

@Samanoova can you give me an example?

export class MyComponent extends Field {
//editForm should be here?
} 

//or editForm should be here?

or maybe you can edit my file and send me again with working editForm?

Samanoova commented 3 years ago

I have to know what your API returning

gladkiy commented 3 years ago

@Samanoova my API just returning array of objects: [{id: 1, name: "John"},{id:2, name: "Peter"}]

Samanoova commented 3 years ago

your API should return an object like mine I uploaded it TextFieldEditForm.zip above

gladkiy commented 3 years ago

@Samanoova can you write it down? and I will see full picture of custom component with API query

Samanoova commented 3 years ago

https://github.com/formio/formio.js/files/7031832/TextFieldEditForm.zip

gladkiy commented 3 years ago

@Samanoova so my API query should return similar content as your file?

Samanoova commented 3 years ago

yes

gladkiy commented 3 years ago

@Samanoova so this code should work?

  editForm = function () { 
    $.ajax({
      url: location.origin + "/api/shared/editForm",
      type: "GET",
      success: function (response) {
        return response;
      }
    });
}

"response" = "{components: [{ type: '1', key: '1', components: [...] }]}"

Samanoova commented 3 years ago

yes i think it's should work

gladkiy commented 3 years ago

no, it doesn't work, and if I put console.log into editForm method I don't see output of that console log

Samanoova commented 3 years ago

i don't have any idea there a lot of things may cause the problem

gladkiy commented 3 years ago

thank you for your help @Samanoova @travist may be you know how to refresh editForm (modal dialog) after ajax query?

JRiggenbach commented 2 years ago

@gladkiy Did you ever get an answer to this? We're using the angular-formio lib so its not quite the same implementation, but we're trying to figure out how to redraw the edit form after its already been loaded.

gladkiy commented 2 years ago

@JRiggenbach I'm also using angular-formio but for custom components it doesn't match for me. For custom components I do ajax queries into constructor or attach method. I think if you need that into built-in components then you need to overwrite typescript class for that component and also do ajax query into constructor or attach method.

hem1232 commented 1 year ago

here is an example import TextFieldEditForm from "your path to script file/TextFieldEditForm.ts"; Formio.Components.components["textfield"].editForm = function () { return TextFieldEditForm; }; Formio.builder(document.getElementById("formio"), {}, {}); TextFieldEditForm.zip then you can edit the textfieldeditform file or add on it

Hello, From where u got this file content. I want this for all form fields.

OleksandrPoshtaruk1533 commented 1 year ago

here is an example import TextFieldEditForm from "your path to script file/TextFieldEditForm.ts"; Formio.Components.components["textfield"].editForm = function () { return TextFieldEditForm; }; Formio.builder(document.getElementById("formio"), {}, {}); TextFieldEditForm.zip then you can edit the textfieldeditform file or add on it

Hello, From where u got this file content. I want this for all form fields.

Check this repo https://github.com/formio/formio.js/tree/master/src/components Each component has EditForm folder

Tatsiana8 commented 8 months ago

Closing this thread as it is outdated. Please re-open if it is still relevant. Thank you for your contribution!