You need to sign in to do that
Don't have an account?
Rakesh 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 .
<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 .
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
MyCustom_ListController.js
Results :
1.Selection
2.Added
3.Removal Selection
4.Remove