Default Selected list item in Master view of the SplitApp

sapui5

I have one SplitApp with Master -Detail layout. I would like to know how can I set the first item in Master view to default so that on loading of application Detail view shows the information about the select list item. So when user open the application automatically first item in Master should be selected and Detail View show the information.

I am using Objectlist Item as control for Master view. And currently using the select event for selecting the list item.

var oList = new sap.m.List("idMasterList",{
     mode: sap.m.ListMode.SingleSelect,
     select: [oController.onSelectItem, oController]
            });

onSelectItem: function(oEvent){

 //var app = sap.ui.getCore().byId("splitApp");
 var oMasterList = sap.ui.getCore().byId("idMasterList");
 var oSelItem = oMasterList.getSelectedItem();

 var sPath = oSelItem.oBindingContexts.druginfo.sPath; 
 var oItem = sap.ui.getCore().getModel("druginfo").getProperty(sPath);
 var oSelModel = new sap.ui.model.json.JSONModel(oItem) ;
 sap.ui.getCore().setModel(oSelModel, "SelectedItem");

 }

Regards,
Mayank

Best Answer

It seems like there is (hidden) API to make the select event fire when setting the selected item:

ListBase.prototype.setSelectedItem = function(oListItem, bSelect, bFireEvent) {
    if (this.indexOfItem(oListItem) < 0) {
        jQuery.sap.log.warning("setSelectedItem is called without valid ListItem parameter on " + this);
        return;
    }
    if (this._bSelectionMode) {
        oListItem.setSelected((bSelect === undefined) ? true : !!bSelect);
        bFireEvent && this._fireSelectionChangeEvent([oListItem]);
    }
};

You could use setSelectedItem once your lists data is loaded (e.g. change event of aggregation binding items) like this:

var oList = this.getView().byId("MyListID"),
    oFirstItem = oList.getItems()[0];
oList.setSelectedItem(oFirstItem, true, true);

This will trigger the selectionChange resp. select event and your already existing event listener will be triggered.

Related Topic