Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit b87fe2d

Browse files
Add "selected" field to custom template variable when multi=false
Add "selected" field to custom template variable when multi=false, based on Grafana behavior. Select first value if current is not specified.
1 parent ba94551 commit b87fe2d

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

grafonnet/template.libsonnet

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@
158158
*
159159
* @name template.custom
160160
* This might be numbers, strings, or even other variables.
161-
* @param name Variable name
161+
* @param name Variable name.
162162
* @param query Comma separated without spacing list of selectable values.
163-
* @param current Selected value
163+
* @param current Selected value. If not specified, first value is selected.
164164
* @param refresh (default `'never'`) `'never'`: Variables queries are cached and values are not updated. This is fine if the values never change, but problematic if they are dynamic and change a lot. `'load'`: Queries the data source every time the dashboard loads. This slows down dashboard loading, because the variable query needs to be completed before dashboard can be initialized. `'time'`: Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.
165165
* @param label (default `''`) Display name of the variable dropdown. If you don’t enter a display name, then the dropdown label will be the variable name.
166166
* @param valuelabels (default `{}`) Display names for values defined in query. For example, if `query='new,old'`, then you may display them as follows `valuelabels={new: 'nouveau', old: 'ancien'}`.
@@ -174,7 +174,7 @@
174174
custom(
175175
name,
176176
query,
177-
current,
177+
current=null,
178178
refresh='never',
179179
label='',
180180
valuelabels={},
@@ -187,6 +187,7 @@
187187
// self has dynamic scope, so self may not be myself below.
188188
// '$' can't be used neither as this object is not top-level object.
189189
local custom = self,
190+
local query_array = std.split(query, ','),
190191

191192
allValue: allValues,
192193
current: {
@@ -201,7 +202,7 @@
201202
custom.valuelabel(current),
202203
[if multi then 'selected']: true,
203204
},
204-
options: std.map(self.option, self.query_array(query)),
205+
options: std.map(self.option, if includeAll then ['All'] + query_array else query_array),
205206
hide: $.hide(hide),
206207
includeAll: includeAll,
207208
label: label,
@@ -218,16 +219,13 @@
218219
option(option):: {
219220
text: custom.valuelabel(option),
220221
value: if includeAll && option == 'All' then '$__all' else option,
221-
[if multi then 'selected']: if multi && std.isArray(current) then
222+
selected: if multi && std.isArray(current) then
222223
std.member(current, option)
223-
else if multi then
224-
current == option
224+
else if current == null then
225+
option == query_array[0]
225226
else
226-
null,
227+
option == current,
227228
},
228-
query_array(query):: std.split(
229-
if includeAll then 'All,' + query else query, ','
230-
),
231229
},
232230
/**
233231
* [Text box variables](https://grafana.com/docs/grafana/latest/variables/variable-types/add-text-box-variable/)

tests/template/custom_compiled.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
"name": "foo",
1313
"options": [
1414
{
15+
"selected": true,
1516
"text": "nouveau",
1617
"value": "new"
1718
},
1819
{
20+
"selected": false,
1921
"text": "ancien",
2022
"value": "old"
2123
}
@@ -37,10 +39,12 @@
3739
"name": "host",
3840
"options": [
3941
{
42+
"selected": false,
4043
"text": "foo",
4144
"value": "foo"
4245
},
4346
{
47+
"selected": true,
4448
"text": "bar",
4549
"value": "bar"
4650
}

0 commit comments

Comments
 (0)