This definitely took some serious time/research to resolve here is how I resolved it:
my dashboard variable is named âlocation_idâ
the variable is used in a query âselect location_name from location where locationid=$location_idâ
when a user clicks on a popup I needed to update the variable so I could refresh the data for other visual components.
import { DataSourceApi } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime';
handleOnPopup = (e:any, locationId:number ) => {
let dataSource:DataSourceApi = getDataSourceSrv() as unknown as DataSourceApi;
let variable = _.find(dataSource.templateSrv.variables, {'name':'location_id'});
this.locationSelectedId = locationId;
variable.query = locationId.toString(); //make string even if its a number
variable.variableSrv.updateOptions(variable).then(() => {
variable.variableSrv.variableUpdated(variable, true);
});
}
render(){
//check the data state and get your updated data
if(data.state == "Done" && data.series[2] && data.series[2].length > 0 && data.series[2]["fields"][0]["values"].buffer[0] == this.locationSelectedId){
this.locationDevices = data.series[2];
}
}
This worked for me to access variables in custom panel when using the âMixedâ mode (in the DS select box).
this is my code:
import { DataSourceApi } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime';
let dataSource:DataSourceApi = getDataSourceSrv() as unknown as DataSourceApi;
let variable = _.find(dataSource.templateSrv.variables, {'name':'location_id'});
I Grafana 7.0 there is no access to variableSrv. I changed the Url using LocationSrv but it does not update panels and selector of variable actively. I asked another question in this link if you have any idea let me know. thanks
My understanding is the bug that is preventing the locationsrv from updating after changing the variable is to be fixed in the 7.1 release (I believe the targeted release date is July 14th 2020)
Iâm having trouble with the url change not triggering the template variable update in Grafana v11.2.0 (2a88694fd3). Iâm trying to update the value of one template variable on another template variable change event using the Grafana interfaceâs Business Chart Plugin. I am able to update the Dahsbord URL, but it doesnât trigger the template variable to be updated.
@himalipancholipancho Are you using the new Scenes dashboards? Grafana introduced breaking changes in the variables and dashboard refreshes. We addressed those issues in the upcoming releases of our plugins.