function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh RRakesh R 

Hello! i am trying to make a custom multipick list component to add and remove items with the help of Checkbox Group but its not working properly. Please help me.

component code:
<aura:component >    
    <aura:attribute name="options" type="List" default="[
                                                        {'label': 'Mobile', 'value': 'Mobile'},
                                                        {'label': 'Laptop', 'value': 'Laptop'},
                                                        {'label': 'Hardware', 'value': 'Hardware'},
                                                        {'label': 'Software', 'value': 'Software'},
                                                        {'label': 'TV', 'value': 'TV'},
                                                        {'label': 'Cars', 'value': 'Cars'}
                                                        ]"/>
    <aura:attribute name="select" type="List" default="option1"/>
    
    <aura:attribute name="addItems" type="List"/>
    <aura:attribute name="selectedAddItems" type="List"/>
    <div class="flex-container">
        <div class="border">
            <lightning:checkboxGroup name="unSelected"
                                     label="Unselected Items"
                                     options="{! v.options }"
                                     value="{! v.select }"
                                     onchange="{! c.handleChange1 }"/>
        </div>
        
        <lightning:button label="Add" onclick="{!c.addValue}"/>
        
        <lightning:button label="Remove" onclick="{!c.removeValue}"/>
        
        <div>
            <lightning:checkboxGroup name="selected"
                                     label="Selected Items"
                                     options="{!v.addItems}"
                                     value="{!v.selectedAddItems}"
                                     onchange="{!c.handleChange2}"/>
        </div>
    </div>
</aura:component>

controller code:
({
    // var itemarray:[];
    handleChange1: function (cmp, event)
    {
     // This will contain the string of the "value" attribute of the selected option
     var selectedOptionValue = event.getParam("value");
     //var shw = 
     //alert('selected value list'+selectedOptionValue.indexOf());
     
    },
    
    addValue : function(component, event, helper) 
    {
     // get all value from the list
     
     var clearitems = component.get("v.options");
     
     // get all selected value from the list
     var gtckboxValue = component.get("v.select");
     //alert('selected value list'+gtckboxValue.length);
     var itemsArray=[];
    
     var j=0;
     for(var i=0 ; i< gtckboxValue.length; i++){
         j++;
         itemsArray.push({
             label: gtckboxValue[i],
             value: gtckboxValue[i]
         });
         clearitems.splice(i,1);
         
         // component.set("v.options",clearitems);
         // component.set("v.addItems",itemsArray);
         /*var index = gtckboxValue.indexOf(i);
            alert('index of selected items'+index);
            if(index > -1){
                gtckboxValue.splice(index, 1);
            }*/
         
     }    
     component.set("v.addItems",itemsArray);
     //clearitems.splice(j,1);
     component.set("v.options",clearitems);
    },
    handleChange2: function (cmp, event) {
        // This will contain the string of the "value" attribute of the selected option
        var selectedOptionValue = event.getParam("value");
        alert('adffdji'+selectedOptionValue);
        
    },
    
    removeValue : function(component, event, helper) {
        var gtckboxValue = component.get("v.select");
        
    }
})
Lightning Application code:
<aura:application extends="force:slds">
    <c:MyCustom_List/>
</aura:application>

Thanks in advanced .
Best Answer chosen by Rakesh R
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi Raka,

Here is the working code. I have fixed it for you and please mark it as best answer, as it will benefit others too.


MyCustom_List.cmp
<aura:component >    
    <aura:attribute name="options" type="List" default=""/>
    <aura:attribute name="select" type="List" default=""/>
     
    <aura:attribute name="addItems" type="List"/>
    <aura:attribute name="selectedAddItems" type="List"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.init }"/>
    
    <div class="flex-container">
        <div class="border">
            <lightning:checkboxGroup name="unSelected"
                                     label="Unselected Items"
                                     options="{! v.options }"
                                     value="{! v.select }"
                                     onchange="{! c.handleChange1 }"/>
        </div>
        
        <lightning:button label="Add" onclick="{!c.addValue}"/>
        
        <lightning:button label="Remove" onclick="{!c.removeValue}"/>
        
        <div>
            <lightning:checkboxGroup name="selected"
                                     label="Selected Items"
                                     options="{!v.addItems}"
                                     value="{!v.selectedAddItems}"
                                     onchange="{!c.handleChange2}"/>
        </div>
    </div>
</aura:component>

MyCustom_ListController.js
 
({
    init: function (cmp, event, helper) {
            cmp.set('v.options',[{'label': 'Mobile', 'value': 'Mobile'},
                {'label': 'Laptop', 'value': 'Laptop'},
                {'label': 'Hardware', 'value': 'Hardware'},
                {'label': 'Software', 'value': 'Software'},
                {'label': 'TV', 'value': 'TV'},
                {'label': 'Cars', 'value': 'Cars'}
            ]);
     },  
    
   
    handleChange1: function (component, event) { },
    
    addValue : function(component, event, helper,row) 
    {
       
         var gtckboxValue = [];
         var ClearItems = component.get("v.options");
         var MoveItems = component.get("v.addItems");
        
         gtckboxValue = component.get("v.select");
        
        for(var i=0; i < gtckboxValue.length; i++){
         for(var j=0; j < ClearItems.length; j++){   
            if(gtckboxValue[i] == ClearItems[j].value){
               MoveItems.push(ClearItems[j]);
               ClearItems.splice([j], 1);
             }
         }    
        }
 
         component.set("v.addItems",MoveItems);
         component.set("v.options",ClearItems);
         component.set("v.select",[]);
       
    },
    handleChange2: function (component, event) { },
    
    removeValue : function(component, event, helper) {
         var gtckboxValue = [];
         var ClearItems = component.get("v.addItems");
         var MoveItems = component.get("v.options");
        
         gtckboxValue = component.get("v.selectedAddItems");
       
        for(var i=0; i < gtckboxValue.length; i++){
         for(var j=0; j < ClearItems.length; j++){   
            if(gtckboxValue[i] == ClearItems[j].value){
               MoveItems.push(ClearItems[j]);
               ClearItems.splice([j], 1);
             }
         }    
        }
 
         component.set("v.addItems",ClearItems);
         component.set("v.options",MoveItems);
         component.set("v.selectedAddItems",[]);

        
    },
})

Results :
1.Selection
User-added image
​​​​​​​
2.Added
User-added image

3.Removal Selection
User-added image

4.Remove
User-added image