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
collectPageDatamethod. - Each response is stored in the
responsesarray using theupdateDatamethod. - A timestamp is added to each response.
Response Format
Each response in theresponses 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
- 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
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 withsurveyDetailsandresponsesproperties
Survey Metadata
Survey metadata is stored in thesurveyDetails object. This includes:
startTime: When the survey beganendTime: When the survey was completed- Custom metadata passed during survey initialization
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
- The value of the specified survey detail
Completing the Survey
When the survey is finished:- A completion message is displayed.
- The
endTimeis 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
validateCurrentPagemethod checks the validity of all elements on the current page. - Each question type has its own
validatemethod 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 thevalidate 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:
- The
validatemethod checks if a response has been given (this.data.response !== undefined) and if it’s within the valid range (>= 1and<= this.maxRating). - If
showErroris true and the validation fails, it displays an error message usingthis.showValidationError(). - The method returns a boolean indicating whether the validation passed or failed.
validate method.
Implementing Complex Validation
For more complex validation scenarios, you can implement additional methods in your custom element class: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.
