How to change a dashboard variable value from a plugin?

Hello-

What is the best way for a panel plugin to update a variable? I have tried:

  this.$location.search( 'var-'+name, value );

This updates the URL properly, and on browser refresh, the variables is set, but I would like to actively set it.

Thanks for any pointers!

ryan

I found a solution that works: the key is: this.variableSrv.variableUpdated(v, true);

  updateVarable(varname:string, path:string) {
      console.log('update variable', varname, path );
      let v = _.find(this.variableSrv.variables, check => {
        return check.name === varname;
      });
      if(v) {
        this.variableSrv.setOptionAsCurrent(v, {
          text: path,
          value: path,
        });
        this.variableSrv.variableUpdated(v, true);
      }
  }
1 Like

Hello,
How to you access variableSrv ? What class do you have to import?

Best regards?

You can just put variableSrv in the constructor and it will get wired up for you. See:

Thanks :slight_smile: work very well :slight_smile:

how about react plugins, how do we access variableSrv?