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)