Search code examples
javascriptsapui5

how to set combo box default value as session logged in user id in sap ui5


I am using comboBox control in sap ui5. I need to show default logged in user id. How can I achieve so? This default user id is the part of odata service. For example I have 10 data(INC0001 to INC0010). If user INC0004 logged in then by default I want to show that in the comboBox. I am getting only first blank right now.

XML Code :

<Select id="select1" 
        items="{ path: '/UserSet', sorter: { path: 'zuserid' } }" 
        change="handleChange">

     <core:Item text="{zuserid}" key="{zuserid}"/>
     <layoutData>
         <l:GridData span="XL2 L2"/>
     </layoutData>

</Select>

Session user in sap ui5:

var userId = sap.ushell.Container.getService("UserInfo").getUser().getId();

Solution

  • This is the solution for this issue. This will help anyone is looking out for the same.

     var oData ={
                    recipient :{
                       name :"TCS0001"}};
    
        var oModel =newJSONModel(oData);
        this.getView().setModel(oModel,"NamedModel");
    
    
        <ComboBox id="combo1" selectedKey="{NamedModel>/recipient/name}" items="{ path: '/UserSet', sorter: { path: 'zuserid' } }" change="handleChange">
                <core:Item text="{zuserid}" key="{zuserid}"/>
                <layoutData>
                 <l:GridData span="XL3 L3"/>
                </layoutData>
               </ComboBox><br>