I'd like to be able to output the survey results into HTML and not stringified JSON. Ideally so I can display it in a nice way on the page (like a summary of the results) or even use it to generate charts/graphs. I've tried a few things to grab the result.data, but each time it doesn't output anything unless I wrap survey.data in JSON.stringify().
Example of what I'd like to see instead of the JSON.stringified result data:
HTML<h1>Results:</h1>
<p><strong>Question 1:</strong></p>
<p>Value of question 1</p>
Basically I'm not going to store results into a database and want to display it in the DOM in some way. I even tried to use Handlebars/Mustache to parse the JSON and display in HTML, but I can't get it to work.
Any direction you could provide would be appreciated.
Hello,
You would have to write some JavaScript code. It depends on what JavaScript library you are using.
Additionally, probably survey.getPlainData() will suite you better.
Thank you,
Andrew
SurveyJS Team
Not using any JS library at the moment. Was wondering if there was a built-in way of doing it. How would I use survey.getPlainData()?
You may just try out survey.getPlainData() - it returns the array of items.
As for the code, it should be as easy as:
for(var key in survey.data) { var q = survey.getQuestionByValueName(key); if(!q) continue; //here write q.title //here write q.value or q.displayValue }
Thank you,
Andrew
SurveyJS Team
I guess this is where I'm confused. I tried using survey.getPlainData(), yet it doesn't work for me (it writes nothing in the DOM).
survey .onComplete .add(function (result) { document .querySelector('#surveyResult') .innerHTML = "result: " + survey.getPlainData(result.data); });
displayingTried your code. When I tested dispalying a value on a series of radiobutton questions with 'yes' or 'no' values, it only displays one value despite having multiple survey questions (looks like it is only displaying the value from the last question). How can I get this to show data for all my survey questions?
Javascript:
for(var key in result.data) { var q = result.getQuestionByValueName(key); if(!q) continue; //here write q.title //here write q.value or q.displayValue document .querySelector('#content-placeholder') .innerHTML = q.displayValue; }
HTML output on completion:
<div id="content">Yes</div>
Hello,
You should call
survey.getPlainData();
without parameters.Regarding the second issue, you keep overriding your '#content-placeholder' html element.
You should create them dynamically. You may google your task and read articles like this one.
PS: Your current task is a typical JavaScript developer task that doesn't related to SurveyJS.
Thank you,
Andrew
SurveyJS Team
Thanks Andrew. I was trying to figure out how to show all values from every question. The example code provided seems to only pull values from the last question. Any thoughts on how to resolve that?
I do not give the exact examples, since I have no idea how you are going to add your html elements.
You are re-writing your html element content in every "for" cycle.
To be honest, it is very basic JavaScript task and as I said, it is not related to our library.
Thank you,
Andrew
SurveyJS Team
Can you provide me an example of how to use survey.getPlainData()? I've tried using this on both my survey and some examples provided on your site and I have not been successful.
I took an example survey and tried using getPlainData(), but it didn't work. Thoughts? https://next.plnkr.co/edit/NVfuycEvlCc64Mgp
Hello,
You did not stringify the JSON object to show it as a text. Here is the forked version.
Thank you,
Andrew
SurveyJS Team
Thank you Andrew. I guess I have a different understanding of the word "plain" and text. What I'm seeing is still being output as JSON. I'd like to just get regular, plain text. It seems there's nothing built-in to SurveyJS to do this?
survey.getPlainData() - returns results for matrix dynamic, panel dynamic and multiple text in flat (non tree) form. It includes question.title (column/item title) and display value as well.
You may always convert json into text by using JSON.stringify(myJson) function.
Thank you,
Andrew
SurveyJS Team