Set filter value by script

Contents[Hide]

1. Overview

This article shows you how to set a filter value by script. For example, you may want to set a different filter value by script, such as when passing filter values from one dashboard to another, or to another filter on the same dashboard. The basic idea is to add a script (JavaScript code) that will change the filter value when the interaction is fired. A sample is shown below, where the filter value is set on a click of a button control.

2. Set-up the dashboard

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

Relational Table
Relational Table

  1. Add a Data Visualization Control on the dashboard, e.g. a Data Table or Chart. Drop a measure and dimension to the MEASURES and ROWS section of the Data Binding Panel. In this sample, a SLICER is used using the Country column.

    Data Binding Panel
    Data Binding Panel

  2. Add a member filter and bind 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 if you want.

    Data Binding Panel
    Data Binding 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 want.

    Check the parameter script name
    Check the parameter script name

3. Determine the required script

  1. Add a Button control and add the script debugger; in the Button Click interaction script. Refer to Writing scripts using browser developer tools for reference.

    Button click interaction script
    Button click interaction script

  2. View the dashboard.

  3. Select the filter value, e.g. "Japan".

  4. Click the button control to fire the Click interaction script.

    Firing the script
    Firing the script

  5. On the Console, type the script below (should match your parameter name):

    parameterHierarchy1.control.parameterValue;

    Developer Tools' console
    Developer Tools' console

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

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

4. Add the Script

You can add this script to any interaction, such as a Button Click, on Dashboard Load or Ready, on a Parameter Changed interaction, etc. If you use the script on Load interaction, you need to replace "dundas.context.baseViewViewService.currentView.control" with "this.control".

var baseViewService = this.getService("BaseViewService");
var myParameter = baseViewService.currentView.control.viewParameters.filter(function(item) {
  return item.name === "viewParameter1";
})[0];

var filterValue = new dundas.data.MemberValue({
  hierarchyUniqueName: "Country",
  levelUniqueName: "Country.Regular",  
  uniqueName: "Japan.Country"
});

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

myParameter.refreshAllAdapters();
//if you are setting the filter values for an embedded dashboard, 
//then the following line of code has to be used instead to refresh the adapters
//myParameter.refreshAllAdapters(null, view);

Add the script in a Button Click Interaction
Add the script in a Button Click Interaction

5. Test the script

  1. View the dashboard
  2. Select any filter value, except the one you're setting through script.
  3. Click the Button control to fire the script, in case you've added the script on a Button Click interaction.

Result: The new filter value should be set:

Add the script in a Button Click Interaction
Add the script in a Button Click Interaction

6. See also

Dundas Data Visualization, Inc.
500-250 Ferrand Drive
Toronto, ON, Canada
M3C 3G8

North America: 1.800.463.1492
International: 1.416.467.5100

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