JavaScript/jQuery Stepper - Implement a Wizard

This tutorial uses a Stepper widget to create a wizard-like application. The created application integrates the Form and MultiView components.

This wizard includes three key parts:

  1. The Stepper component.
  2. Step content where the MultiView component shows content for each step. Every view with input fields contains a Form.
  3. A navigation panel with an active step caption ("Step 1 of 5") and buttons for moving between steps ("Next" and "Back").

Stepper wizard elements

To view the complete code of this example, refer to the following demo:

Stepper - Form Integration Demo

Set Up App Layout

To get started, add a Stepper and a MultiView to your project. Create a navigation panel - a container with child items. Child items are a <div> element that displays the active step caption and two Buttons:

index.html
index.js
  • <div id="stepper"></div>
  • <div class="content">
  • <div id="stepContent"></div>
  • <div class="nav-panel">
  • <div class="current-step"> Step <span class="selected-index">1</span> of 5</span></div>
  • <div class="nav-buttons">
  • <div id="prevButton"></div>
  • <div id="nextButton"></div>
  • </div>
  • </div>
  • </div>
  • const stepper = $('#stepper').dxStepper({
  • // ...
  • items: steps,
  • }).dxStepper('instance');
  •  
  • const stepContent = $('#stepContent').dxMultiView({
  • // ...
  • focusStateEnabled: false,
  • swipeEnabled: false,
  • items: multiViewItems,
  • }).dxMultiView('instance');
  •  
  • const prevButton = $('#prevButton').dxButton({
  • // ...
  • text: 'Back',
  • visible: false,
  • }).dxButton('instance');
  •  
  • const nextButton = $('#nextButton').dxButton({
  • // ...
  • text: 'Next',
  • }).dxButton('instance');

The "Back" button is initially hidden. A later step in this tutorial implements dynamic visibility logic (see Synchronize Steps and Navigation Panel).

Configure forms for each step with MultiView items[].template:

index.js
  • const multiViewItems = [
  • { template: getDatesForm() },
  • { template: getGuestsForm() },
  • { template: getRoomAndMealForm() },
  • { template: getAdditionalRequestsForm() },
  • { template: getConfirmationTemplate() },
  • ];
  •  
  • function getDatesForm() {
  • return () => $('<div>').append(
  • $('<p>').text('Select your check-in and check-out dates. If your dates are flexible, include that information in Additional Requests. We will do our best to suggest best pricing options, depending on room availability.'),
  • $('<div>').dxForm({
  • formData,
  • items: [{
  • dataField: 'dates',
  • editorType: 'dxDateRangeBox',
  • editorOptions: {
  • elementAttr: { id: 'datesPicker' },
  • startDatePlaceholder: 'Check-in',
  • endDatePlaceholder: 'Check-out',
  • },
  • label: { visible: false },
  • }],
  • }),
  • );
  • }
  •  
  • function getGuestsForm() {
  • // Configure the form
  • }
  •  
  • function getRoomAndMealForm() {
  • // Configure the form
  • }
  •  
  • function getAdditionalRequestsForm() {
  • // Configure the form
  • }
  •  
  • function getConfirmationTemplate() {
  • // Configure the form
  • }

Synchronize Steps and Navigation Panel

To synchronize the selectedIndex properties of Stepper and MultiView, set them to a common value in a new function, setSelectedIndex. Call this function in the onSelectionChanged handler of Stepper and the onClick handlers of both navigation buttons:

index.js
  • const stepper = $('#stepper').dxStepper({
  • // ...
  • onSelectionChanged(e) {
  • const selectedIndex = e.component.option('selectedIndex');
  •  
  • setSelectedIndex(selectedIndex);
  • },
  • }).dxStepper('instance');
  •  
  • const prevButton = $('#prevButton').dxButton({
  • // ...
  • onClick: () => {
  • const selectedIndex = stepper.option('selectedIndex');
  •  
  • setSelectedIndex(selectedIndex - 1);
  • },
  • visible: false,
  • }).dxButton('instance');
  •  
  • const nextButton = $('#nextButton').dxButton({
  • // ...
  • onClick: () => {
  • const selectedIndex = stepper.option('selectedIndex');
  •  
  • if (selectedIndex < steps.length - 1) {
  • setSelectedIndex(selectedIndex + 1);
  • }
  • },
  • }).dxButton('instance');
  •  
  • function setSelectedIndex(index) {
  • stepper.option('selectedIndex', index);
  • stepContent.option('selectedIndex', index);
  • setCurrentStepCaption(index);
  • updateStepNavigationButtons(index);
  •  
  • if (index === steps.length - 1) {
  • stepContent.option('items[4].template', getConfirmationTemplate());
  • }
  • }

setSelectedIndex also updates the navigation panel. It calls the following methods: updateStepNavigationButtons and setCurrentStepCaption.

On the last step, the "Next" button changes to "Confirm". The "Confirm" button submits the form and disables user interactions with the Stepper. For instructions on how to disable Stepper interactions, refer to the following help topic: Configure a Readonly Stepper.

After users submit the form, they can reset the wizard and start over.

index.js
styles.css
  • let confirmed = false;
  •  
  • const nextButton = $('#nextButton').dxButton({
  • onClick: () => {
  • if (selectedIndex < steps.length - 1) {
  • setSelectedIndex(selectedIndex + 1);
  • } else if (confirmed) {
  • reset();
  • } else {
  • confirm();
  • }
  • }
  • })
  •  
  • function setCurrentStepCaption(index) {
  • if (confirmed) {
  • $('.current-step').empty();
  • } else if (!$('.current-step').text()) {
  • $('.current-step').append(`Step <span class="selected-index">${index + 1}</span> of ${steps.length}`);
  • } else {
  • $('.selected-index').text(index + 1);
  • }
  • }
  •  
  • function reset() {
  • confirmed = false;
  • setSelectedIndex(0);
  • // ...
  • }
  •  
  • function confirm() {
  • confirmed = true;
  • setSelectedIndex(steps.length - 1);
  • // ...
  • }
  •  
  • function updateStepNavigationButtons(index) {
  • const isLastStep = index === steps.length - 1;
  • const lastStepNextButtonText = confirmed ? 'Reset' : 'Confirm';
  • const nextButtonText = isLastStep ? lastStepNextButtonText : 'Next';
  •  
  • prevButton.option('visible', !!index && !confirmed);
  • nextButton.option('text', nextButtonText);
  • }
  •  
  • function setStepperReadonly(readonly) {
  • stepper.option('focusStateEnabled', !readonly);
  •  
  • if (readonly) {
  • stepper.option('elementAttr', { class: 'readonly' });
  • } else {
  • stepper.resetOption('elementAttr');
  • }
  • }
  • .readonly {
  • pointer-events: none;
  • }

Implement Validation

To configure validation, assign validation groups and specify validation rules in each MultiView form that requires validation:

index.js
  • const validationGroups = ['dates', 'guests', 'roomAndMealPlan'];
  •  
  • function getDatesForm() {
  • return () => $('<div>').append(
  • // ...
  • $('<div>').dxForm({
  • // ...
  • validationGroup: validationGroups[0],
  • items: [{
  • // ...
  • isRequired: true,
  • }],
  • }),
  • );
  • }

This tutorial validates steps only when users move forward. If a step fails validation, navigation operation is cancelled:

index.js
  • const stepper = $('#stepper').dxStepper({
  • // ...
  • onSelectionChanging(args) {
  • const { component, addedItems, removedItems } = args;
  • const { items = [] } = component.option();
  •  
  • const addedIndex = items.findIndex((item) => item === addedItems[0]);
  • const removedIndex = items.findIndex((item) => item === removedItems[0]);
  • const isMoveForward = addedIndex > removedIndex;
  •  
  • if (isMoveForward && validateStep(removedIndex) === false) {
  • args.cancel = true;
  • }
  • },
  • }).dxStepper('instance');
  •  
  • const nextButton = $('#nextButton').dxButton({
  • // ...
  • onClick: () => {
  • // ...
  • if (selectedIndex < steps.length - 1) {
  • if (validateStep(selectedIndex)) {
  • setSelectedIndex(selectedIndex + 1);
  • }
  • }
  • // ...
  • }
  • })
  •  
  • function validateStep(index) {
  • const isValid = getValidationResult(index);
  •  
  • stepper.option(`items[${index}].isValid`, isValid);
  •  
  • return isValid;
  • }
  •  
  • function getValidationResult(index) {
  • if (index >= validationGroups.length) {
  • return true;
  • }
  •  
  • return DevExpress.validationEngine.validateGroup(validationGroups[index]).isValid;
  • }

Review the full code of this tutorial in the following demo:

Stepper - Form Integration Demo