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.22k stars 816 forks source link

Dropdown with Lazy Loading appears empty when reopened #9013

Closed JaneSjs closed 2 weeks ago

JaneSjs commented 2 weeks ago

T20594 - Specify as many forms as i need whit its corresponding form question name https://surveyjs.answerdesk.io/internal/ticket/details/T20594


Please consider the following demo: View CodeSandbox. To reproduce the issue, select dropdown 1, dropdown 2. Re-open dropdown 2: a list appears empty even though the options.setItems(selectedItem.values, selectedItem.values.length) function seems to be invoked with a correct item value array.

image

JaneSjs commented 2 weeks ago

I got a reply from @OlgaLarina. The survey.onChoicesLazyLoad event's options.setItems callback function should either accept an array of strings or of the { value: XXX, text: YYY } objects. Therefore, it is necessary to update the code as follows: View Demo.

survey.onChoicesLazyLoad.add((_, options) => {
    if (options.question.name === "dropdown1") {
      const dropdownItems = dropdown1Values.map((item) => {
        return {
          value: item.id,
          text: item.title,
          values: item.values,
        };
      });
      options.setItems(dropdownItems, dropdownItems.length);
    }
    if (options.question.name === "dropdown2") {
      const dropdown1 = options.question.parent.getQuestionByName("dropdown1");
      const selectedItem = dropdown1.selectedItem;
      if (!!selectedItem) {
        const dropdown2Choices = dropdown1.selectedItem.values.map((item) => {
          return { value: item.value, text: item.text };
        });
        options.setItems(dropdown2Choices, dropdown2Choices.length);
      }
    }
});