Data
RoundtableJS provides a robust system for handling and storing survey data. This includes managing responses, survey metadata, and methods to access and manipulate this data.
Data Structure
The Survey class maintains two main data structures:
responses
: An array that stores all question responses.surveyDetails
: An object that stores metadata about the survey.
Storing Responses
When a user answers a question:
- The response is collected via the
collectPageData
method. - Each response is stored in the
responses
array using theupdateData
method. - A timestamp is added to each response.
Response Format
Each response in the responses
array has the following structure:
Accessing Data
RoundtableJS provides several methods to access stored data:
Retrieves the most recent response for a specific question.
Parameters:
questionId
(string): The ID of the question
Returns:
- The response value for the specified question or null if not found
Checks if a specific question has been answered.
Parameters:
questionId
(string): The ID of the question
Returns:
boolean
: True if the question has been answered, false otherwise
Returns the entire responses
array.
Returns:
array
: An array containing all responses
Returns an object containing all survey details and responses.
Returns:
object
: An object withsurveyDetails
andresponses
properties
Survey Metadata
Survey metadata is stored in the surveyDetails
object. This includes:
startTime
: When the survey beganendTime
: When the survey was completed- Custom metadata passed during survey initialization
You can set and get survey details using:
Sets a custom survey detail.
Parameters:
key
(string): The key for the survey detailvalue
(any): The value to set for the survey detail
Retrieves a custom survey detail.
Parameters:
key
(string): The key of the survey detail to retrieve
Returns:
- The value of the specified survey detail
Completing the Survey
When the survey is finished:
- A completion message is displayed.
- The
endTime
is recorded insurveyDetails
. - The complete survey data is logged to the console.
Completes the survey and displays a finishing message.
Parameters:
options
(object): An object containing the finishing optionsmessage
(string): The message to display upon survey completion
Data Validation
RoundtableJS includes built-in validation for survey responses:
- The
validateCurrentPage
method checks the validity of all elements on the current page. - Each question type has its own
validate
method that can be customized. - Validation errors are displayed to the user, preventing progression until resolved.
Custom Validation
You can implement custom validation for your elements by overriding the validate
method when creating custom question types. This allows you to define specific validation rules for each element type.
Here’s an example of how to implement custom validation for a rating question:
In this example:
- The
validate
method checks if a response has been given (this.data.response !== undefined
) and if it’s within the valid range (>= 1
and<= this.maxRating
). - If
showError
is true and the validation fails, it displays an error message usingthis.showValidationError()
. - The method returns a boolean indicating whether the validation passed or failed.
You can implement similar custom validation for any element type by extending the Element class and overriding the validate
method.
Implementing Complex Validation
For more complex validation scenarios, you can implement additional methods in your custom element class:
This approach allows you to break down complex validation into smaller, more manageable pieces, making your code more readable and maintainable.
Remember to call super.validate(showError)
if you want to retain the base validation logic from the parent class.
By implementing custom validation, you can ensure that the data collected in your survey meets your specific requirements and quality standards.