简体   繁体   中英

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();

Have you tried binding the data in your controller?

this.getView().byId("select1").bindItems({
            path: "/UserSet",
            template: new sap.ui.core.Item({
                key: "{zuserid}"
                text: "{zuserid}"
            }),
            events: {
                dataReceived: function () {
                    var userId = sap.ushell.Container.getService("UserInfo").getUser().getId();
                    this.getView().byId("select1").setSelectedKey(userId);
                }.bind(this)
            }
        });

The event dataReceived is triggered once the data is received and at that point it is possible to set the selected key of your ComboBox as @Jorg proposes in the reaction above.

Select has a parameter called selectedKey you can use for this. Usually this is another model binding but you can do it programmatically as well.

The SDK has an example: https://sapui5.netweaver.ondemand.com/sdk#/entity/sap.m.Select/sample/sap.m.sample.Select/code

            <Select
                forceSelection="false"
                selectedKey="{/SelectedProduct}"
                items="{
                    path: '/ProductCollection',
                    sorter: { path: 'Name' }
                }">
                <core:Item key="{ProductId}" text="{Name}" />
            </Select>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM