Just thought I'd post an alternative way of doing this. I'm not a big fan of adding javascript to my dashboards, so I've found a way to do this by adjusting the token for the input using the change & eval attributes. See the example below from one of our dashboards:
<input type="multiselect" token="host" searchWhenChanged="true">
<label>Hosts</label>
<search base="lookup">
<query>search $env$ | dedup host</query>
</search>
<valuePrefix>host=</valuePrefix>
<delimiter> OR </delimiter>
<default>*</default>
<choice value="*">All</choice>
<fieldForLabel>host</fieldForLabel>
<fieldForValue>host</fieldForValue>
<change>
<eval token="form.host">if(mvcount('form.host')=0,"*",if(mvcount('form.host')!=1,mvfilter('form.host'!="*"),'form.host'))</eval>
</change>
</input>
This will reset the token to 'All' if no options are selected, and will remove the 'All' option if more than 1 are selected.
UPDATE:
One of my colleagues found this didn't work properly. When you select a couple of hosts, the only way to get back to 'All' is to remove all your previous selections, at which point 'All' will re-appear. Below is the fix he added, this is the commentary from our internal Jira system:
Basically, because of the '*', the regex doesn't quite work, so we need to change the
<choice value="*">All</choice>
to
<choice value="All">All</choice>
so the regex mvfind works as expected. Then, set a second token dependent on the value of 'form.blah', which is then used in any post-process search [decided to change the name of the original multi-select form token, and then set a token with the old name, so it remains transparent to the post-process searches that use the original token].
So:
- it defaults to 'All' (All is alone at the start, index=0)
- if another env is chosen, 'All' is removed
- if 'All' is chosen, all other envs are removed (ie All is always alone at the start, index=0)
- if everything is removed, 'All' reappears (ie All is always alone at the start, index=0)
<input type="multiselect" token="env2" searchWhenChanged="true">
<label>Environment</label>
<search base="lookup">
<query>dedup environment</query>
</search>
<default>All</default>
<valuePrefix>environment=</valuePrefix>
<delimiter> OR </delimiter>
<choice value="All">All</choice>
<fieldForLabel>environment</fieldForLabel>
<fieldForValue>environment</fieldForValue>
<change>
<eval token="form.env2">case(mvcount('form.env2')=0,"All",mvcount('form.env2')>1 AND mvfind('form.env2',"All")>0,"All",mvcount('form.env2')>1 AND mvfind('form.env2',"All")=0,mvfilter('form.env2'!="All"),1==1,'form.env2')</eval>
<eval token="env">if(mvfind('form.env2',"All")=0,"environment=*",$env2$)</eval>
</change>
</input>
...still use $env$ where appropriate
<input type="dropdown" token="host" searchWhenChanged="true">
<label>Select a Host:</label>
<selectFirstChoice>true</selectFirstChoice>
<search base="lookup">
<query>search $env$ | dedup host</query>
</search>
Hope this helps, cheers.
... View more