cancel
Showing results for 
Search instead for 
Did you mean: 

Amend a Granularity button to filter as well

Juandres
8 - Cloud Apps
8 - Cloud Apps

Good day all,

Can someone please assist me with creating a custom action, I have tried everything but I cannot get it to work 
I have these date granularity buttons, But I need the button to affect the dashboard as well if a selection is made 
Here are my buttons 

{
"style": "",
"script": "",
"title": "",
"titleStyle": [
{
"display": "none"
}
],
"showCarousel": true,
"body": [],
"actions": [
{
"type": "dynamicGranSwap",
"title": "Months",
"style": {
"background-color": "#1D426C"
},
"data": {
"widgetToModify": [
"65f1604d8f23ac0033253ddd"
],
"filterJaql": {
"explicit": true,
"multiSelection": true,
"members": [
"1"
]
},
"dim": {
"title": "Month_End",
"table": "Calendar",
"column": "Month_End",
"datatype": "Int"
}
},
"dategran": "months"
},
{
"type": "dynamicGranSwap",
"title": "Days",
"style": {
"background-color": "#D3D3D3"
},
"data": {
"widgetToModify": [
"65f1604d8f23ac0033253ddd"
]
},
"dategran": "days"
}
],
"dateFilters": {
"dim": {
"title": "Month_End",
"table": "Calendar",
"column": "Month_End",
"datatype": "Int"
},
"title": "Month_End",
"column": "Month_End",
"table": "Calendar",
"datatype": "Int",
"level": "days",
"filter": {
"last": {
"count": 8,
"offset": 0
}
}
}
}
And here is the 
dynamicGranSwap Action 
// Holds the chosen granularity from the selected button 'months' for example
const dategran = payload.dategran;
const widgetIds = payload.data.widgetToModify;
var filtername = "Month_End";

//Change the background color for unselected buttons
payload.widget.style.currentCard.actions.forEach(function (i) {
i.style["background-color"] = '#D3D3D3';
});

//Change the background color for selected buttons
payload.widget.style.currentCard.actions
.filter(i => i.dategran == dategran)[0].style["background-color"] = "#1D426C";

//Redraw the changes
payload.widget.redraw();

//For each widget change the data granularity and update filter Jaql if widget has filter controls
widgetIds.forEach(function (widgetId) {
const widget = payload.widget.dashboard.widgets.$$widgets.find(widget => widget.oid === widgetId);
if (widget) {
// Change the level of granularity to the chosen value from our button: 'months' for example
widget.metadata.panels[0].items[0].jaql.level = dategran;
// Apply changes to Mongo
widget.changesMade('someEvent', ['metadata', 'properties_changed']);
// Refresh widget
widget.refresh();

// Update filter Jaql if widget has filter controls
const filterControl = widget.metadata.controls && widget.metadata.controls.find(control => control.type === "Filters");
if (filterControl && filterControl.data.filters && filterControl.data.filters.length > 0) {
const filterJaql = filterControl.data.filters[0].filterJaql;
if (filterJaql) {
if (dategran === "months") {
filterJaql.name = filterName;
filterJaql.explicit = true;
filterJaql.members = ["1"]; // Update members as needed
} else if (dategran === "days") {
filterJaql.name = filterName;
filterJaql.explicit = false;
filterJaql.multiSelection = true;
filterJaql.all = true;
}
}
}
}
});
And these are the filters, 
if Daily is selected: 
"filters": [
{
"filterJaql": {
"explicit": false,
"multiSelection": true,
"all": true
},
"dim": {
"title": "Month_End",
"table": "Calendar",
"column": "Month_End",
"datatype": "Int"
}
}
]
And this if Months are selected: 
"filters": [
{
"filterJaql": {
"explicit": true,
"multiSelection": true,
"members": [
"1"
]
},
"dim": {
"title": "Month_End",
"table": "Calendar",
"column": "Month_End",
"datatype": "Int"
}
}
]


 

1 REPLY 1

rapidbisupport
10 - ETL
10 - ETL

Hi @Juandres ,

Nice script!

I remember that this (viewer switching date levels on the fly) was the exact scenario that led to RAPID BI developing Sisense Add-Ons | Adaptive Switcher (rapidbi.com.au) which allows us and our customers to switch items (dims / measures / date grans) more generally against all or specifically selected widgets.

In any case - it looks like your script is failing at:

 

widget.changesMade('someEvent', ['metadata', 'properties_changed']);

 

I've tested removing this and it works on my side. It won't persist changes on the widget level though.

I've typically used this function in this way in the past, despite being different to my memory of how it was documented in the Sisense release notes:

 

widget.changesMade('widget', 'metadata');

 

Let me know how you go?

Thanks,

Daniel

RAPID BI

[email protected]

RAPID BI - Sisense Professional Services | Implementations | Custom Add-ons