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