Question T15866
Visible to All Users

Is it possible to select choices received from the restful web service and display only the selected choices to the end user?

created a year ago

Functionality required

  1. Survey creater to view choices received from web service.
  2. Survey creater should be able to select choices received from the web service.
  3. Only the selected choices by the survey creater to be visible for the end user to select.

Answers approved by surveyjs Support

created a year ago

Hello Ishantha,
In a Survey Creator, users can populate a collection of choices which will be available for respondents to select from.

From what I gather, you wish to allow survey editors to fetch a source list of choices from a specified endpoint and populate the final list of choices by copying particular choices from the source list. If so, I believe that you may wish to register a custom 'source choice list' property: Add Custom Properties to the Property Grid.

For instance, set this property's type to multiplevalues, and populate the choices array by implementing the corresponding choices function. To copy selected choices to the target choices property, implement the onSetValue function. Consider this sample: View CodeSandbox.

JavaScript
import { Serializer } from "survey-core"; Serializer.addProperty("dropdown", { name: "sourceChoices", category: "choices", type: "multiplevalues", index: 0, choices: function (obj, choicesCallback) { const xhr = new XMLHttpRequest(); xhr.open("GET", "https://surveyjs.io/api/CountriesExample"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onload = function () { if (xhr.status === 200) { const response = JSON.parse(xhr.response); const result = []; result.push({ value: null }); response.forEach((item) => { result.push({ value: item.cioc, text: item.name }); }); choicesCallback(result); } }; xhr.send(); }, onSetValue: function (surveyElement, value) { surveyElement.setPropertyValue("sourceChoices", value); surveyElement.setPropertyValue("choices", value); }, });

Clipboard-File-1.png
Users will be able to select multiple choices from a source list, and you'll be able to copy this choices to the question's choices property. As a result, respondents will be able to select an option from a preconfigured list of options.

I hope this example can help you.

Please let me know if you have any further questions.