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

Asked

Viewed 45 times

1

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

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

Example of Json that creates the questionnaire 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 : Surveyjs editor

1 answer

1


You need to change the default JSON generated by the component. To do this just make a for in on the object changing the key name for 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>
<script src="https://surveyjs.azureedge.net/1.0.18/survey.jquery.min.js"></script>
<div id="surveyContainer"></div>

  • dvd, thanks for the return, I performed some test and this working, but I’m trying to accomplish this when I call the function "Survey.onComplete.add" because there is a visibleif option of the component that uses the tag name , and if I change this tag name before visibleif will not work. If you have any other suggestions, thank you.

  • Okay... I’ll analyze it and I’ll give you an answer

  • Does cloning the JSON and leaving the original intact. How would this function "Survey.onComplete.add"?

  • Survey.onComplete.add(Function (result) { }); it is fired after confirming that the questionnaire is complete then by result.data, collect the result but it does not come as the format I need,I think cloning won’t work... I’m losing my hair from thinking about it so much.

  • Then, the result.data is the return of the query. If you create a variable that stores the result.data, type resultado = result.data; you can make the changes I proposed and use the two things differently.

  • Got DVD, thank you so much for the help!!! With this in mind I will be able to finalize this code , Vlww!!

  • Cool... I hope it works. Be sure to mark the famous to close the issue. If you need just call.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.