You need to sign in to do that
Don't have an account?
prasad vivek
Hi, I have a doubt regarding dual list box in lightning
Hi i have a doubt regarding dual list box in lightning, on page load I am trying to assign selected values attribute by using a list, but I am not able to do so. Can anyone help me on this,
My code is
<aura:attribute name="GeneralList" type="List" default="[]" description="Genre Picklist Values"/>
<aura:attribute name="selectedList" type="List" default="[]" description="Selected Genre Picklist Values"/>
<div class="slds-m-around_xx-large">
<lightning:dualListbox aura:id="select"
name="Genre"
label="Select"
sourceLabel="Available"
selectedLabel="Selected"
options="{!v.GeneralList}"
value="{!v.selectedList}"
onchange="{!c.handleChange}"/>
JS Controller
doInit: function(component, event, helper) {
var action = component.get("c.getPicklistValues");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS"){
var result = response.getReturnValue();
var picklistValues = [];
var selectedlist = [];
for (var i = 0; i < result.length-5; i++) {
picklistValues.push({
label: result[i],
value: result[i]
});
}
for (var i = 4; i < result.length; i++) {
selectedlist.push({
label: result[i],
value: result[i]
});
}
alert('picklistValues'+JSON.stringify(picklistValues));
component.set("v.GeneralList", picklistValues);
// Hard coding is done
//alert('selectedlist'+ JSON.stringify(selectedlist));
component.set("v.selectedList",selectedlist);
alert('get selected list'+JSON.stringify(component.get("v.selectedList")));
}
});
$A.enqueueAction(action);
}
My code is
<aura:attribute name="GeneralList" type="List" default="[]" description="Genre Picklist Values"/>
<aura:attribute name="selectedList" type="List" default="[]" description="Selected Genre Picklist Values"/>
<div class="slds-m-around_xx-large">
<lightning:dualListbox aura:id="select"
name="Genre"
label="Select"
sourceLabel="Available"
selectedLabel="Selected"
options="{!v.GeneralList}"
value="{!v.selectedList}"
onchange="{!c.handleChange}"/>
JS Controller
doInit: function(component, event, helper) {
var action = component.get("c.getPicklistValues");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS"){
var result = response.getReturnValue();
var picklistValues = [];
var selectedlist = [];
for (var i = 0; i < result.length-5; i++) {
picklistValues.push({
label: result[i],
value: result[i]
});
}
for (var i = 4; i < result.length; i++) {
selectedlist.push({
label: result[i],
value: result[i]
});
}
alert('picklistValues'+JSON.stringify(picklistValues));
component.set("v.GeneralList", picklistValues);
// Hard coding is done
//alert('selectedlist'+ JSON.stringify(selectedlist));
component.set("v.selectedList",selectedlist);
alert('get selected list'+JSON.stringify(component.get("v.selectedList")));
}
});
$A.enqueueAction(action);
}
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Component:
Controller:
Helper:
Apex:
Reference: http://sfdcmonkey.com/2018/02/25/fetch-multi-picklist-values-lightningduallistbox-component/
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
You just need to push result[i] direct into list(selectedlist) as given below:
for (var i = 4; i < result.length; i++) {
selectedlist.push(result[i]);
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks.
Deepali Kulshrestha