Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0deceee

Browse files
committedJul 16, 2021
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 3082bfc commit 0deceee

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
@@ -157,9 +157,9 @@
157157
*
158158
* @name template.custom
159159
* This might be numbers, strings, or even other variables.
160-
* @param name Variable name
160+
* @param name Variable name.
161161
* @param query Comma separated without spacing list of selectable values.
162-
* @param current Selected value
162+
* @param current Selected value. If not specified, first value is selected.
163163
* @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.
164164
* @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.
165165
* @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'}`.
@@ -173,7 +173,7 @@
173173
custom(
174174
name,
175175
query,
176-
current,
176+
current=null,
177177
refresh='never',
178178
label='',
179179
valuelabels={},
@@ -186,6 +186,7 @@
186186
// self has dynamic scope, so self may not be myself below.
187187
// '$' can't be used neither as this object is not top-level object.
188188
local custom = self,
189+
local query_array = std.split(query, ','),
189190

190191
allValue: allValues,
191192
current: {
@@ -200,7 +201,7 @@
200201
custom.valuelabel(current),
201202
[if multi then 'selected']: true,
202203
},
203-
options: std.map(self.option, self.query_array(query)),
204+
options: std.map(self.option, if includeAll then ['All'] + query_array else query_array),
204205
hide: $.hide(hide),
205206
includeAll: includeAll,
206207
label: label,
@@ -217,16 +218,13 @@
217218
option(option):: {
218219
text: custom.valuelabel(option),
219220
value: if includeAll && option == 'All' then '$__all' else option,
220-
[if multi then 'selected']: if multi && std.isArray(current) then
221+
selected: if multi && std.isArray(current) then
221222
std.member(current, option)
222-
else if multi then
223-
current == option
223+
else if current == null then
224+
option == query_array[0]
224225
else
225-
null,
226+
option == current,
226227
},
227-
query_array(query):: std.split(
228-
if includeAll then 'All,' + query else query, ','
229-
),
230228
},
231229
/**
232230
* [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)
This repository has been archived.