How to set an embedded dashboard filter name as a saved variable with JSON.stringify()

Knowledge Drop

Last tested: Dec 24, 2018
 

When using embedded Javascript events you might use JSON.stringify() to pass a string into the postMessage() function.

Example function that takes in filter and value and tries to pass them into the JSON inside of JSON.stringify()

function updateFilters(filter, value) {

var iframe = document.getElementById('looker');

var update_request = JSON.stringify(

{

"type": "dashboard:filters:update",

"filters": {

filter : value

}

}

)
iframe.contentWindow.postMessage(

update_request,

"https://{{ domain }}"

)

}

This will not work, the value variable will be turned into a value like "Sweaters", but the filter will turn into "filter" and not the name of the filter on the dashboard like "Category"

To get both the key AND the value passed in, we need to add this data into the JSON differently

...

filter_name = "Category"

var object = {

"type": "dashboard:filters:update",

"filters": {}

}

object['filters'][filter_name] = value

...

This will go into the filters hash of object, and then set "Category" = some_value you click on.

Sources:  StackOverflow

This content is subject to limited support.                

Version history
Last update:
‎06-14-2021 06:16 PM
Updated by: