cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to avoid model changes using selected key in sap.m.select?

0 Likes
903
<Select id="selApplication" forceSelection="false" change="handleChangeApplication" selectedKey="{/Applications/0/Application}" items="{path: '/Applications'}"> 
<core:Item key="{Application}" text="{Name}"/>
 </Select>

I am using the above code snippet.

The JSONModel looks like this:

{ 
"Applications": [{ 
    "Application": "GRN", 
    "Name": "Goods Receipt Note"
    },
    { 
    "Application": "NPO",
    "Name": "Non-PO Invoice Approval"
     }] 
}

On initial load of application the Model remains same as above.

But when I change the selection in UI from GRN to NPO, on debugging the application in handleChangeApplication method, the Model changes to following:

{
"Applications": [{ 
    "Application": "NPO", 
    "Name": "Goods Receipt Note"
    },
    { 
    "Application": "NPO",
    "Name": "Non-PO Invoice Approval"
     }] 
}

As we can see, the Application value is automatically changed, this is causing issues.

Please help me in understanding where I am going wrong. Is this an issue with how I am binding the selectedKey?

View Entire Topic
Ricardo___
Participant
0 Likes

I think this has to do with a reference problem in javascript objects, to avoid this I usually define a new property in the model.

<Select id="selApplication" forceSelection="false" change="handleChangeApplication" selectedKey="{/sSelectedKeyApplication}" items="{path: '/Applications'}">
	<core:Item key="{Application}" text="{Name}"/>
</Select>
//we define the key of the first element of the array
var aApplications = oModelJson.getProperty("/Applications");
var sKeyApplication = aApplications[0].Application;
oModelJson.setProperty("/sSelectedKeyApplication",sKeyApplication);

I hope it helps.

Hi Ricardo,

It works fine. Thank you.

Also, went through the Chrome console to find that the default binding is 2 way. We can also use the below snippet to change it to OneWay.

oModel.setDefaultBindingMode(sap.ui.model.BindingMode.OneWay);

After the call, all future model assignments will be one way after this call.

Ricardo___
Participant
0 Likes

glad it worked.