Skip to main content
A newer version of this page is available. .

Manage Dashboard State in JavaScript Applications

  • 4 minutes to read

A dashboard state describes the result of client actions users perform on a dashboard. The DashboardState class contains:

You can manage the following objects in a dashboard state:

State Value

API

selected master filters values

ItemState.MasterFilterValues

current drill-down levels

ItemState.DrillDownValues

a selected tab page

ItemState.TabPageName

Range Filter/Date Filter settings

ItemState.RangeFilterState

a selected dashboard item layer[1]

ItemState.SelectedLayerIndex

selected parameter values

DashboardState.Parameters

API Overview

The DashboardState class represents a dashboard’s state on the server side and includes individual dashboard items’ states (the DashboardState.Items property) and the selected parameter values (DashboardState.Parameters). On the client side, a dashboard state is a JSON object that stores the same data. The table below lists the main client-side API members related to obtaining and setting a dashboard state:

Client-side API

Description

DashboardControl.getDashboardState

Gets the current dashboard state.

DashboardControl.setDashboardState

Applies the dashboard state to the loaded dashboard.

DashboardControlOptions.onDashboardStateChanged

A handler for the event that occurs after the current dashboard state in the Web Dashboard is changed.

DashboardControlOptions.initialDashboardState

Specifies the initial state of the loaded dashboard.

Use the initialDashboardId option to set the identifier of the dashboard to which the state is applied.

To specify the initial dashboard state, initialize the DashboardState object on a server, save this object to JSON using the DashboardStateExtensions.SaveToJson extension method and assign the resulting string to the initialDashboardState property.

The following code shows how to pass a string that contains a dashboard state saved in JSON format to the initialDashboardState property:

var dashboardControl = new DashboardControl(this.element.nativeElement.querySelector(".dashboard-container"),  {
  endpoint: "https://demos.devexpress.com/services/dashboard/api",
  workingMode: "Viewer",
  initialDashboardState: "{\"Items\":{\"gridSalesByState\":{\"MasterFilterValues\":[[\"Nevada\"]]}}}",
});

Include a Dashboard State to URL

You can include a dashboard state in the browsed dashboard’s URL to allow users to share a dashboard state. To do this, enable the UrlStateExtensionOptions.includeDashboardStateToUrl property. In this case, the resulting link allows users to open the dashboard with the specified state. Note that these properties are in effect when the DashboardControlOptions.workingMode is set to “Viewer” or “ViewerOnly”.

The code sample below shows how to include a dashboard state to URL:

var dashboardControl = new DashboardControl(this.element.nativeElement.querySelector(".dashboard-container"),  {
    endpoint: "https://demos.devexpress.com/services/dashboard/api",
    workingMode: "Viewer",
    extensions: {
        "urlState": {
            includeDashboardStateToUrl: true
        }
    }
});

Note

You can also include the selected dashboard’s identifier in a URL using the UrlStateExtensionOptions.includeDashboardIdToUrl property.

Examples

How to Apply a Dashboard State

The following example shows how to apply the dashboard state to the Energy Statistics demo. The Year parameter is 2008, the Card‘s master filter value is Solid Fuels. MVCxDashboard is the client instance name of the DashboardControl.

function onClick() {
  var control = MVCxDashboard.GetDashboardControl();
  control.setDashboardState({
    "Parameters": {
      "Year": 2008
    },
    "Items": {
      "cardProductionImportByType": {
          MasterFilterValues: [["Solid Fuels"]]
      }
    }
  });
}

The code below shows how to apply the dashboard state to the Sales Overview demo. The Range Filter‘s predefined period is 6 Month, the Grid‘s master filter value is Nevada. MVCxDashboard is the client instance name of the DashboardControl.

function onClick() {
  var control = MVCxDashboard.GetDashboardControl();
  control.setDashboardState({
      "Items": {
      "gridSalesByState": {
        MasterFilterValues: [["Nevada"]]
      },
      "range": {
        RangeFilterState: {
          PeriodName: "6 Months"
        }
      }
     }
  });
}

How to Reset a Dashboard State to Default

The code below clears filters (unselects all master filter values), the RangeFilter state, and drill-down levels for dashboard items. To do this, pass an empty object of a dashboard item state to the entire dashboard state.

Note

The control variable in the example below is the obtained DashboardControl instance. Refer to the Extensions Overview topic for information on how to obtain a DashboardControl for each platform.

function onClick() {
  //... 
  var state = JSON.parse(control.getDashboardState());
  if (state && state.Items) {
      Object.keys(state.Items).forEach(function (itemName) {
          var itemState = state.Items[itemName];
          itemState.MasterFilterValues = [];
          itemState.DrillDownValues = [];
          itemState.RangeFilterState = { };
      }) 
  }
  control.setDashboardState(JSON.stringify(state));
}
Footnotes
  1. For the Pie, Card, Treemap, Gauge and Choropleth Map dashboard items.