Skip to content

Commit 6f094ef

Browse files
committed
fix(templating): fixed issue with nested template variables and multi select, the child variable selection state is now updated like single select variables, so if none matches the first option is selected, fixes grafana#4861
1 parent c41c771 commit 6f094ef

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

public/app/features/templating/templateValuesSrv.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,26 @@ function (angular, _, kbn) {
244244
this.validateVariableSelectionState = function(variable) {
245245
if (!variable.current) {
246246
if (!variable.options.length) { return; }
247-
return self.setVariableValue(variable, variable.options[0], true);
247+
return self.setVariableValue(variable, variable.options[0], false);
248248
}
249249

250250
if (_.isArray(variable.current.value)) {
251251
self.selectOptionsForCurrentValue(variable);
252+
// updated selected value
253+
var selected = {
254+
value: _.map(_.filter(variable.options, {selected: true}), function(op) {
255+
return op.value;
256+
})
257+
};
258+
// if none pick first
259+
if (selected.value.length === 0) {
260+
selected = variable.options[0];
261+
}
262+
return self.setVariableValue(variable, selected, false);
252263
} else {
253264
var currentOption = _.findWhere(variable.options, {text: variable.current.text});
254265
if (currentOption) {
255-
return self.setVariableValue(variable, currentOption, true);
266+
return self.setVariableValue(variable, currentOption, false);
256267
} else {
257268
if (!variable.options.length) { return; }
258269
return self.setVariableValue(variable, variable.options[0]);

public/test/specs/templateValuesSrv-specs.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,46 @@ define([
126126
});
127127
});
128128

129+
describeUpdateVariable('query variable with multi select and new options does not contain some selected values', function(scenario) {
130+
scenario.setup(function() {
131+
scenario.variable = {
132+
type: 'query',
133+
query: '',
134+
name: 'test',
135+
current: {
136+
value: ['val1', 'val2', 'val3'],
137+
text: 'val1 + val2 + val3'
138+
}
139+
};
140+
scenario.queryResult = [{text: 'val2'}, {text: 'val3'}];
141+
});
142+
143+
it('should update current value', function() {
144+
expect(scenario.variable.current.value).to.eql(['val2', 'val3']);
145+
expect(scenario.variable.current.text).to.eql('val2 + val3');
146+
});
147+
});
148+
149+
describeUpdateVariable('query variable with multi select and new options does not contain any selected values', function(scenario) {
150+
scenario.setup(function() {
151+
scenario.variable = {
152+
type: 'query',
153+
query: '',
154+
name: 'test',
155+
current: {
156+
value: ['val1', 'val2', 'val3'],
157+
text: 'val1 + val2 + val3'
158+
}
159+
};
160+
scenario.queryResult = [{text: 'val5'}, {text: 'val6'}];
161+
});
162+
163+
it('should update current value with first one', function() {
164+
expect(scenario.variable.current.value).to.eql('val5');
165+
expect(scenario.variable.current.text).to.eql('val5');
166+
});
167+
});
168+
129169
describeUpdateVariable('query variable with numeric results', function(scenario) {
130170
scenario.setup(function() {
131171
scenario.variable = { type: 'query', query: '', name: 'test', current: {} };

0 commit comments

Comments
 (0)