[SalesForce] How to remove the selected values from multi select picklist

I have 2 multi-select picklists and I want to:
1. Add all the values selected in Picklist1 to Picklist2
2. Remove the selected values from Picklist1 after they are added to Picklist2

While, I am able to achieve the 1st requirement.
How to achieve the 2nd requirement.

Best Answer

Ok, so assuming you have just completed step 1, you have a large list in picklist 1 and a smaller list in picklist two.

I'm also assuming that you are using databinding in lightning are are not directly manipulating the DOM

Given these conditions, run your first list through this function. It will remove any dupe values already in the second list:

xorSourceItems : function(source,dest) {
  var itemsToReturn = [];
  source.forEach(function(sourceItem, sourceIndex) {
    var match = false;
    dest.forEach(function(destItem, destIndex) {
      if (destItem.value == sourceItem.value) {
        match = true;
      }
    });
    if (!match){
        itemsToReturn.push(sourceItem)
    }
  });
  return itemsToReturn;
}
Related Topic