Skip to Content

How to Set Default Value in XML View Select

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Apr 03 at 11:13 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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
      
  • Apr 04 at 02:51 PM

    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!");
    	}
    });
    

    Add comment
    10|10000 characters needed characters exceeded