Skip to Content
0

How to Set Default Value in XML View Select

Apr 03 at 10:50 PM

53

avatar image

Hey Everyone,

I have an edit screen that has a selection list. I get that list by populating a named model. I believe this is a pretty standard approach. I do not however, know how to set a default on my Select. I've read posts on how to do this with a json.view but I'm using an xml view.

Here is the component.js declaration:

this.setModel(new sap.ui.model.json.JSONModel(), "myStatusModel");

Here is the init of my controller:

var oStatusModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/ZCRM_OPPORTUNITY_SRV");
var that = this;
oStatusModel.read("UserStatuses", {
	success: function(oData) {
		var myStatusModel = that.getOwnerComponent().getModel("myStatusModel");
		myStatusModel.setData(oData.results);
	},
	error: function(error) {
		MessageToast.show(" Aww Snap!");
	}
});

And of course my View (just the select part):

<Select id="userStatus" 
        items="{myStatusModel>/}" 
        change="onUserStatusChange">
<core:Item key="{myStatusModel>UserStatusCode}" 
           text="{myStatusModel>UserStatusText}"/>
</Select>

The select defaults to the first item in the list. I have the default value in a {UserStatusText} element of my model but don't know how to make it default. Perhaps I shouldn't be using a Select?

I've read suggestions to change it on the backend, but that is not really an option I have.

Any advice is greatly appreciated...Thanks!

Greg

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Tri Minh Le Apr 03 at 11:13 PM
2

Hi Greg,

You can follow the example from SAPUI5 SDK. Here is the link.

Select

There is a property: selectedKey. You just set the value to the first item of your model.

Regards,

Tri

Show 4 Share
10 |10000 characters needed characters left characters exceeded

Thanks Tri,

I have tried using the selectedKey in the xml. However, this doesn't work and actually restricts the selection to just the first setting. Which is odd, I know. I think it is related to me using a named model and the selectedKey value is in the regular binding. Below is what I've tried.

<Select id="userStatus" 
		items="{myStatusModel>/}"  
		selectedKey="{UserStatusText}" 
		change="onUserStatusChange">
	<core:Item key="{myStatusModel>UserStatusCode}" 
	           text="{myStatusModel>UserStatusText}"/>
</Select>
0

The selectedKey must refer to the key values

key="{myStatusModel>UserStatusCode}" 

not the text value. Also make sure to refer to the model correctly.

selectedKey="{myStatusModel>UserStatusCode}" 

Regards,

Jamie

1

Thanks for your suggestion Jamie,

My named model "myStatusModel" is just an array of seven options for the Select. There is no indication in there as to what the main model has populated. I did find a workaround which I'll post now. You did happen to notice my typo though and I apologize for that. I was using UserStatusCode and not UserStatusText. - Greg

items="{myStatusModel>/}"  
  selectedKey="{myStatusModel>UserStatusCode}" 
  change="onUserStatusChange">
    <core:Item key="{myStatusModel>UserStatusCode}" 
               text="{myStatusModel>UserStatusText}"/>
0

Sometimes when a problem persists it is just best to move on, return a day later and bam. You spot your error and wonder how you missed it in the first place. Tri and Jamie were both spot on. Here is code that works, I was stupidly setting my named model in the selectedKey. duh. Maybe someone will get use out of my over-engineered process below.

<Select id="userStatus" 
		selectedKey="{UserStatusCode}"
		items="{myStatusModel>/}"  
		change="onUserStatusChange">
	<core:Item key="{myStatusModel>UserStatusCode}" 
			   text="{myStatusModel>UserStatusText}"/>
</Select
1
Greg Foss Apr 04 at 02:51 PM
0

This is how I worked around it. Kind of a hack, but it does work.

When I load the named model I go through the odata.results and put the selected one first, which will be the default value. I'll put the descriptions of what I did by number.

1. Get the path which is needed to get the current UserStatusCode.

2. Get the current UserStatusCode.

3. In the list of seven options in oData.results find which one is the current one by index.

4. Reorder oData.results, putting the index of the current value in position 0.

I'm not totally happy with this so I'm not going to post this as an answer for a couple days to give people a chance to do it better. Then again, it is only 7 lines of code.

var oStatusModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/ZCRM_OPPORTUNITY_SRV");
var that = this;
oStatusModel.read("UserStatuses", {
	success: function(oData) {
1-->		var sPath = that.getView().getBindingContext().getPath();
2-->		var sCurrentStatusCode = 
                   that.getView().getBindingContext().oModel.oData[sPath.substring(1)].UserStatusCode;
3-->		for (var i = 0; i < oData.results.length; i++) {
			if (oData.results[i].UserStatusCode === sCurrentStatusCode) {
4-->				oData.results.splice(0, 0, oData.results.splice(i, 1)[0]);
			}
		}
		var myStatusModel = that.getOwnerComponent().getModel("myStatusModel");
		myStatusModel.setData(oData.results);
	},
	error: function(error) {
		MessageToast.show("Status Model refused to bind. Aww Snap!");
	}
});

Share
10 |10000 characters needed characters left characters exceeded