surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.21k stars 814 forks source link

Dynamic Panel - Introduce an option to copy specific field values to newly added panels #7867

Closed JaneSjs closed 9 months ago

JaneSjs commented 9 months ago

User Inquiry: T16854 - I would need defaultPanelValueExpression for paneldynamic https://surveyjs.answerdesk.io/internal/ticket/details/T16854

A Dynamic Panel has the defaultValueFromLastPanel option which copies all panel values to a newly added panel.

However, it may be required to copy only specific field values. In the meantime, it is only possible to enable this from code but not from within the SureyJS Creator UI. Introduce an option/setting which would allow users to specify which fields to copy.

andrewtelnov commented 9 months ago

I would not add this functinality into the library right now. A developer can write a generic code to implement this functionality. 1) Add a new property Survey.Serializer.addProperty("paneldynamic", {name: "copiedNamesFromLastPanel", category: "data"}); 2) Use this property to copy values:

    survey.onDynamicPanelAdded.add(function(sender, options) {
        const q = options.question;
        const index = options.panelIndex;
        if(!q.copiedNamesFromLastPanel || index < 1) return;
        const names = q.copiedNamesFromLastPanel.split(";");

        const prevPanel = q.panels[index - 1];
        names.forEach(name => {
            const prevQ = prevPanel.getQuestionByName(name);
            const curQ = options.panel.getQuestionByName(name);
            if(prevQ && curQ && !prevQ.isEmpty()) {
                curQ.value = prevQ.value;
            }
        });
    });

The example of using:

    {
      "type": "paneldynamic",
      "name": "vacation-info",
      "copiedNamesFromLastPanel": "country;end-date"
}

Here is the working example.

Thank you, Andrew