How can I change template varibale in a react plugin in grafana 7.0?

I want to change template variable from a plugin which I developed in React. I check two previous topics about updating the template variables. They say that I have to use variableSrv but how can I access this variable in grafana 7.0 and React?

1 Like

variableSrv is going away for upcoming versions of Grafana. I’d recommend that you take a look at LocationSrv to update the template variable using the URL:

For example,

getLocationSrv().update({
  partial: true, 
  query: {
    'var-instance': instanceName,
  }, 
  replace: true,
});

For more information on the options, check out LocationUpdate.

3 Likes

Thanks for your reply. I test this method, URL get updated but the selector of variable and the dependent panels do not update. How should I force updating the panels and selector of variables?

Just learned that updating links this way is currently broken, and that we’re working on fixing it. You can follow the progress here: https://github.com/grafana/grafana/pull/25192

1 Like

Hello, I follow the the code example from @marcusolsson and it works but Grafana start to make request until get an error:

Unhandled promise rejection InternalError: "too much recursion" _invoke runtime.js:263 t runtime.js:97 Ht app.bb5d8fb9085bbb733af9.js:2 i app.bb5d8fb9085bbb733af9.js:2 Xt app.bb5d8fb9085bbb733af9.js:2 H es.promise.js:233 Xt app.bb5d8fb9085bbb733af9.js:2 t actions.ts:20 $/</</< Redux i store.ts:27 e adapter.ts:29 c runtime.js:45 host-report-errors.js:6:56

Did you have this error also?

Hi, I’m trying to use this method to update variables. Right now I’m seeing:

new_variable=new_value

in the URL parameters so it works. Is this the way to add ad hoc filters for elasticsearch datasource? If so, how do I change the equals to something else?

Thank you!

This is what I’m trying to achieve: Set ad hoc filter from Panel Plugin

Be careful that on variable change url changes such as var-${variableName} ; thus the ‘var-’ in marcusolsson’s answer.