H
ere’s a cool catalog client script that I figured out for a client. It allows you to move one or more selected options from one side of a list collector variable slushbucket to another. Using the script is pretty straight forward. Just supply the name of the list collector variable you are working with, and then make sure you provide an array of option IDs to move from one side to another. The option IDs need to be added to the ‘selectedIDs’ array in the middle chunk of code. The code below is set up to move ALL options in the right column of a slushbucket to the left.
//Name of variable to move options from
var varName = 'YOUR_VARIABLE_NAME_HERE';
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var selectedOptions = rightBucket.options;
//Get an array of all option IDs to move
var selectedIDs = new Array();
var index = 0;
for(var i = 0; i < selectedOptions.length; i++){
selectedIDs[index] = i;
index++;
}
//Move all returned options from right to left bucket and sort the results
//Switch 'rightBucket' and 'leftBucket' to move from left to right
moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
//Sort the resultant options in the left bucket
sortSelect(leftBucket);
var varName = 'YOUR_VARIABLE_NAME_HERE';
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var selectedOptions = rightBucket.options;
//Get an array of all option IDs to move
var selectedIDs = new Array();
var index = 0;
for(var i = 0; i < selectedOptions.length; i++){
selectedIDs[index] = i;
index++;
}
//Move all returned options from right to left bucket and sort the results
//Switch 'rightBucket' and 'leftBucket' to move from left to right
moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
//Sort the resultant options in the left bucket
sortSelect(leftBucket);
Comments
Posted On
Sep 07, 2010Posted By
Phil WrightGreat script!! I do have a question. How do I reference the value of an item in the left bucket. What I want to do is step through the left bucket and move only certain one to the right based on a dynamic condition. I have tried:
selectedOptions[i]
selectedOptions[i].value
selectedOptions.value[i]
Any ideas??
Phil
Posted On
Sep 07, 2010Posted By
Mark StangerYou should just have to modify the ‘for’ loop to be something like this…
//Check for the item to add here based on sys_id
if(selectedOptions[i].value == 'sys_id_value_of_item'){
//Or check for item here based on display value
//if(selectedOptions[i].text == 'display_value_of_item'){
selectedIDs[index] = i;
index++;
}
}
Posted On
Sep 08, 2010Posted By
Chris YorkMark,
Thanks for this script. This has helped me out a great deal. I have one issue I can’t figure out though. I am using an onChange client Script to move the List items from List Collector 2 when an item in List Collector 1 is changed. the onChange event occurs even when simply clicking in the list, not necessarily when moving an item from left/right or right/left. Therefore my script runs prematurely. Comparing oldValue to newValue doesn’t appear to be worthwhile because oldValue is always empty. Is there a way to see previous list prior to the onChange? Or is there a way to only fire the onChange event when an item is really moved?
Thanks!
Posted On
Sep 08, 2010Posted By
Mark StangerHey Chris. Unfortunately I’m not aware of a way around that. That actually sounds like a bug to me though. The onChange event shouldn’t be triggering unless an item is moved from one side to the other. I think you’ll have to open that one with Service-now support.
Posted On
Nov 25, 2010Posted By
Russ HartHi Mark,
Very useful script !..
Do you know of a way to modify or remove the add / remove labels from a catalog list collector ?
Posted On
Nov 26, 2010Posted By
Mark StangerThere’s not really a good way to change this for single variables/items because it would require a client script with easy access to those labels. Neither of those labels have a DOM ID so you could use a client script hack and modify them but that’s probably not a good (or simple) solution.
The simplest way of changing them would be a global setting for ALL slushbuckets in the system. You can just go to ‘System UI->Messages’ and search for the messages with keys of ‘uppercase_add’ and ‘Remove’ and change the associated ‘Message’ values.
Posted On
Apr 18, 2011Posted By
Jim CoyneJust wondering where the “moveSelectedOptions” function is hiding? I was trying to figure out why the “–None–” item is not being removed from the right bucket as I move stuff over from the left (I swapped the left/right variables in the function call). I added the check for a particular set of values in the left bucket and move them over if they exist instead of moving all items over.
//check for items here based on display value
if((selectedOptions[i].text.toLowerCase() == 'string1')||(selectedOptions[i].text.toLowerCase() == 'string2')){
selectedIDs[index] = i;
index++;
}
}
moveSelectedOptions(selectedIDs, leftBucket, rightBucket,'--None--');
sortSelect(rightBucket);
Posted On
Apr 19, 2011Posted By
Mark StangerYou can dig into the code by using the script panel in a dom inspector. I use firebug most of the time. You can search through any client scripts loaded for a given page. You should be able to find the ‘moveSelectedOptions’ function by searching for ‘function moveSelectedOptions’ from the script panel in firebug.
As to why it’s not removing the ‘–None–’ value, the system adds that after the fact any time the right slushbucket ends up empty as a result of the move. I’m not sure why you would want to change that behavior, but you could add “rightBucket.options.length = ’0′;” to the end of your script to wipe out the ‘–None–’ value.
Posted On
Apr 19, 2011Posted By
Jim CoyneThe problem is “–None–” is still in the right bucket list when I move over 2 items into the right from the left. The catalog item starts off with 10 items in the left bucket list and I have an onLoad script to pre-populate the right bucket with 2 of those items. I end up with 3 items in the right bucket once I run my code above: “–None–” and the 2 default items I need over there. I added rightBucket.options.length = ’0′; before the move and it is working OK now.
Thanks