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 815 forks source link

Set multiple text items in onLoaded or onCreated #4603

Closed SamMousa closed 2 years ago

SamMousa commented 2 years ago

Are you requesting a feature, reporting a bug or asking a question?

Bug or question

What is the current behavior?

I'm getting an error when trying to set multiple text item in a custom question's onLoaded event.

What is the expected behavior?

I think it should work, but am not sure.

How would you reproduce the current behavior (if this is a bug)?

See test code or plunkr below, if you open developer tools you'll see this stack trace:

Uncaught TypeError: e.setData is not a function
    at survey.core.min.js:6:691711
    at o.push (survey.core.min.js:6:133310)
    at Object.onCreated (<anonymous>:29:36)
    at e.onCreated (survey.core.min.js:24:70917)
    at t [as constructor] (survey.core.min.js:24:73441)
    at new t (survey.core.min.js:24:76986)
    at e.createCustomModel (survey.core.min.js:24:73177)
    at e.createQuestion (survey.core.min.js:24:72939)
    at e.creator (survey.core.min.js:24:70719)
    at e.createClass (survey.core.min.js:6:64111)

Provide the test code and the tested page URL (if applicable)

Tested page URL: https://plnkr.co/edit/Q9MlWJtnrwcWeAJj

Test code

Survey.ComponentCollection.Instance.add({
    name: 'localizableprojecttext',
    title: 'Localizable project text',
    iconName: 'icon-text',
    questionJSON: {
      type: 'multipletext',
      items: [
      ],
    },
    onInit () {
      // This is sample code, I know I could put this static array in the config above, but my goal is to load it dynamically.
      // 
      this.questionJSON.items = [
        {
          name: 'en-US',
          title: 'English',
        },
        {
          name: 'fr-FR',
          title: 'French',
        },
        {
          name: 'ar-AR',
          title: 'Arabic',
        },
      ]
    },
    onCreated (question) {
      question.contentQuestion.items.push({ name: 'nl-NL', title: 'Nederlandisch' })
    },
  })

Specify your

andrewtelnov commented 2 years ago

@SamMousa Here is the working example.

   onCreated(question) {
      // This is sample code, I know I could put this static array in the config above, but my goal is to load it dynamically.
      // 
      const items = [
        {
          name: 'en-US',
          title: 'English',
        },
        {
          name: 'fr-FR',
          title: 'French',
        },
        {
          name: 'ar-AR',
          title: 'Arabic',
        },
      ];
      const multipleQuestion = question.contentQuestion;
      items.forEach(item => {
        multipleQuestion.addItem(item.name, item.title);
      });
    }

Thank you, Andrew SurveyJS Team If you like our products, please take a moment to share your experience with our company by contributing a short review for SurveyJS page at g2.com or at Trustpilot.com. As we’re building our business, every review helps build credibility for future customers and your feedback would be greatly appreciated.

SamMousa commented 2 years ago

Does that mean docs are wrong? image

andrewtelnov commented 2 years ago

@SamMousa The documentation is correct. You can store items in JSON. However, you have to assign the list of MultipleTextItemModel instances. It is similar to survey pages. It can be store in JSON and you can assign the list if pages but you can't assign a list of any object into survey.pages property.

Thank you, Andrew