Skip to content

Table Of Contents - Custom TOC Item API Enhancement #8798

@JaneSjs

Description

@JaneSjs

T26775 - Multi page form show the count of questions on each page title
https://surveyjs.answerdesk.io/internal/ticket/details/T26775
T23589 - Table of Contents - Display the number of total and answered questions
https://surveyjs.answerdesk.io/internal/ticket/details/T23589
T14475 - Is it possible to show the progress bar based on the pages?
https://surveyjs.answerdesk.io/internal/ticket/details/T14475


The current API: View Demo.

class CustomTOCItem extends ReactSurveyElement {
    constructor(props) {
        super(props);
    }
    render() {
        const survey = this.props.model.locOwner;
        const pageName = this.props.item.id;
        const page = survey.getPageByName(pageName);
        
        const pageQuestions = page.questions;
        const totalNumberOfQuestions = pageQuestions.length;
        const answeredQuestionsPerPage = pageQuestions.filter(question => !question.isEmpty()).length;

        const text = page.title || page.name;
        const content = (
            <span>
                {text} {" (Answered "}
                <strong>{answeredQuestionsPerPage}</strong>
                {"/"}
                <strong>{totalNumberOfQuestions}</strong>
                {")"}
            </span>
        );

        const backgroundClass =
            answeredQuestionsPerPage === 0
                ? "no-answered"
                : answeredQuestionsPerPage === totalNumberOfQuestions
                    ? "all-answered"
                    : "some-answered";

        return (
            <div className={`toc-item-container ${backgroundClass}`}>
                {content}
            </div>
        );
    }
}

ReactElementFactory.Instance.registerElement("sv-custom-toc-item", (props) => {
    return React.createElement(CustomTOCItem, props);
});
//...
survey.findLayoutElement("toc-navigation").getData().listModel.itemComponent = "sv-custom-toc-item";

Please consider the following API enhancements:

  • Introduce a property within SurveyModel which would allow developers to specify a custom TOC component name.
survey.tocItemComponent = "sv-custom-toc-item";
  • Update a custom TOC item component props and add all required objects:
    • Instead of const survey = this.props.model.locOwner, use this.props.survey
    • Instead of this
       const pageName = this.props.item.id;
       const page = survey.getPageByName(pageName);
    
    Add the this.props.page.
    • Perhaps, add this.props.item to access a current IAction.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions