Need a way to select the first value by default in a filtered list for a single select filter
We have a dashboard that is focused on viewing user performance and thus requires a single-select user filter so you can view only one user at a time. We deploy a single dashboard for this purpose but use the dynamic elasticube switcher plugin to switch which data source we're using based on which customer is accessing the report. However, when we load the dashboard it defaults to a specific selected user (whoever was selected during when the dashboard was exported/imported).
My preference would be that a single-select filter should always default to the first available value if the currently selected value no longer exists in the data source, but that's obviously not how it works. Is there a way to put a dashboard script in place to update the filter each time the dashboard is loaded with a filter option that's missing from the data source so we're not defaulting to a view with no data?
-
Hey,
Try out this dashboard script. You will need to update the parameter called defaultFilterConfig.dim1. You need to put in the correct dimension which can be found if you save the dashboard script, refresh the brower and open the developer tools and navigate to the console, you will see all the filter dimensions being printed out. Replace that parameter with the correct one and you should be good to go.
var defaultFilterConfig = {
dim1: "[DIM_DATE.DATE (Calendar)]"
};
dashboard.on('initialized', function(d, args) {
//print out all dimensions
for(var i=0; i<args.dashboard.filters.$$items.length; i++) {
if(args.dashboard.filters.$$items[i].levels != null) { //this is for dependent filters
for(var j=0; j<args.dashboard.filters.$$items[i].levels.length; j++) {
console.log(args.dashboard.filters.$$items[i].levels[j].dim);
}
}
else { //normal filters
console.log(args.dashboard.filters.$$items[i].jaql.dim);
}
}
//query the filter dimension to retrieve all members sorted descending
var dimJaql = '{"dim": "' + defaultFilterConfig.dim1 + '", "sort": "desc"}';
var query = '{"datasource": ' + JSON.stringify(args.dashboard.datasource) + ', "metadata": [' + dimJaql + ']}';
var url = '/api/elasticubes/' + args.dashboard.datasource.title + '/jaql';
$.ajax({
async:false,
method: 'POST',
url: url,
data: query,
success: function(result) {
for(var i=0; i<args.dashboard.filters.$$items.length; i++) {
if(args.dashboard.filters.$$items[i].levels != null) { //this is for dependent filters
for(var j=0; j<args.dashboard.filters.$$items[i].levels.length; j++) {
if(defaultFilterConfig.dim1 === args.dashboard.filters.$$items[i].levels[j].dim) {
args.dashboard.filters.$$items[i].levels[j].filter = {
explicit: true,
members:[result.values[0][0].data],
multiSelection: false,
userMultiSelect: false
};
}
}
}
else if(defaultFilterConfig.dim1 === args.dashboard.filters.$$items[i].jaql.dim) { //this is for normal filters
args.dashboard.filters.$$items[i].jaql.filter = {
explicit: true,
members:[result.values[0][0].data],
multiSelection: false,
userMultiSelect: false
};
}
}
},
contentType: "application/json; charset=utf-8",
dataType: "json"
});
});
Cheers! Let me know if this is what you were looking for.
-
This is exactly what I needed. I loved that you included the extra script to dump out the filter dimensions because sometimes those names are not intuitive. The only bit I changed other than the dimension was sorting the list in asc (ascending) order so it picks the one at the top of the order alphabetically instead of the last entry. Thank you for the fantastic work on this script!
-
Hi Elliott
We have a very similar scenario however we also have background filters and whilst this script enabled the single-select filter to always default to the first available value, we noticed that is also removed our background filters which are equally important as these reduce the list of data presented on the dashboards.
Would really appreciate your help!
Thanks
Please sign in to leave a comment.
Comments
4 comments