Set filter value by script

Contents[Hide]

1. Overview

This article walks through how to set a parameter value using script. In the example shown below, a filter is set to a hierarchy value on a click of a button.

While script is not required to accomplish an interaction like this in Dundas BI, the purpose of this article is to demonstrate how you can perform this action using the API as part of your own custom functionality.

Script Library samples: Filter and Parameter

2. Set up the dashboard

For this example, a relational table (non-OLAP) is used.

Relational Table
Relational Table

Add a data visualization to the dashboard such as a table or chart. Drop a measure under Measures and a hierarchy to Rows in the Data Analysis Panel. In this example, the Country column is also added to Slicers.

Data Analysis Panel
Data Analysis Panel

Add a hierarchy filter and connect it to your column. Take note of the View Parameter Script Name, which in this case is viewParameter1. For your first filter created on the dashboard, this is the default name, but you can rename it if you choose.

Filter Visualizations panel
Filter Visualizations panel

Take note also of the filter control name, which is parameterHierarchy1. Like the View Parameter, this is the default name for the first filter added, but you can rename this if you choose.

Check the parameter script name
Check the parameter script name

3. Determine the required script

The script to use depends on the type of value you want to set and how you determine its value. You can find details and examples of various parameter types in the article Modify a filter / view parameter using scripting.

Below, we will walk through one way to find a hierarchy member's information interactively and use it to set a CollectionMemberValue type parameter.

Add a button component and add the script debugger; in the button's Click script action. (The article Writing scripts using browser developer tools contains more details on this JavaScript statement.)

Button click interaction script
Button click interaction script

View the dashboard and select the filter value (Japan in this example).

Click the button to fire the Click script action.

Firing the script
Firing the script

In the browser's JavaScript console, type the script below while the debugger is paused within your click script action (adjust the filter control name if necessary):

parameterHierarchy1.control.parameterValue;

Browser developer tools' console
Browser developer tools' console

Inspect the parameter's structure and the values, particularly the following:

  • hierarchyUniqueName = "Country"
  • LevelUniqueName = "Country.Regular"
  • uniqueName = "Japan.Country"

4. Add the script

Note
You can add this script to any event, such as a button's Click, a dashboard's Load or Ready, a parameter's value changed actions, etc. If you use the script in the actions of a dashboard or other view, you should replace this.parentView with simply this.

var viewParameter = this.parentView.control.getViewParameterByName("viewParameter1");
var filterValue = new dundas.data.MemberValue({
  hierarchyUniqueName: "Country",
  levelUniqueName: "Country.Regular",  
  uniqueName: "Japan.Country"
});

viewParameter.parameterValue.token = null;
viewParameter.parameterValue.values.length = 0;
viewParameter.parameterValue.values.push(filterValue);

viewParameter.invalidateParameterValueLastModifiedTime();
viewParameter.refreshAllAdapters(null, this.parentView);
// (Pass the current view in case it's embedded inside another)

Add the script to a button's click actions
Add the script to a button's click actions

5. Test the script

  1. View the dashboard.
  2. Select any filter value other than the one you're setting through script.
  3. Click the Button control to fire the script.

Result: The new filter value should be set:

Add the script in a button's click actions
Add the script in a button's click actions

6. See also

Dundas Data Visualization, Inc.
400-15 Gervais Drive
Toronto, ON, Canada
M3C 1Y8

North America: 1.800.463.1492
International: 1.416.467.5100

Dundas Support Hours:
Phone: 9am-6pm, ET, Mon-Fri
Email: 7am-6pm, ET, Mon-Fri