Last active
August 1, 2019 01:29
-
-
Save hobbes3/7c52b67c1de5ba4d9dfe to your computer and use it in GitHub Desktop.
multiselect force default select input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TESTED in 7.3.x and 6.5.x (so should work in all other versions) | |
require([ | |
'jquery', | |
'underscore', | |
'splunkjs/mvc', | |
'splunkjs/mvc/simplexml/ready!' | |
], function( | |
$, | |
_, | |
mvc | |
) { | |
var multiselects = _(mvc.Components.toJSON()).filter(function(obj) { | |
// This seems hacky; there should be a better way to do this | |
return obj.settings && obj.settings.get("type") === "multiselect" && obj.$el[0].className.indexOf("input-multiselect") > -1; | |
}); | |
_(multiselects).each(function(multiselect) { | |
multiselect.on("change", function() { | |
var values = this.val(); | |
// I assume the default multiselect value will be the first (hardcoded) choice | |
// Like <choice value="*">All</choice> | |
// If there is no hardcoded choice, then .options.choices[0] won't exist... | |
var first_choice_value = this.options.choices[0].value; | |
// If the user removed everything then add the first choice "All" back | |
if(values.length === 0) { | |
this.val([first_choice_value]); | |
} | |
// If the user choose something else then remove the first choice "All" (if it's there) | |
else if(values.indexOf(first_choice_value) >= 0 && values.length > 1) { | |
this.val(_.without(values, first_choice_value)); | |
} | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
Great JS, used it often in 6.3 & 6.4, so big thanks !
Apparently, there is a change in 6.5 that prevent it to work :/
They are talking about it there :
https://answers.splunk.com/answers/456860/why-are-javascript-files-not-loading-after-update-1.html
Trying to debug, without luck for now :/