Change Filters When Using SisenseJS
Introduction
Sisense allows importing widgets and filters as components to be presented in different websites. This is done by SisenseJS. Sometimes, it is required to modify the widgets filters based on website attributes such as logged in user, location, previous user selections etc.
Using the below scripts, you will be able to modify the dashboard filters and save them to the server so data will be saved even when refreshing the page.
Code Snippet:
1. Modifying dashboard filters:
// Find the filter we want to change based on the dim
var filterToChange = dash.filters.$$filters.find(item => item.jaql.dim == "[Commerce.Age Range]");
if (filterToChange && filterToChange.$$model.jaql.filter){
var members = filterToChange.$$model.jaql.filter.members;
// Check if we already have the member selected
if (members && !members.includes("65+")) {
// Push a new member to the filter
members.push("65+");
// Save the dashboard
dash.$$model.$dashboard.updateDashboard(dash.$$model,"filters");
}
}
2. Save filters changes to the server is done by sending "true" as the second parameter to the "connect" function:
Sisense.connect('http://10.20.4.155:8081',true).then(function(app) {
.
.
.
});
3. Create and add new filter to the dashboard:
var applyFilter = function (dim, value, activeDashboard) {
// Get the active dashboard, only run if sisense is loaded
if (activeDashboard) {
// Create the filter options
var filterOptions = {
save: true,
refresh: true,
unionIfSameDimensionAndSameType: false
};
// Create the jaql for the filter
var jaql = {
'datatype': 'text',
'dim': dim,
'filter': {
'multiSelection': true,
'members': value,
'explicit': true
},
"title": dim
};
// Create the filter jaql
var applyJaql = {
jaql: jaql
};
// Set the new filter
activeDashboard.$$model.filters.update(applyJaql, filterOptions);
}
};
Download:
Updated 03-02-2023
intapiuser
Admin
Joined December 15, 2022