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
Soundar Rajan PonpandiSoundar Rajan Ponpandi 

How to display a Picklist value based on selected Picklist in lightning component

Hi,

I would like to display a users based on selected department.

1. I am using 2 objects for this functionality 
              A. User Assignment  (here user & department is a picklist )
              B. Department 
2. I need to display a users based on selected department (users are mapped in User Assignment Object)

I tried in my way, I can return from a class but list of users is not displaying while onchange.

Please review my code and let me know where i did a mistake.

Component.
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader" controller="GD_CreateQuoteTeam" access="global">
    
      
    <aura:attribute name="recordId" type="String" />
   <aura:attribute name="depts" type="list"/>
    <aura:attribute name="usersOrg1" type="list"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <div class="slds"> 
        <div class="slds-form-element" align="center"> 
            <h3 class="slds-section-title--divider"><b>Add Quote Team Members</b></h3>
        </div>
    </div>
    <br/>
    <div class="container-fluid">
        <table class="slds-table slds-table_bordered slds-table_cell-buffer">
            <thead>
                <tr class="slds-text-title_caps">
                    <th scope="col">
                        <!--<div class="slds-truncate">Sr.no</div>-->
                    </th>
                     <th scope="col">
                        <div class="slds-truncate" title="Team Role"><b><span style="color: red;">*</span>Department</b></div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate" title="User"><b><span style="color: red;">*</span>User</b></div>
                    </th>
                    
                </tr>
            </thead>
            <tbody>    
                <aura:iteration items="{!v.QuoteList}" var="quote" indexVar="index">
                    <tr>
                        <td> 
                            <br/>{!index + 1}
                        </td>
                        <td>
                            <lightning:select value="{!quote.GD_Department__c}" onchange="{!c.onChangeVal}">
                                <option value="">-- None --</option>
                                <aura:iteration items="{!v.depts}" var="dept" >
                                    <option  value="{!dept.value}" >{!dept.label}</option> 
                                </aura:iteration>
                            </lightning:select>
                        </td>
                       
                        <td>
                            <lightning:select value="{!quote.GD_User__c}">
                                <option value="">-- None --</option>
                                <aura:iteration items="{!v.usersOrg1}" var="user" >
                                    <option  value="{!user.value}" >{!user.label}</option> 
                                </aura:iteration>
                            </lightning:select>
                        </td>
                        
                    </tr>
                    
                </aura:iteration>
            </tbody>
        </table>
    </div>
</aura:component>

Controller.JS
doInit: function(component, event, helper) {
        helper.getDepartmentPickList(component, event);
        helper.getUserOrgPickList(component, event);
    },

onChangeVal:function (component, event, helper) {
        var val = event.getSource().get("v.value");
        console.log('val'+val);
         alert('On Change' + val);
        var action = component.get("c.getSelectedUser");
        action.setParams({"sname":val});
        action.setCallback(this, function(result){
            var users = result.getReturnValue();
            component.set("v.usersOrg1", users);
        });
        $A.enqueueAction(action);
    },

Helper.Js
getUserOrgPickList: function(component, event) {
        var action = component.get("c.queryUsersOrg1");
            action.setCallback(this, function(data) {
                var result = data.getReturnValue()
				var opts = [];
			for (var i in result){
                console.log(i);
                    opts.push({
                        class: "optionClass",
                        label: result[i],
                        value: i
                    });
                }
                component.set("v.usersOrg", opts);
        });
        $A.enqueueAction(action);
        
    },

getDepartmentPickList: function(component, event) {
        var action = component.get("c.queryDepartments");
            action.setCallback(this, function(data) {
                var result = data.getReturnValue()
				var opts = [];
			for (var i in result){
                console.log(i);
                    opts.push({
                        class: "optionClass",
                        label: result[i],
                        value: i
                    });
                }
                component.set("v.depts", opts);
        });
        $A.enqueueAction(action);
        
    },



Apex Class
@AuraEnabled
    public static Map<String, String> queryUsersOrg1(string sname) {
        Map<String, String> usersOrg1 = new Map<String, String>();
        system.debug('sname' + sname);
        for(GD_User_Organization_Assignments__c usrOrg : [select ID, Name, GD_User__c,GD_Department__c,GD_Department__r.Name, GD_User__r.Name from GD_User_Organization_Assignments__c where GD_Department__c  =: sname]) {
        	//usersOrg.put('--None--', '-- None --');  
        	system.debug('usrOrg Query ***' +usrOrg.GD_User__r.Name + ' - ' + usrOrg.GD_Department__r.Name);
            usersOrg1.put(usrOrg.GD_User__c, usrOrg.GD_User__r.Name);     
        }
        return usersOrg1; 
    }


Thanks in Advance,
Soundar.
            
Best Answer chosen by Soundar Rajan Ponpandi
Gaurav Sharma 472Gaurav Sharma 472
Code looks correct. Please try to debug through google developer tools what is happening. Also in aura,you are iterating over label and value of v.usersOrg1. But in js controller you are iust assigning returned value to this. Make sure correct format is passed to the component

All Answers

Gaurav Sharma 472Gaurav Sharma 472
DO A METHOD EXIST IN APEX WITH NAME  getSelectedUser   WHICH IS RETURNING ALL USERS FOR GIVEN DEPARTMENT?
Soundar Rajan PonpandiSoundar Rajan Ponpandi
HI Gaurav,

Thanks for your quick response.

Yes that is correct "getSelectedUser" method returning users as well and i returned in the variable "usersOrg1". But it's not displaying correctly when i select the department.
 
onChangeVal:function (component, event, helper) {
        var val = event.getSource().get("v.value");
        console.log('val'+val);
         alert('On Change' + val);
        var action = component.get("c.getSelectedUser");
        action.setParams({"sname":val});
        action.setCallback(this, function(result){
            var users = result.getReturnValue();
            component.set("v.usersOrg1", users);
        });
        $A.enqueueAction(action);
    },

Please advise me how to handle this situation, Else do you have sample coding ?

Regards,
Soundar.
Gaurav Sharma 472Gaurav Sharma 472
Code looks correct. Please try to debug through google developer tools what is happening. Also in aura,you are iterating over label and value of v.usersOrg1. But in js controller you are iust assigning returned value to this. Make sure correct format is passed to the component
This was selected as the best answer