Sometimes we need to hide or show few columns in pivot table based on a filter selected. Here is a script to achieve this.
Steps:
Create a Pivot table
Add panels to enable/disable based on selected filter and disable them. It will enable dynamically based on selection.
Add below script to widget
Update the variable 'ItemList' with mapping of filter item to name of panels to be enabled. Also update the variable 'filterName' with name of dashboard filter based on which the columns enable/disable
Save the script and refresh dashboard.
let filterName = 'Region'
let ItemList = {
'South':['County','Gender', 'City'],
'Northeast':['Gender', 'State'],
'Default':['City', 'State']
}
widget.on("buildquery", function (se, ev) {
let filter = se.dashboard.filters.$$items.find(el=>el.jaql.title == filterName)
let filterItem = filter.jaql.filter.members[0]
let columnList = ItemList[filterItem]
if(columnList == null || columnList == undefined)
columnList = ItemList['Default']
$.each(ev.widget.metadata.panels[0].items, function(index, value){
if(value.disabled == true && columnList.includes(value.jaql.title) )
{
var newJaql = {
jaql : JSON.parse(JSON.stringify(value.jaql))
}
ev.query.metadata.push(newJaql)
lastIndex = ev.query.metadata.length - 1
ev.query.metadata[lastIndex].disabled = false
ev.query.metadata[lastIndex].panel = 'rows'
}
})
$.each(ev.widget.metadata.panels[1].items, function(index, value){
if(value.disabled == true && columnList.includes(value.jaql.title) )
{
var newJaql = {
jaql : JSON.parse(JSON.stringify(value.jaql))
}
ev.query.metadata.push(newJaql)
lastIndex = ev.query.metadata.length - 1
ev.query.metadata[lastIndex].disabled = false
ev.query.metadata[lastIndex].panel = 'measures'
}
})
})
Komentarze