Surveyjs get the title tag instead of the name in the result

1

I'm using the Surveyjs editor library, however in the editor the result is being saved using name as a reference to put in the result.

Example: Survey Result: {"question1":"item1","question2":"item2"}.

Example of Json creating the quiz in the editor:

{
 pages: [
  {
   name: "page1",
   elements: [
    {
     type: "radiogroup",
     name: "question1",
     title: "Sobre sua vida",
     choices: [
      "item1",
      "item2",
      "item3"
     ]
    },
    {
     type: "radiogroup",
     name: "question2",
     title: "Sobre seus carros",
     choices: [
      "item1",
      "item2",
      "item3"
     ]
    }
   ]
  }
 ]
}

Result I'm trying to get:

Survey Result: {"Sobre sua vida":"item1","Sobre seus carros":"item2"}.

This is the component link: Editor surveyjs

    
asked by anonymous 19.04.2018 / 19:56

1 answer

1

You need to change the default JSON generated by the component. To do this, simply make a for in on the object by changing the name key to title :

Survey.Survey.cssType = "bootstrap";

var surveyJSON = {"pages":[{"name":"page1","elements":[{"type":"radiogroup","name":"question2","title":"pergunta 1","choices":["item1","item2","item3"]},{"type":"radiogroup","name":"question1","title":"pergunta 2","choices":["item1","item2","item3"]}]}]}

// aqui eu altero o JSON
var objJson = surveyJSON.pages[0].elements;
for(var item in objJson){
   objJson[item].name = objJson[item].title;
}

function sendDataToServer(survey) {
    //send Ajax request to your web server.
    alert("The results are:" + JSON.stringify(survey.data));
}

var survey = new Survey.Model(surveyJSON);
$("#surveyContainer").Survey({
    model: survey,
    onComplete: sendDataToServer
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://surveyjs.azureedge.net/1.0.18/survey.jquery.min.js"></script>
<div id="surveyContainer"></div>
    
19.04.2018 / 21:11