Need a way to select the first value by default in a filtered list for a single select filter

Comments

4 comments

  • Avatar
    Elliott Herz

    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.

    0
    Comment actions Permalink
  • Avatar
    JJ Chritton

    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!

    0
    Comment actions Permalink
  • Avatar
    Elliott Herz

    Awesome to hear JJ! I'm glad I could help out!

    0
    Comment actions Permalink
  • Avatar
    Sophia Lee

    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

    0
    Comment actions Permalink

Please sign in to leave a comment.