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.13k stars 804 forks source link

dropdown choices based on other answer #1046

Closed masciugo closed 6 years ago

masciugo commented 6 years ago

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

feature/question

What is the current behavior?

choices cannot be based on other answers by the json model

What is the expected behavior?

lively update dropdown choices based on answer of type array. Just wonder if the the correct method is by coding like in this example or there is any plan to make it possible to use something like expression in property like choices so that it can be obtained only by the json model. I think that this use case is particularly delineated to be implemented by the json model

andrewtelnov commented 6 years ago

@masciugo The scenario is valid and look good to me. Unfortunately, the only way to implement it right now, it is to writte some code. as in your example. It is in our TODO list, together with ability to make item visible/invisible and text processing, like: item = {value : 1, text : "You have choosen the car: {car}"}

Thank you, Andrew

andrewtelnov commented 6 years ago

@masciugo The functionality has been implemented in v1.0.24. Here is the demo And here is the updated plunker

Thank you, Andrew

masciugo commented 6 years ago

@andrewtelnov thanks for this enhancement but this solve my needs partially. Let me show my use case. I need to build my array of choices dynamically by adding choices and not by hiding some of them. For example take my own select2 widget (because I was unsatisfied with the official one) which let me exploit the select2 tags option to dynamically create new choices. I am fine with it but as it let me create new choices I cannot then use them in other selectBase question because their are unknown at building model time. In the car context if I create new cars I would like to see them among the preferred ones. In general I wonder if it will ever possibile to build the array of choices using expression in the choices property for example, exaggerating, taking the values of questions of different types (imagine a dynamic matrix to build a dynamic list o friend names and a then a dropdown with that offers them as choices)

just sharing idea! thank you

andrewtelnov commented 6 years ago

@masciugo What is the functionality do you miss in our tagbox and select2 impelementation? Probably we will add it and it make sure that new functionality is working as well.

Thank you, Andrew

masciugo commented 6 years ago

@andrewtelnov before building my own select2 widgets I basically tried to use the hasOther property to enable the tags select2 option without success. I was not able to inhibit/hide the hasOther behaviour for QuestionSelectBase questions. I Think that adding the other option inline is better from a UX point of view. More over it lets you add more then one other option.

However in my situation

so I decided to develop the widgets my way. Have a look to the plunker to see all the cases I needed to cover

Anyway apart from select2 widgets have you got the need for which I opened this issue? Was it clear? let me know. thank you

andrewtelnov commented 6 years ago

@masciugo choicesVisibleIf works for your select2 implementation: https://plnkr.co/edit/Tpj3ahJj6o1vgHFuyKPl?p=preview I have added two new questions into your survey.

Thank you, Andrew

masciugo commented 6 years ago

@andrewtelnov you had already given me the same choicesVisibleIf solution 6 days ago and I thank you. But it does not work if you create a new car in the first question (for example using your tagbox widget or mine)! But thinking to select2 is misleading as the feature I'm talking about does not have anything to do with select2. I just wonder if exist or could be of general interest having a way to map the choices of a question to the choices selected in another one including choices added dynamically by the user. I guess we need a different way to implement this (an expression for choices property?). Something like:

{
    questions: [
        {
            type: "checkbox",
            name: "car",   
            title: "What car are you driving?",
            isRequired: true,
            colCount: 4,
            choices: [     <======= in this question I can add new car models!
                "None",
                "Ford",
                "Vauxhall",
                "Volkswagen",
                "Nissan",
                "Audi",
                "Mercedes-Benz",
                "BMW",
                "Peugeot",
                "Toyota",
                "Citroen"
            ]
        },
        {
          type: "dropdown",
          title: "what car do you prefer among the ones you are driving?",
          name: "prefer",
          choices: "{car}"   <======= here I want to see the new models too
        }
    ]
}

thanks again for your time!

andrewtelnov commented 6 years ago

@masciugo So the idea is to have a master choices (itemvalue array) that can be used and updated on every change in all questions that is used. Am I correct?

Thank you, Andrew

masciugo commented 6 years ago

Yes. There are question types that produce array as value(checkbox, tagbox). Those array can be the choices for other questions. And all should work dynamically.

You can even consider panel and matrix dynamicas source of choices because the values they return are array. As in this case the array is made of objects you can use those objects as the ones you retrive from a rest service when you use choicesByUrl specifying valueName and titleName too.

abdul-qadir commented 6 years ago

@andrewtelnov Suppose "car" is single select dropdown and is part of some other panel dynamic's template element and we added different rows for it. now we want that "prefer"(again single select dropdown in another panel dynamic) should have all selected value of different row in its single select dropdown. how is it possible?

andrewtelnov commented 6 years ago

@abdul-qadir Sorry, I could not understand your scenario. Could you please describe it in more details?

Thank you, Andrew

darthsmada commented 4 years ago

{ "type": "dropdown", "name": "question1", "title": "GID", "choices": [ { "value": "item1", "text": "F80504" }, { "value": "item2", "text": "SCOMPTO" }, { "value": "item3", "text": "Z12345" } ] }, { "type": "dropdown", "name": "question2", "title": "Employee GID", "choices": [ { "value": "item1", "text": "A12345" }, { "value": "item2", "text": "B12345" }, { "value": "item3", "text": "C12345" } ], "choicesVisibleIf": "{question1} contains {item}" } ] }

This gets the question2 values associated with question1 but how do I add more choices

darthsmada commented 4 years ago

Figured it out....Here is how you associate choices in one question based on choices in another question.

{ "type": "dropdown", "name": "ManagerGID", "choices": [ { "value": "item1", "text": "laim"}, { "value": "item2", "text": "baoe"}, { "value": "item3", "text": "lij"}, { "value": "item4", "text": "t74104"}, { "value": "item5", "text": "wangch"}, { "value": "item6", "text": "z41928"} ] },

    {
      "type": "dropdown",
      "name": "EmployeeGID",
      "title": "GID",
      "choices": [
      { "value": "item2", "text": "xur"},
      {"value": "item3", "text": "q73500"},
      { "value": "item3", "text": "p41145"}, 
      { "value": "item2", "text": "j27411"},
      { "value": "item2", "text": "x41239"},
      { "value": "item3", "text": "n10054"},
      { "value": "item3", "text": "h41083"},
      { "value": "item3", "text": "e92313"},
      { "value": "item3", "text": "d65777"},
      {  "value": "item5", "text": "w62868"},
      { "value": "item5", "text": "w47783"},
      { "value": "item5", "text": "u18625"},
      { "value": "item5", "text": "t62294"},
      { "value": "item5", "text": "liud"},
      { "value": "item5", "text": "b37997"},
      { "value": "item5", "text": "b23037"},
      { "value": "item5", "text": "x72889"},
      { "value": "item6", "text": "z63524"},
      { "value": "item6", "text": "z00201"},
      { "value": "item6", "text": "y59417"},
      { "value": "item6", "text": "x08640"},
      { "value": "item6", "text": "u72542"},
      { "value": "item6", "text": "t46383"},
      { "value": "item6", "text": "h71847"},
      { "value": "item6", "text": "e07862"},
      { "value": "item2", "text": "m99743"},
      { "value": "item2", "text": "s73614"},
      { "value": "item1", "text": "s59193"},
      { "value": "item1", "text": "a44493"},
      { "value": "item1", "text": "m41154"},
      { "value": "item4", "text": "n62878"},
      { "value": "item4", "text": "f72163"},
      { "value": "item4", "text": "n18106"},
      { "value": "item4", "text": "y96414"},
      { "value": "item4", "text": "f51397"},
      { "value": "item4", "text": "s75248"},
      { "value": "item4", "text": "f33580"},
      { "value": "item4", "text": "d00307"}
      ],
       "choicesVisibleIf": "{ManagerGID} contains {item}"

    }
masciugo commented 3 years ago

recently implemented. See https://github.com/surveyjs/survey-library/issues/2591

go-sudo commented 2 years ago

Hello @masciugo @darthsmada

how about if we want to show a text, expression or HTML , instead dropbox for selected ManagerGID thank you