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.09k stars 795 forks source link

Show and set other value at dropdown #1677

Closed michaelchandrag closed 5 years ago

michaelchandrag commented 5 years ago

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

Asking a question

What is the current behavior?

I have a survey with a dropdown question and it has attribute hasOther: true. On the other hand, i also have a data which i would set it to the answer of the survey. The data could be like this

survey.data = {
    car: 'My Other Car'
};

The question more like this

var json = {
    questions: [
      {
            type: "dropdown",
            name: "car",
            title: "What car are you driving?",
            isRequired: true,
            hasOther: true,
            colCount: 0,
            choices: [
                "None",
                "Ford",
                "Vauxhall",
                "Volkswagen",
                "Nissan",
                "Audi",
                "Mercedes-Benz",
                "BMW",
                "Peugeot",
                "Toyota",
                "Citroen"
            ]
        }
    ]
};

The current dropdown question doesnt have 'My Other Car' options. Can the surveyjs achieve this? By automatically, showing other values and the answer at the 'other' input text if there are no matching option on dropdown with the value data

What is the expected behavior?

Showing 'other' values at the dropdown and the'other' input text is by default showing the data value

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

Give 2 question 1 for dropdown 1 for answer of the other

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

Tested page URL: https://plnkr.co/edit/YMKDnpcndF0pg5K4PmoM?p=preview Test code

your_code_here

Specify your

tsv2013 commented 5 years ago

You need to use the following code:

survey.data = {
    "car": "other",
    "car-Comment": 'My Other Car'
};

Here is your updated plunker - https://plnkr.co/edit/frYgVmDXwWMFSy91OUy0?p=preview

michaelchandrag commented 5 years ago

hey thanks for the answer @tsv2013 it worked perfectly additional question not related to the title thread can i change the style of requiredText (*) to color red? i found this thread, but it is using another libraries https://github.com/surveyjs/survey-library/issues/539

can i achieve that without using third-party library?

michaelchandrag commented 5 years ago

nvm i solve my requiredText problem this is how i do it

model.onTextMarkdown.add(function (survey, options) {
      if (options.element.isRequired === true) {
        options.html = options.text
      }
    })

dont forget to add requiredText add your json survey

requiredText: '<span style="color:red;">*</span>',