|
157 | 157 | *
|
158 | 158 | * @name template.custom
|
159 | 159 | * This might be numbers, strings, or even other variables.
|
160 |
| - * @param name Variable name |
| 160 | + * @param name Variable name. |
161 | 161 | * @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. |
163 | 163 | * @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.
|
164 | 164 | * @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.
|
165 | 165 | * @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 | 173 | custom(
|
174 | 174 | name,
|
175 | 175 | query,
|
176 |
| - current, |
| 176 | + current=null, |
177 | 177 | refresh='never',
|
178 | 178 | label='',
|
179 | 179 | valuelabels={},
|
|
186 | 186 | // self has dynamic scope, so self may not be myself below.
|
187 | 187 | // '$' can't be used neither as this object is not top-level object.
|
188 | 188 | local custom = self,
|
| 189 | + local query_array = std.split(query, ','), |
189 | 190 |
|
190 | 191 | allValue: allValues,
|
191 | 192 | current: {
|
|
200 | 201 | custom.valuelabel(current),
|
201 | 202 | [if multi then 'selected']: true,
|
202 | 203 | },
|
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), |
204 | 205 | hide: $.hide(hide),
|
205 | 206 | includeAll: includeAll,
|
206 | 207 | label: label,
|
|
217 | 218 | option(option):: {
|
218 | 219 | text: custom.valuelabel(option),
|
219 | 220 | 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 |
221 | 222 | std.member(current, option)
|
222 |
| - else if multi then |
223 |
| - current == option |
| 223 | + else if current == null then |
| 224 | + option == query_array[0] |
224 | 225 | else
|
225 |
| - null, |
| 226 | + option == current, |
226 | 227 | },
|
227 |
| - query_array(query):: std.split( |
228 |
| - if includeAll then 'All,' + query else query, ',' |
229 |
| - ), |
230 | 228 | },
|
231 | 229 | /**
|
232 | 230 | * [Text box variables](https://grafana.com/docs/grafana/latest/variables/variable-types/add-text-box-variable/)
|
|
0 commit comments