Juandres
03-13-2024Cloud Apps
Amend a Granularity button to filter as well
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:
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"
}
}
]